vbv/client/src/services/dashboard.ts

199 lines
5.1 KiB
TypeScript

import { graphqlClient } from "@/graphql/client";
import {
DASHBOARD_CONFIG,
DASHBOARD_COURSE_SESSION_PROGRESS,
DASHBOARD_COURSE_STATISTICS,
DASHBOARD_MENTOR_COMPETENCE_SUMMARY,
} from "@/graphql/queries";
import { itGetCached } from "@/fetchHelpers";
import type {
AssignmentsStatisticsType,
CourseProgressType,
CourseStatisticsType,
DashboardConfigType,
} from "@/gql/graphql";
import type { DashboardPersonsPageMode, DueDate } from "@/types";
export type DashboardPersonRoleType =
| "SUPERVISOR"
| "EXPERT"
| "MEMBER"
| "LEARNING_MENTOR"
| "LEARNING_MENTEE";
export type DashboardRoleKeyType =
| "Supervisor"
| "Trainer"
| "Member"
| "MentorUK"
| "MentorVV";
export type WidgetType =
| "ProgressWidget"
| "CompetenceWidget"
| "MentorTasksWidget"
| "MentorPersonWidget"
| "MentorCompetenceWidget"
| "CompetenceCertificateWidget"
| "UKStatisticsWidget";
export type DashboardPersonCourseSessionType = {
id: string;
session_title: string;
course_id: string;
course_title: string;
course_slug: string;
region: string;
generation: string;
user_role: DashboardPersonRoleType;
user_role_display: string;
my_role: DashboardPersonRoleType;
my_role_display: string;
is_uk: boolean;
is_vv: boolean;
competence_metrics?: {
passed_count: number;
failed_count: number;
};
};
export type DashboardPersonType = {
user_id: string;
first_name: string;
last_name: string;
email: string;
course_sessions: DashboardPersonCourseSessionType[];
avatar_url: string;
avatar_url_small: string;
competence_metrics?: {
passed_count: number;
failed_count: number;
};
};
export type DashboardCourseConfigType = {
course_id: string;
course_slug: string;
course_title: string;
role_key: DashboardRoleKeyType;
is_uk: boolean;
is_vv: boolean;
is_mentor: boolean;
widgets: WidgetType[];
has_preview: boolean;
session_to_continue_id: string;
};
export type DashboardDueDate = DueDate & {
course_session: DashboardPersonCourseSessionType;
translatedType: string;
persons?: DashboardPersonType[];
};
export const fetchStatisticData = async (
courseId: string
): Promise<CourseStatisticsType | null> => {
try {
console.log("fetching statistics for course ID: ", courseId);
const res = await graphqlClient.query(DASHBOARD_COURSE_STATISTICS, { courseId });
if (res.error) {
console.error("Error fetching statistics for course ID:", courseId, res.error);
}
return res.data?.course_statistics || null;
} catch (error) {
console.error(`Error fetching statistics for course ID: ${courseId}`, error);
return null;
}
};
export const fetchProgressData = async (
courseId: string
): Promise<CourseProgressType | null> => {
try {
const res = await graphqlClient.query(DASHBOARD_COURSE_SESSION_PROGRESS, {
courseId,
});
if (res.error) {
console.error("Error fetching progress for course ID:", courseId, res.error);
}
return res.data?.course_progress || null;
} catch (error) {
console.error(`Error fetching progress for course ID: ${courseId}`, error);
return null;
}
};
export const fetchDashboardConfig = async (): Promise<DashboardConfigType[] | null> => {
try {
const res = await graphqlClient.query(DASHBOARD_CONFIG, {});
if (res.error) {
console.error("Error fetching dashboard config:", res.error);
}
return (res.data?.dashboard_config as unknown as DashboardConfigType[]) || null;
} catch (error) {
console.error("Error fetching dashboard config:", error);
return null;
}
};
export const fetchMentorCompetenceSummary = async (
courseId: string
): Promise<AssignmentsStatisticsType | null> => {
try {
const res = await graphqlClient.query(DASHBOARD_MENTOR_COMPETENCE_SUMMARY, {
courseId,
});
if (res.error) {
console.error("Error fetching data for course ID:", courseId, res.error);
}
return res.data?.mentor_course_statistics?.assignments || null;
} catch (error) {
console.error(`Error fetching data for course ID: ${courseId}`, error);
return null;
}
};
export async function fetchDashboardPersons(mode: DashboardPersonsPageMode) {
let url = "/api/dashboard/persons/";
if (mode === "competenceMetrics") {
url += "?with_competence_metrics=true";
}
return await itGetCached<DashboardPersonType[]>(url);
}
export async function fetchDashboardDueDates() {
return await itGetCached<DashboardDueDate[]>("/api/dashboard/duedates/");
}
export async function fetchDashboardConfigv2() {
return await itGetCached<DashboardCourseConfigType[]>("/api/dashboard/config/");
}
export async function fetchMenteeCount(courseId: string) {
return await itGetCached<{ mentee_count: number }>(
`/api/dashboard/course/${courseId}/mentees/`
);
}
export async function fetchOpenTasksCount(courseId: string) {
return await itGetCached<{ open_task_count: number }>(
`/api/dashboard/course/${courseId}/open_tasks/`
);
}
export function courseIdForCourseSlug(
dashboardConfigs: DashboardCourseConfigType[],
courseSlug: string
) {
const config = dashboardConfigs.find((config) => config.course_slug === courseSlug);
return config?.course_id;
}