vbv/client/src/stores/dashboard.ts

74 lines
2.2 KiB
TypeScript

import type {
CourseProgressType,
CourseStatisticsType,
DashboardConfigType,
} from "@/gql/graphql";
import type { DashboardCourseConfigType } from "@/services/dashboard";
import {
fetchDashboardConfig,
fetchDashboardConfigv2,
fetchStatisticData,
} from "@/services/dashboard";
import { defineStore } from "pinia";
import type { Ref } from "vue";
import { ref } from "vue";
export const useDashboardStore = defineStore("dashboard", () => {
const dashboardConfigs: Ref<DashboardConfigType[]> = ref([]);
const dashboardConfigsv2: Ref<DashboardCourseConfigType[]> = ref([]);
const currentDashboardConfig: Ref<DashboardConfigType | undefined> = ref();
const dashBoardDataCache: Record<
string,
CourseStatisticsType | CourseProgressType | null
> = {};
const currentDashBoardData: Ref<CourseStatisticsType | CourseProgressType | null> =
ref(null);
const loading = ref(false);
const switchAndLoadDashboardConfig = async (config: DashboardConfigType) => {
currentDashboardConfig.value = config;
await loadDashboardDetails();
};
const loadDashboardConfig = async () => {
if (dashboardConfigs.value.length > 0) return;
const configData = await fetchDashboardConfig();
if (configData && configData.length > 0) {
dashboardConfigs.value = configData;
await switchAndLoadDashboardConfig(configData[0]);
}
};
const loadDashboardDetails = async () => {
loading.value = true;
dashboardConfigsv2.value = await fetchDashboardConfigv2();
console.log("got dashboard config v2: ", dashboardConfigsv2.value);
loading.value = false;
};
const loadStatisticsData = async (id: string) => {
const data = await fetchStatisticData(id);
dashBoardDataCache[id] = data;
currentDashBoardData.value = data;
};
const loadStatisticsDatav2 = async (id: string) => {
const data = await fetchStatisticData(id);
dashBoardDataCache[id] = data;
return data;
};
return {
dashboardConfigs,
dashboardConfigsv2,
currentDashboardConfig,
switchAndLoadDashboardConfig,
loadDashboardConfig,
loadDashboardDetails,
currentDashBoardData,
loading,
loadStatisticsData,
loadStatisticsDatav2,
};
});