From 65fe1ec8edef559b7a8db62d92a9a7de3848cfc3 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Tue, 6 Feb 2024 15:51:55 +0000 Subject: [PATCH] Made it so it currently is possible to save the progress on the Writing exercise as well --- src/components/Exercises/Writing.tsx | 22 ++++++++++++++++++++++ src/pages/(exam)/ExamPage.tsx | 3 +++ 2 files changed, 25 insertions(+) diff --git a/src/components/Exercises/Writing.tsx b/src/components/Exercises/Writing.tsx index 58366a5c..6004a198 100644 --- a/src/components/Exercises/Writing.tsx +++ b/src/components/Exercises/Writing.tsx @@ -22,9 +22,31 @@ export default function Writing({ const [isModalOpen, setIsModalOpen] = useState(false); const [inputText, setInputText] = useState(userSolutions.length === 1 ? userSolutions[0].solution : ""); const [isSubmitEnabled, setIsSubmitEnabled] = useState(false); + const [saveTimer, setSaveTimer] = useState(0); + const {userSolutions: storeUserSolutions, setUserSolutions} = useExamStore((state) => state); const hasExamEnded = useExamStore((state) => state.hasExamEnded); + useEffect(() => { + const saveTimerInterval = setInterval(() => { + setSaveTimer((prev) => prev + 1); + }, 1000); + + return () => { + clearInterval(saveTimerInterval); + }; + }, []); + + useEffect(() => { + if (inputText.length > 0 && saveTimer % 10 === 0) { + setUserSolutions([ + ...storeUserSolutions.filter((x) => x.exercise !== id), + {exercise: id, solutions: [{id, solution: inputText}], score: {correct: 1, total: 1, missing: 0}, type}, + ]); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [saveTimer]); + useEffect(() => { if (localStorage.getItem("enable_paste")) return; diff --git a/src/pages/(exam)/ExamPage.tsx b/src/pages/(exam)/ExamPage.tsx index 4eb8dd50..fd411913 100644 --- a/src/pages/(exam)/ExamPage.tsx +++ b/src/pages/(exam)/ExamPage.tsx @@ -68,6 +68,8 @@ export default function ExamPage({page}: Props) { // eslint-disable-next-line react-hooks/exhaustive-deps const saveSession = async () => { + console.log("Saving your session..."); + await axios.post("/api/sessions", { id: sessionId, sessionId, @@ -139,6 +141,7 @@ export default function ExamPage({page}: Props) { const nextExam = exams[moduleIndex]; if (partIndex === -1 && nextExam.module !== "listening") setPartIndex(0); + if (exerciseIndex === -1 && !["reading", "listening"].includes(nextExam.module)) setExerciseIndex(0); setExam(nextExam ? updateExamWithUserSolutions(nextExam) : undefined); } })();