diff --git a/src/interfaces/user.ts b/src/interfaces/user.ts index 15040ef1..1b3d783c 100644 --- a/src/interfaces/user.ts +++ b/src/interfaces/user.ts @@ -45,6 +45,7 @@ export interface Stat { module: Module; solutions: any[]; type: string; + timeSpent?: number; score: { correct: number; total: number; diff --git a/src/pages/exam.tsx b/src/pages/exam.tsx index 3a860a16..9b73d366 100644 --- a/src/pages/exam.tsx +++ b/src/pages/exam.tsx @@ -74,6 +74,7 @@ export default function Page() { const [isEvaluationLoading, setIsEvaluationLoading] = useState(false); const [showAbandonPopup, setShowAbandonPopup] = useState(false); const [avoidRepeated, setAvoidRepeated] = useState(false); + const [timeSpent, setTimeSpent] = useState(0); const [exams, setExams] = useExamStore((state) => [state.exams, state.setExams]); const [userSolutions, setUserSolutions] = useExamStore((state) => [state.userSolutions, state.setUserSolutions]); @@ -85,6 +86,20 @@ export default function Page() { useEffect(() => setSessionId(uuidv4()), []); + useEffect(() => { + selectedModules.length > 0 && timeSpent === 0 && !showSolutions; + if (selectedModules.length > 0 && timeSpent === 0 && !showSolutions) { + const timerInterval = setInterval(() => { + setTimeSpent((prev) => prev + 1); + }, 1000); + + return () => { + clearInterval(timerInterval); + }; + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [selectedModules.length]); + useEffect(() => { (async () => { if (selectedModules.length > 0 && exams.length > 0 && moduleIndex < selectedModules.length) { @@ -116,6 +131,7 @@ export default function Page() { if (selectedModules.length > 0 && exams.length !== 0 && moduleIndex >= selectedModules.length && !hasBeenUploaded && !showSolutions) { const newStats: Stat[] = userSolutions.map((solution) => ({ ...solution, + timeSpent, session: sessionId, exam: solution.exam!, module: solution.module!, diff --git a/src/pages/exercises.tsx b/src/pages/exercises.tsx index e71ea07c..61004268 100644 --- a/src/pages/exercises.tsx +++ b/src/pages/exercises.tsx @@ -77,6 +77,7 @@ export default function Page() { const [isEvaluationLoading, setIsEvaluationLoading] = useState(false); const [showAbandonPopup, setShowAbandonPopup] = useState(false); const [avoidRepeated, setAvoidRepeated] = useState(false); + const [timeSpent, setTimeSpent] = useState(0); const [exams, setExams] = useExamStore((state) => [state.exams, state.setExams]); const [userSolutions, setUserSolutions] = useExamStore((state) => [state.userSolutions, state.setUserSolutions]); @@ -89,6 +90,19 @@ export default function Page() { useEffect(() => console.log({examId: exam?.id, exam}), [exam]); useEffect(() => setSessionId(uuidv4()), []); + useEffect(() => { + if (selectedModules.length > 0 && timeSpent === 0 && !showSolutions) { + const timerInterval = setInterval(() => { + setTimeSpent((prev) => prev + 1); + }, 1000); + + return () => { + clearInterval(timerInterval); + }; + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [selectedModules.length]); + useEffect(() => { (async () => { if (selectedModules.length > 0 && exams.length > 0 && moduleIndex < selectedModules.length) { @@ -120,6 +134,7 @@ export default function Page() { if (selectedModules.length > 0 && exams.length !== 0 && moduleIndex >= selectedModules.length && !hasBeenUploaded && !showSolutions) { const newStats: Stat[] = userSolutions.map((solution) => ({ ...solution, + timeSpent, session: sessionId, exam: solution.exam!, module: solution.module!, diff --git a/src/pages/record.tsx b/src/pages/record.tsx index 4f4c5868..3af61a2c 100644 --- a/src/pages/record.tsx +++ b/src/pages/record.tsx @@ -153,6 +153,8 @@ export default function History({user}: {user: User}) { level: calculateBandScore(x.correct, x.total, x.module, user.focus), })); + const timeSpent = dateStats[0].timeSpent; + const selectExam = () => { const examPromises = uniqBy(dateStats, "exam").map((stat) => getExamById(stat.module, stat.exam)); @@ -184,7 +186,14 @@ export default function History({user}: {user: User}) { onClick={selectExam} role="button">