import { Module } from "@/interfaces"; import { Exam, Shuffles, UserSolution } from "@/interfaces/exam"; import { Assignment } from "@/interfaces/results"; import { create } from "zustand"; import { createJSONStorage, persist } from "zustand/middleware"; import { immer } from "zustand/middleware/immer"; export interface ExamState { exams: Exam[]; userSolutions: UserSolution[]; showSolutions: boolean; hasExamEnded: boolean; selectedModules: Module[]; assignment?: Assignment; timeSpent: number; sessionId: string; moduleIndex: number; exam?: Exam; partIndex: number; exerciseIndex: number; questionIndex: number; inactivity: number; shuffles: Shuffles[]; bgColor: string; currentSolution?: UserSolution | undefined; } export interface ExamFunctions { setExams: (exams: Exam[]) => void; setUserSolutions: (userSolutions: UserSolution[]) => void; setShowSolutions: (showSolutions: boolean) => void; setHasExamEnded: (hasExamEnded: boolean) => void; setSelectedModules: (modules: Module[]) => void; setAssignment: (assignment?: Assignment) => void; setTimeSpent: (timeSpent: number) => void; setSessionId: (sessionId: string) => void; setModuleIndex: (moduleIndex: number) => void; setExam: (exam?: Exam) => void; setPartIndex: (partIndex: number) => void; setExerciseIndex: (exerciseIndex: number) => void; setQuestionIndex: (questionIndex: number) => void; setInactivity: (inactivity: number) => void; setShuffles: (shuffles: Shuffles[]) => void; setBgColor: (bgColor: string) => void; setCurrentSolution: (currentSolution: UserSolution | undefined) => void; reset: () => void; } export const initialState: ExamState = { exams: [], userSolutions: [], showSolutions: false, selectedModules: [], hasExamEnded: false, assignment: undefined, timeSpent: 0, sessionId: "", exam: undefined, moduleIndex: 0, partIndex: -1, exerciseIndex: -1, questionIndex: 0, inactivity: 0, shuffles: [], bgColor: "bg-white", currentSolution: undefined }; const useExamStore = create((set) => ({ ...initialState, setUserSolutions: (userSolutions: UserSolution[]) => set(() => ({ userSolutions })), setExams: (exams: Exam[]) => set(() => ({ exams })), setShowSolutions: (showSolutions: boolean) => set(() => ({ showSolutions })), setSelectedModules: (modules: Module[]) => set(() => ({ selectedModules: modules })), setHasExamEnded: (hasExamEnded: boolean) => set(() => ({ hasExamEnded })), setAssignment: (assignment?: Assignment) => set(() => ({ assignment })), setTimeSpent: (timeSpent) => set(() => ({ timeSpent })), setSessionId: (sessionId: string) => set(() => ({ sessionId })), setExam: (exam?: Exam) => set(() => ({ exam })), setModuleIndex: (moduleIndex: number) => set(() => ({ moduleIndex })), setPartIndex: (partIndex: number) => set(() => ({ partIndex })), setExerciseIndex: (exerciseIndex: number) => set(() => ({ exerciseIndex })), setQuestionIndex: (questionIndex: number) => set(() => ({ questionIndex })), setInactivity: (inactivity: number) => set(() => ({ inactivity })), setShuffles: (shuffles: Shuffles[]) => set(() => ({ shuffles })), setBgColor: (bgColor) => set(() => ({ bgColor })), setCurrentSolution: (currentSolution: UserSolution | undefined) => set(() => ({ currentSolution })), reset: () => set(() => initialState), })); export default useExamStore; export const usePersistentExamStore = create()( persist( immer((set) => ({ ...initialState, setUserSolutions: (userSolutions: UserSolution[]) => set(() => ({ userSolutions })), setExams: (exams: Exam[]) => set(() => ({ exams })), setShowSolutions: (showSolutions: boolean) => set(() => ({ showSolutions })), setSelectedModules: (modules: Module[]) => set(() => ({ selectedModules: modules })), setHasExamEnded: (hasExamEnded: boolean) => set(() => ({ hasExamEnded })), setAssignment: (assignment?: Assignment) => set(() => ({ assignment })), setTimeSpent: (timeSpent) => set(() => ({ timeSpent })), setSessionId: (sessionId: string) => set(() => ({ sessionId })), setExam: (exam?: Exam) => set(() => ({ exam })), setModuleIndex: (moduleIndex: number) => set(() => ({ moduleIndex })), setPartIndex: (partIndex: number) => set(() => ({ partIndex })), setExerciseIndex: (exerciseIndex: number) => set(() => ({ exerciseIndex })), setQuestionIndex: (questionIndex: number) => set(() => ({ questionIndex })), setInactivity: (inactivity: number) => set(() => ({ inactivity })), setShuffles: (shuffles: Shuffles[]) => set(() => ({ shuffles })), setBgColor: (bgColor) => set(() => ({ bgColor })), setCurrentSolution: (currentSolution: UserSolution | undefined) => set(() => ({ currentSolution })), reset: () => set(() => initialState), })), { name: 'persistent-exam-store', storage: createJSONStorage(() => localStorage), partialize: (state) => ({ ...state }), } ) );