"use client"; import { useRouter, useSearchParams } from "next/navigation"; import { Suspense, useEffect, useState } from "react"; import { ArrowLeft, HelpCircle, Clock, XCircle, OctagonX } 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; }; function PretestPageContent() { const router = useRouter(); const searchParams = useSearchParams(); const { startExam, setCurrentExam } = useExam(); // Get params from URL search params const id = searchParams.get("test_id") || ""; const typeParam = searchParams.get("type"); const type = typeParam === "mock" || typeParam === "subject" || typeParam === "topic" ? typeParam : 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; try { setLoading(true); const token = await getToken(); const questionResponse = await fetch(`${API_URL}/tests/${type}/${id}`, { method: "GET", headers: { authorization: `Bearer ${token}`, }, }); if (!questionResponse.ok) { throw new Error("Failed to fetch questions"); } const data = await questionResponse.json(); const fetchedMetadata = fetchMetadata(type, data.metadata); setMetadata(fetchedMetadata); } catch (error) { console.error(error); setError(error instanceof Error ? error.message : "An error occurred"); } finally { setLoading(false); } } fetchQuestions(); }, [id, type]); if (error) { return (
} />
); } if (loading) { return (

Loading...

); } // function handleStartExam() { // if (!examData) return; // setCurrentExam(examData); // startExam(examData); // router.push(`/exam/${id}?time=${metadata?.metadata.duration}`); // } return (
{metadata ? (

{metadata.name}

Rating: 8 / 10

{metadata.num_questions}

Multiple Choice Questions

{String(metadata.time_limit_minutes)} mins

Time Taken

{metadata.deduction} marks

From each wrong answer

Ready yourself!

You must complete this test in one session - make sure your internet connection is reliable.

There is negative marking for the wrong answer.

The more you answer correctly, the better chance you have of winning a badge.

You can retake this test however many times you want. But, you will earn points only once.

) : (

Loading...

)}
{/* */}
); } export default function PretestPage() { return (

Loading...

} >
); }