import { defineStore } from "pinia"; import { StudenthubAnmeldung, StudenthubStudent } from "../types"; const STUDENTHUB_STUDENT_DATA = "studenthubStudentData"; const STUDENTHUB_APPLICATIONS_DATA = "studenthubApplicationsData"; export const useStudenthubStore = defineStore("studenthub", { state: () => { return { student: null as StudenthubStudent | null, applications: null as StudenthubAnmeldung[] | null, }; }, getters: { hasSomeStudenthubData(): boolean { return ( (this.student != null && this.student.studierendeId != null) || (this.applications != null && this.applications?.length != null) ); }, hasAllStudenthubData(): boolean { return ( this.student != null && this.student.studierendeId != null && this.applications != null && this.applications?.length != null ); }, passedModuleIds(): number[] { if (this.applications === null) return []; return this.applications .filter((a) => a.bestanden && a.modulanlass?.nummer.endsWith(".HN")) .map((a) => a.modulanlass?.modulId ?? -1); }, creditedModuleIds(): number[] { if (this.student === null) return []; return this.student.modulAnrechnungen?.map((m) => m.modulId) ?? []; }, completedModuleIds(): number[] { return [...this.passedModuleIds, ...this.creditedModuleIds]; }, activeModuleIds(): number[] { if (this.applications === null) return []; return this.applications .filter( (a) => !a.bestanden && a.modulanlass && a.statusName == "aA.Angemeldet" && a.modulanlass.nummer.endsWith(".HN"), ) .map((a) => a.modulanlass?.modulId ?? -1); }, failedModuleIds(): number[] { if (this.applications === null) return []; return this.applications .filter( (a) => !a.bestanden && a.modulanlass && a.statusName == "aA.Nicht erfolgreich teilgenommen" && a.modulanlass.nummer.endsWith(".HN"), ) .map((a) => a.modulanlass?.modulId ?? -1); }, moduleAttemptCount(): Record { if (this.applications === null) return []; const attempts = {} as Record; this.applications .filter((a) => a.modulanlass && a.modulanlass.nummer.endsWith(".HN")) .forEach((a) => { const mid = a.modulanlass?.modulId; if (!mid) return; if (mid in attempts) attempts[mid]++; else attempts[mid] = 1; }); return attempts; }, }, actions: { loadData() { try { this.student = JSON.parse(localStorage[STUDENTHUB_STUDENT_DATA]); } catch (e) { console.info("Failed to parse the studenthub student data!"); console.error(e); } try { this.applications = JSON.parse( localStorage[STUDENTHUB_APPLICATIONS_DATA], ); } catch (e) { console.info("Failed to parse the studenthub applications data!"); console.error(e); } }, setStudentData(stud: StudenthubStudent | null) { if (stud === null) { localStorage[STUDENTHUB_STUDENT_DATA] = null; this.student = null; return; } localStorage[STUDENTHUB_STUDENT_DATA] = JSON.stringify(stud); this.student = stud; }, setApplicationsData(application: StudenthubAnmeldung[] | null) { if (application === null) { localStorage[STUDENTHUB_APPLICATIONS_DATA] = null; this.applications = null; return; } localStorage[STUDENTHUB_APPLICATIONS_DATA] = JSON.stringify(application); this.applications = application; }, hasCompletedModule(moduleID: number | null) { if (moduleID === null) return false; return this.completedModuleIds.find((m) => m == moduleID) !== undefined; }, }, });