From 65e33388592651149bbf732cb7145f7386c1adb3 Mon Sep 17 00:00:00 2001 From: shafin-r Date: Sun, 31 Aug 2025 17:45:08 +0600 Subject: [PATCH] fix(api): exam logic yet to be fixed --- app/exam/[id]/page.tsx | 10 ++-------- components/Header.tsx | 4 ++-- context/TimerContext.tsx | 19 ++++++++++++------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/app/exam/[id]/page.tsx b/app/exam/[id]/page.tsx index 5f86983..912f3ee 100644 --- a/app/exam/[id]/page.tsx +++ b/app/exam/[id]/page.tsx @@ -16,7 +16,7 @@ export default function ExamPage() { const test_id = searchParams.get("test_id") || ""; const type = searchParams.get("type") || ""; - const { isOpen, close, open } = useModal(); + // const { isOpen, close, open } = useModal(); const { timeRemaining, setInitialTime, stopTimer } = useTimer(); const { test, answers, startExam, setAnswer, submitExam, cancelExam } = useExam(); @@ -36,7 +36,7 @@ export default function ExamPage() { if (window.confirm("Are you sure you want to quit the exam?")) { stopTimer(); cancelExam(); - router.push("/unit"); + router.push(`/categories/${type}s`); } }, [stopTimer, cancelExam, router]); @@ -142,12 +142,6 @@ export default function ExamPage() { > Submit - diff --git a/components/Header.tsx b/components/Header.tsx index 4574c7b..38c83e9 100644 --- a/components/Header.tsx +++ b/components/Header.tsx @@ -51,11 +51,11 @@ const Header = ({
- {user?.username ? user.username.charAt(0).toUpperCase() : ""} + {user?.username ? user.username.charAt(0).toUpperCase() : "U"} - Hello, {user?.username ? user.username.split(" ")[0] : ""} + Hello, {user?.username ? user.username.split(" ")[0] : "User"}
)} diff --git a/context/TimerContext.tsx b/context/TimerContext.tsx index b019c45..43d8563 100644 --- a/context/TimerContext.tsx +++ b/context/TimerContext.tsx @@ -23,7 +23,7 @@ export const TimerProvider: React.FC<{ children: React.ReactNode }> = ({ const [timeRemaining, setTimeRemaining] = useState(0); const timerRef = useRef(null); - // countdown effect + // Effect: run interval whenever timeRemaining is set > 0 useEffect(() => { if (timeRemaining > 0 && !timerRef.current) { timerRef.current = setInterval(() => { @@ -39,16 +39,18 @@ export const TimerProvider: React.FC<{ children: React.ReactNode }> = ({ } return () => { - if (timerRef.current) { + if (timerRef.current && timeRemaining <= 0) { clearInterval(timerRef.current); timerRef.current = null; } }; - }, [timeRemaining]); + }, [timeRemaining]); // 👈 depend on timeRemaining const resetTimer = (duration: number) => { - if (timerRef.current) clearInterval(timerRef.current); - timerRef.current = null; + if (timerRef.current) { + clearInterval(timerRef.current); + timerRef.current = null; + } setTimeRemaining(duration); }; @@ -57,11 +59,14 @@ export const TimerProvider: React.FC<{ children: React.ReactNode }> = ({ clearInterval(timerRef.current); timerRef.current = null; } + setTimeRemaining(0); }; const setInitialTime = (duration: number) => { - if (timerRef.current) clearInterval(timerRef.current); - timerRef.current = null; + if (timerRef.current) { + clearInterval(timerRef.current); + timerRef.current = null; + } setTimeRemaining(duration); };