Prevented the bug where the application is crashing

This commit is contained in:
Tiago Ribeiro
2024-03-24 02:32:12 +00:00
parent 29b2c8b3b8
commit 22f2b43692
8 changed files with 86 additions and 68 deletions

View File

@@ -41,6 +41,8 @@ export default function ExamPage({page}: Props) {
const assignment = useExamStore((state) => state.assignment);
const initialTimeSpent = useExamStore((state) => state.timeSpent);
const examStore = useExamStore;
const {exam, setExam} = useExamStore((state) => state);
const {exams, setExams} = useExamStore((state) => state);
const {sessionId, setSessionId} = useExamStore((state) => state);
@@ -189,8 +191,8 @@ export default function ExamPage({page}: Props) {
id: solution.id || uuidv4(),
timeSpent,
session: sessionId,
exam: exam!.id,
module: exam!.module,
exam: solution.exam!,
module: solution.module!,
user: user?.id || "",
date: new Date().getTime(),
isDisabled: solution.isDisabled,
@@ -218,29 +220,33 @@ export default function ExamPage({page}: Props) {
const checkIfStatsHaveBeenEvaluated = (ids: string[]) => {
setTimeout(async () => {
const awaitedStats = await Promise.all(ids.map(async (id) => (await axios.get<Stat>(`/api/stats/${id}`)).data));
const solutionsEvaluated = awaitedStats.every((stat) => stat.solutions.every((x) => x.evaluation !== null));
if (solutionsEvaluated) {
const statsUserSolutions: UserSolution[] = awaitedStats.map((stat) => ({
id: stat.id,
exercise: stat.exercise,
score: stat.score,
solutions: stat.solutions,
type: stat.type,
exam: stat.exam,
module: stat.module,
}));
try {
const awaitedStats = await Promise.all(ids.map(async (id) => (await axios.get<Stat>(`/api/stats/${id}`)).data));
const solutionsEvaluated = awaitedStats.every((stat) => stat.solutions.every((x) => x.evaluation !== null));
if (solutionsEvaluated) {
const statsUserSolutions: UserSolution[] = awaitedStats.map((stat) => ({
id: stat.id,
exercise: stat.exercise,
score: stat.score,
solutions: stat.solutions,
type: stat.type,
exam: stat.exam,
module: stat.module,
}));
const updatedUserSolutions = userSolutions.map((x) => {
const respectiveSolution = statsUserSolutions.find((y) => y.exercise === x.exercise);
return respectiveSolution ? respectiveSolution : x;
});
const updatedUserSolutions = userSolutions.map((x) => {
const respectiveSolution = statsUserSolutions.find((y) => y.exercise === x.exercise);
return respectiveSolution ? respectiveSolution : x;
});
setUserSolutions(updatedUserSolutions);
return setStatsAwaitingEvaluation((prev) => prev.filter((x) => !ids.includes(x)));
setUserSolutions(updatedUserSolutions);
return setStatsAwaitingEvaluation((prev) => prev.filter((x) => !ids.includes(x)));
}
return checkIfStatsHaveBeenEvaluated(ids);
} catch {
return checkIfStatsHaveBeenEvaluated(ids);
}
return checkIfStatsHaveBeenEvaluated(ids);
}, 5 * 1000);
};
@@ -276,7 +282,7 @@ export default function ExamPage({page}: Props) {
setHasBeenUploaded(true);
setIsEvaluationLoading(true);
await Promise.all(
Promise.all(
exam.exercises.map(async (exercise) => {
const evaluationID = uuidv4();
if (exercise.type === "writing")
@@ -287,8 +293,21 @@ export default function ExamPage({page}: Props) {
}),
)
.then((responses) => {
examStore.setState((prev) => {
return {
userSolutions: [
...prev.userSolutions.filter(
(x) =>
!responses
.filter((r) => !!r)
.map((r) => r!.id)
.includes(x.id),
),
...responses.filter((x) => !!x),
] as any,
};
});
setStatsAwaitingEvaluation((prev) => [...prev, ...responses.filter((x) => !!x).map((r) => (r as any).id)]);
setUserSolutions([...userSolutions.filter((x) => !solutionIds.includes(x.exercise)), ...responses.filter((x) => !!x)] as any);
})
.finally(() => {
setHasBeenUploaded(false);
@@ -297,8 +316,7 @@ export default function ExamPage({page}: Props) {
axios.get("/api/stats/update");
if (exam && exam.module !== "writing" && exam.module !== "speaking")
setUserSolutions([...userSolutions.filter((x) => !solutionIds.includes(x.exercise)), ...solutions]);
setUserSolutions([...userSolutions.filter((x) => !solutionIds.includes(x.exercise)), ...solutions]);
setModuleIndex(moduleIndex + 1);
setPartIndex(-1);
@@ -306,7 +324,7 @@ export default function ExamPage({page}: Props) {
setQuestionIndex(0);
};
const aggregateScoresByModule = (answers: UserSolution[]): {module: Module; total: number; missing: number; correct: number}[] => {
const aggregateScoresByModule = (): {module: Module; total: number; missing: number; correct: number}[] => {
const scores: {
[key in Module]: {total: number; missing: number; correct: number};
} = {
@@ -337,7 +355,7 @@ export default function ExamPage({page}: Props) {
},
};
answers.forEach((x) => {
userSolutions.forEach((x) => {
const examModule =
x.module || (x.type === "writing" ? "writing" : x.type === "speaking" || x.type === "interactiveSpeaking" ? "speaking" : undefined);
@@ -383,7 +401,7 @@ export default function ExamPage({page}: Props) {
setPartIndex(exams[0].module === "listening" ? -1 : 0);
setExam(exams[0]);
}}
scores={aggregateScoresByModule(userSolutions)}
scores={aggregateScoresByModule()}
/>
);
}