diff --git a/app/exam/pretest/page.tsx b/app/exam/pretest/page.tsx index 3c34489..6e7d599 100644 --- a/app/exam/pretest/page.tsx +++ b/app/exam/pretest/page.tsx @@ -2,26 +2,26 @@ import { useRouter, useSearchParams } from "next/navigation"; import { Suspense, useEffect, useState } from "react"; -import { ArrowLeft, HelpCircle, Clock, XCircle, OctagonX } from "lucide-react"; +import { + ArrowLeft, + HelpCircle, + Clock, + XCircle, + OctagonX, + RotateCw, +} from "lucide-react"; import DestructibleAlert from "@/components/DestructibleAlert"; import BackgroundWrapper from "@/components/BackgroundWrapper"; import { API_URL, getToken } from "@/lib/auth"; import { useExam } from "@/context/ExamContext"; -import { Test } from "@/types/exam"; -import { Metadata, MockMeta, SubjectMeta, TopicMeta } from "@/types/exam"; - -type MetadataType = "mock" | "subject" | "topic"; - -type MetadataMap = { - mock: MockMeta; - subject: SubjectMeta; - topic: TopicMeta; -}; +import { Question } from "@/types/exam"; +import { Metadata } from "@/types/exam"; function PretestPageContent() { const router = useRouter(); const searchParams = useSearchParams(); const { startExam, setCurrentExam } = useExam(); + const [examData, setExamData] = useState(); // Get params from URL search params const id = searchParams.get("test_id") || ""; @@ -31,19 +31,10 @@ function PretestPageContent() { ? typeParam : null; - const [metadata, setMetadata] = useState( - null - ); + const [metadata, setMetadata] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(); - function fetchMetadata( - type: T, - data: unknown - ): MetadataMap[T] { - return data as MetadataMap[T]; // you'd validate in real code - } - useEffect(() => { async function fetchQuestions() { if (!id || !type) return; @@ -63,9 +54,11 @@ function PretestPageContent() { } const data = await questionResponse.json(); - const fetchedMetadata = fetchMetadata(type, data.metadata); + const fetchedMetadata: Metadata = data.metadata; + const fetchedQuestions: Question[] = data.questions; setMetadata(fetchedMetadata); + setExamData(fetchedQuestions); } catch (error) { console.error(error); setError(error instanceof Error ? error.message : "An error occurred"); @@ -80,19 +73,25 @@ function PretestPageContent() { if (error) { return ( -
-
- -
- } - /> +
+
+ } + /> +
+ +
@@ -121,13 +120,13 @@ function PretestPageContent() { ); } - // function handleStartExam() { - // if (!examData) return; + function handleStartExam() { + if (!examData) return; - // setCurrentExam(examData); - // startExam(examData); - // router.push(`/exam/${id}?time=${metadata?.metadata.duration}`); - // } + setCurrentExam(examData); + startExam(examData); + router.push(`/exam/${id}?test_id=${metadata?.test_id}`); + } return (
@@ -226,7 +225,7 @@ function PretestPageContent() {