From b9c55f6d75872cf87be291f099e0030bf047aaf1 Mon Sep 17 00:00:00 2001 From: Akash Date: Sat, 4 May 2024 22:32:07 +0530 Subject: [PATCH] Closes #4 Added 10 QuizTimer to all quizzes Signed-off-by: Akash --- app/[quizid]/page.tsx | 35 +++++++++++------ app/studentdata/page.test.tsx | 73 +++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 11 deletions(-) create mode 100644 app/studentdata/page.test.tsx diff --git a/app/[quizid]/page.tsx b/app/[quizid]/page.tsx index f5e5d3a..367eb39 100644 --- a/app/[quizid]/page.tsx +++ b/app/[quizid]/page.tsx @@ -20,8 +20,8 @@ type QuizQuestion = { function Quiz() { + const quizTime = 600000; // This can be updated to allow for custom quiz times - const router = useRouter(); const SearchParams = useSearchParams(); const quizId : any = SearchParams.get('id') @@ -35,6 +35,7 @@ function Quiz() { const [score, setScore] = useState(0); const [isFinalQuestion, setIsFinalQuestion] = useState(false); const [isTimeUp, setIsTimeUp] = useState(false); + const [timeLeft, setTimeLeft] = useState(quizTime/1000); // Time limit in seconds async function fetchQuizData() { @@ -59,20 +60,28 @@ function Quiz() { } - useEffect(() => { - const timer = setTimeout(() => { - setIsTimeUp(true); - }, 3000); // Set the quiz time limit here. 60000ms = 1 minute - - // Clear the timer when the component unmounts - return () => clearTimeout(timer); - }, []); + // Update the useEffect hook where the timer is set +useEffect(() => { + const timer = setTimeout(() => { + setIsTimeUp(true); + }, quizTime); + + const interval = setInterval(() => { + setTimeLeft((prevTimeLeft) => prevTimeLeft - 1); + }, 1000); // Decrease timeLeft by 1 every second + + // Clear the timer and the interval when the component unmounts or when the time is up + return () => { + clearTimeout(timer); + clearInterval(interval); + }; +}, []); useEffect(() => { if (isTimeUp) { console.log('Time is up!'); toast.error('Time is up! Auto Submitting Quiz'); - // handleQuizSubmit(); + handleQuizSubmit(); } }, [isTimeUp]); @@ -138,7 +147,7 @@ function Quiz() { //check if all questions are attempted - if (selectedOptions.length !== correctAnswers.length){ + if (selectedOptions.length !== correctAnswers.length && !isTimeUp){ toast.error("Please attempt all of the questions") return } @@ -188,6 +197,7 @@ function Quiz() { return (
+ {currentQuestion ? (

Question {currentQuestionIndex + 1}:

@@ -216,6 +226,9 @@ function Quiz() { Previous )} +
+ Time left: {timeLeft} seconds +
{isFinalQuestion ? (