"use client"; import React, { useEffect, useCallback } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import Header from "@/components/Header"; import QuestionItem from "@/components/QuestionItem"; import BackgroundWrapper from "@/components/BackgroundWrapper"; import { useExamStore } from "@/stores/examStore"; import { useTimerStore } from "@/stores/timerStore"; export default function ExamPage() { const router = useRouter(); const searchParams = useSearchParams(); const test_id = searchParams.get("test_id") || ""; const type = searchParams.get("type") || ""; const { test, answers, startExam, setAnswer, submitExam, cancelExam } = useExamStore(); const { resetTimer, stopTimer } = useTimerStore(); // Start exam + timer automatically useEffect(() => { if (type && test_id) { startExam(type, test_id).then((fetchedTest) => { if (fetchedTest?.metadata.time_limit_minutes) { resetTimer(fetchedTest.metadata.time_limit_minutes * 60, () => { // Timer ended → auto-submit exam submitExam(type); router.push(`/categories/${type}s`); alert("Time's up! Your exam has been submitted."); }); } }); } }, [type, test_id, startExam, resetTimer, submitExam, router]); const showExitDialog = useCallback(() => { if (window.confirm("Are you sure you want to quit the exam?")) { stopTimer(); cancelExam(); router.push(`/categories/${type}s`); } }, [stopTimer, cancelExam, router, type]); if (!test) { return (
Loading exam...