"use client"; import { useSearchParams } from "next/navigation"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import Header from "@/components/Header"; import DestructibleAlert from "@/components/DestructibleAlert"; import BackgroundWrapper from "@/components/BackgroundWrapper"; import { API_URL, getToken } from "@/lib/auth"; import { Loader, RefreshCw } from "lucide-react"; import { v4 as uuidv4 } from "uuid"; import { useAuth } from "@/context/AuthContext"; import { Question } from "@/types/exam"; type Subject = { subject_id: string; name: string; unit: string; }; export default function PaperScreen() { const router = useRouter(); const searchParams = useSearchParams(); const { user } = useAuth(); const [subjects, setSubjects] = useState([]); const [errorMsg, setErrorMsg] = useState(null); const [refreshing, setRefreshing] = useState(false); async function fetchSubjects() { setRefreshing(true); try { const token = await getToken(); const response = await fetch(`${API_URL}/subjects/`, { method: "GET", headers: { Authorization: `Bearer ${token}`, }, }); if (!response.ok) { throw new Error("Failed to fetch subjects"); } const fetchedSubjects: Subject[] = await response.json(); setSubjects(fetchedSubjects); } catch (error) { setErrorMsg( "Error fetching subjects: " + (error instanceof Error ? error.message : "Unknown error") ); } finally { setRefreshing(false); } } useEffect(() => { const fetchData = async () => { if (await getToken()) { fetchSubjects(); } }; fetchData(); }, []); const onRefresh = async () => { fetchSubjects(); }; if (errorMsg) { return (
); } return (

{user?.preparation_unit}

{subjects.length > 0 ? ( subjects .filter((subject) => subject.unit === user?.preparation_unit) .map((subject) => (
)) ) : (

Loading...

)}
{/* */}
); }