Updated the eval calls to the backend, passed the navigation logic of level to useExamNavigation hook

This commit is contained in:
Carlos-Mesquita
2024-11-26 09:04:38 +00:00
parent bb5326a331
commit 2ed4e6509e
14 changed files with 452 additions and 493 deletions

View File

@@ -26,7 +26,7 @@ export const initialState: ExamState = {
inactivity: 0,
shuffles: [],
bgColor: "bg-white",
currentSolution: undefined,
evaluated: [],
user: undefined,
navigation: {
previousDisabled: false,
@@ -39,6 +39,7 @@ export const initialState: ExamState = {
reviewAll: false,
finalizeModule: false,
finalizeExam: false,
pendingEvaluation: false,
},
};
@@ -61,6 +62,8 @@ const useExamStore = create<ExamState & ExamFunctions>((set, get) => ({
setQuestionIndex: (questionIndex: number) => set(() => ({ questionIndex })),
setBgColor: (bgColor: string) => set(() => ({ bgColor })),
setEvaluated: (evaluated: UserSolution[]) => set(() => ({ evaluated })),
setNavigation: (updates: Partial<Navigation>) => set((state) => ({
navigation: {
...state.navigation,
@@ -75,7 +78,6 @@ const useExamStore = create<ExamState & ExamFunctions>((set, get) => ({
}
})),
setTimeIsUp: (timeIsUp: boolean) => set((state) => ({ flags: { ...state.flags, timeIsUp } })),
reset: () => set(() => initialState),
@@ -162,12 +164,12 @@ export const usePersistentExamStore = create<ExamState & ExamFunctions>()(
setTimeIsUp: (timeIsUp: boolean) => set((state) => ({ flags: { ...state.flags, timeIsUp } })),
saveStats: async () => {},
saveSession: async () => {},
saveStats: async () => { },
saveSession: async () => { },
setEvaluated: (evaluated: UserSolution[]) => {},
reset: () => set(() => initialState),
dispatch: (action) => set((state) => rootReducer(state, action))
})),
{
name: 'persistent-exam-store',

View File

@@ -11,9 +11,10 @@ import { convertToUserSolutions } from "@/utils/stats";
export type RootActions =
{ type: 'INIT_EXAM'; payload: { exams: Exam[], modules: Module[], assignment?: Assignment } } |
{ type: 'INIT_SOLUTIONS'; payload: { exams: Exam[], modules: Module[], stats: Stat[], timeSpent?: number, inactivity?: number } } |
{ type: 'UPDATE_TIMERS'; payload: { timeSpent: number; inactivity: number; timeSpentCurrentModule: number;} } |
{ type: 'UPDATE_TIMERS'; payload: { timeSpent: number; inactivity: number; timeSpentCurrentModule: number; } } |
{ type: 'FINALIZE_MODULE'; payload: { updateTimers: boolean } } |
{ type: 'FINALIZE_MODULE_SOLUTIONS' }
{ type: 'FINALIZE_MODULE_SOLUTIONS' } |
{ type: 'UPDATE_EXAMS'}
export type Action = RootActions | SessionActions;
@@ -84,19 +85,28 @@ export const rootReducer = (
case 'UPDATE_TIMERS': {
// Just assigning the timers at once instead of two different calls
const { timeSpent, inactivity, timeSpentCurrentModule } = action.payload;
return {
return {
timeSpentCurrentModule,
timeSpent,
inactivity
timeSpent,
inactivity
}
};
case 'FINALIZE_MODULE': {
const { updateTimers } = action.payload;
const solutions = state.userSolutions;
const evaluated = state.evaluated;
const hasUnevaluatedSolutions = solutions.some(solution =>
(solution.type === 'speaking' ||
solution.type === 'writing' ||
solution.type === 'interactiveSpeaking') &&
!evaluated.some(evaluation => evaluation.exercise === solution.exercise)
);
// To finalize a module first flag the timers to be updated
if (updateTimers) {
return {
flags: { ...state.flags, finalizeModule: true }
flags: { ...state.flags, finalizeModule: true, pendingEvaluation: hasUnevaluatedSolutions }
}
} else {
// then check whether there are more modules in the exam, if there are
@@ -119,10 +129,12 @@ export const rootReducer = (
// and the Finish view is set there, no need to
// dispatch another init
return {
showSolutions: true,
flags: {
...state.flags,
finalizeModule: false,
finalizeExam: true,
pendingEvaluation: hasUnevaluatedSolutions,
}
}
}
@@ -155,6 +167,14 @@ export const rootReducer = (
}
}
}
case 'UPDATE_EXAMS': {
const exams = state.exams.map((e) => updateExamWithUserSolutions(e, state.userSolutions));
const exam = updateExamWithUserSolutions(state.exam!, state.userSolutions);
return {
exams,
exam
}
}
default:
return {};
}

View File

@@ -16,6 +16,7 @@ export interface StateFlags {
reviewAll: boolean;
finalizeModule: boolean;
finalizeExam: boolean;
pendingEvaluation: boolean;
}
export interface ExamState {
@@ -39,6 +40,7 @@ export interface ExamState {
currentSolution?: UserSolution | undefined;
navigation: Navigation;
flags: StateFlags,
evaluated: UserSolution[];
}
@@ -63,6 +65,8 @@ export interface ExamFunctions {
setTimeIsUp: (timeIsUp: boolean) => void;
setEvaluated: (evaluated: UserSolution[]) => void,
saveSession: () => Promise<void>;
saveStats: () => Promise<void>;