From c2b6be4425db12b6b38bedaf84c2581564027da0 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Sun, 24 Mar 2024 12:22:52 +0000 Subject: [PATCH] Solved the solution duplication bug --- src/pages/(exam)/ExamPage.tsx | 48 +++++++++++++---------------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/src/pages/(exam)/ExamPage.tsx b/src/pages/(exam)/ExamPage.tsx index 46acd8af..34359ae3 100644 --- a/src/pages/(exam)/ExamPage.tsx +++ b/src/pages/(exam)/ExamPage.tsx @@ -276,47 +276,35 @@ export default function ExamPage({page}: Props) { const solutionIds = solutions.map((x) => x.exercise); const solutionExams = solutions.map((x) => x.exam); + let newSolutions = [...solutions]; + if (exam && !solutionExams.includes(exam.id)) return; if (exam && (exam.module === "writing" || exam.module === "speaking") && solutions.length > 0 && !showSolutions) { setHasBeenUploaded(true); setIsEvaluationLoading(true); - Promise.all( - exam.exercises.map(async (exercise) => { - const evaluationID = uuidv4(); - if (exercise.type === "writing") - return await evaluateWritingAnswer(exercise, solutions.find((x) => x.exercise === exercise.id)!, evaluationID); + const responses: UserSolution[] = ( + await Promise.all( + exam.exercises.map(async (exercise) => { + const evaluationID = uuidv4(); + if (exercise.type === "writing") + return await evaluateWritingAnswer(exercise, solutions.find((x) => x.exercise === exercise.id)!, evaluationID); - if (exercise.type === "interactiveSpeaking" || exercise.type === "speaking") - return await evaluateSpeakingAnswer(exercise, solutions.find((x) => x.exercise === exercise.id)!, evaluationID); - }), - ) - .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)]); - }) - .finally(() => { - setHasBeenUploaded(false); - }); + if (exercise.type === "interactiveSpeaking" || exercise.type === "speaking") + return await evaluateSpeakingAnswer(exercise, solutions.find((x) => x.exercise === exercise.id)!, evaluationID); + }), + ) + ).filter((x) => !!x) as UserSolution[]; + + newSolutions = [...newSolutions.filter((x) => !responses.map((y) => y.exercise).includes(x.exercise)), ...responses]; + setStatsAwaitingEvaluation((prev) => [...prev, ...responses.filter((x) => !!x).map((r) => (r as any).id)]); + setHasBeenUploaded(false); } axios.get("/api/stats/update"); - setUserSolutions([...userSolutions.filter((x) => !solutionIds.includes(x.exercise)), ...solutions]); + setUserSolutions([...userSolutions.filter((x) => !solutionIds.includes(x.exercise)), ...newSolutions]); setModuleIndex(moduleIndex + 1); setPartIndex(-1);