import { itGetCached } from "@/fetchHelpers"; import type { CourseSessionUser, ExpertSessionUser } from "@/types"; import log from "loglevel"; import { useUserStore } from "@/stores/user"; import { defineStore } from "pinia"; export type CockpitStoreState = { courseSessionUsers: CourseSessionUser[] | undefined; cockpitSessionUser: ExpertSessionUser | undefined; selectedCircles: string[]; }; export const useCockpitStore = defineStore({ id: "cockpit", state: () => { return { courseSessionUsers: undefined, cockpitSessionUser: undefined, selectedCircles: [], } as CockpitStoreState; }, actions: { async loadCourseSessionUsers(courseSessionId: number, reload = false) { log.debug("loadCockpitData called"); const users = (await itGetCached( `/api/course/sessions/${courseSessionId}/users/`, { reload: reload, } )) as CourseSessionUser[]; this.courseSessionUsers = users.filter((user) => user.role === "MEMBER"); const userStore = useUserStore(); const currentUser = users.find((user) => user.user_id === userStore.id); if (currentUser && currentUser.role === "EXPERT") { this.cockpitSessionUser = currentUser as ExpertSessionUser; } if (this.cockpitSessionUser && this.cockpitSessionUser.circles?.length > 0) { this.selectedCircles = [this.cockpitSessionUser.circles[0].translation_key]; } if (!this.courseSessionUsers) { throw `No courseSessionUsers data found for user`; } return this.courseSessionUsers; }, toggleCircleSelection(translationKey: string) { if (this.selectedCircles.indexOf(translationKey) < 0) { this.selectedCircles.push(translationKey); } else { if (this.selectedCircles.length === 1) { return; } const index = this.selectedCircles.indexOf(translationKey); this.selectedCircles.splice(index, 1); } }, }, });