"use client" import { useState, useEffect, useRef } from "react" import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" import { Switch } from "@/components/ui/switch" import { Input } from "@/components/ui/input" import { Loader2, Upload, FileText, Clock, CalendarIcon } from "lucide-react" import DatePicker from "react-datepicker" import "react-datepicker/dist/react-datepicker.css" import { useActiveUser } from "@/hooks/useActiveUser" export default function AdminTestsPage() { const [tests, setTests] = useState([]) const [loading, setLoading] = useState(true) const [uploading, setUploading] = useState(false) const { userId } = useActiveUser() // Upload Form State const [title, setTitle] = useState("") const [examPattern, setExamPattern] = useState("JEE_Main") const [timeLimit, setTimeLimit] = useState("180") const [scheduledDate, setScheduledDate] = useState(null) const [endDate, setEndDate] = useState(null) const [questionsJson, setQuestionsJson] = useState("") const fetchTests = async () => { try { const res = await fetch("/api/admin/tests") if (res.ok) { const data = await res.json() setTests(data) } } catch (e) { console.error(e) } finally { setLoading(false) } } useEffect(() => { fetchTests() }, []) const handleToggle = async (testId: string) => { try { const res = await fetch(`/api/admin/tests/${testId}/toggle`, { method: "PUT" }) if (res.ok) { fetchTests() } } catch (e) { console.error(e) } } const handleUpload = async (e: React.FormEvent) => { e.preventDefault() if (!questionsJson) return if (!userId) return setUploading(true) let parsedQuestions = []; try { parsedQuestions = JSON.parse(questionsJson); } catch(err) { alert("Invalid JSON format. Please paste a valid JSON array."); setUploading(false); return; } const payload = { title, exam_pattern: examPattern, time_limit: parseInt(timeLimit), admin_id: userId, questions_json: parsedQuestions, scheduled_date: scheduledDate ? scheduledDate.toISOString() : null, end_date: endDate ? endDate.toISOString() : null, }; try { const res = await fetch("/api/admin/tests/create_manual", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload) }) if (res.ok) { alert("Test created successfully!") setTitle("") setQuestionsJson("") fetchTests() } else { alert("Creation failed") } } catch (error) { console.error(error) alert("Creation failed") } finally { setUploading(false) } } return (

Tests & DPPs

Manage CBT examinations and daily practice sheets

{/* Upload Form */} Create Manual Test Paste the structured JSON block of questions in LaTeX format to create a deterministic test.
setTitle(e.target.value)} placeholder="e.g. JEE Main Mock Test 1" />
setTimeLimit(e.target.value)} />
setScheduledDate(date)} showTimeSelect timeFormat="HH:mm" timeIntervals={15} timeCaption="time" dateFormat="MMMM d, yyyy h:mm aa" className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2" placeholderText="Select start date & time" />
setEndDate(date)} showTimeSelect timeFormat="HH:mm" timeIntervals={15} timeCaption="time" dateFormat="MMMM d, yyyy h:mm aa" className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2" placeholderText="Select end date & time" />