import { createFileRoute, useNavigate, Link } from "@tanstack/react-router"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { supabase } from "@/integrations/supabase/client"; import { useAuth } from "@/hooks/use-auth"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { ArrowLeft, Lock, Loader2 } from "lucide-react"; import { useState } from "react"; import { toast } from "sonner"; export const Route = createFileRoute("/_authenticated/students/new")({ head: () => ({ meta: [{ title: "New student — School Portal" }] }), component: NewStudentPage, }); function NewStudentPage() { const { roles } = useAuth(); const navigate = useNavigate(); const qc = useQueryClient(); const [firstName, setFirstName] = useState(""); const [lastName, setLastName] = useState(""); const [dob, setDob] = useState(""); const create = useMutation({ mutationFn: async () => { const { data, error } = await supabase .from("students") .insert({ first_name: firstName.trim(), last_name: lastName.trim(), dob: dob || null }) .select("id") .single(); if (error) throw error; return data.id as string; }, onSuccess: (id) => { qc.invalidateQueries({ queryKey: ["students"] }); toast.success("Student created — fill out the rest of the profile"); navigate({ to: "/students/$id", params: { id } }); }, onError: (e: Error) => toast.error(e.message), }); if (!roles.includes("admin")) { return

Admins only

Only admins can add students.

; } const canSave = firstName.trim() && lastName.trim() && !create.isPending; return (
Back to students

New student

Start with a name — you'll fill out the full profile (or send it to a parent) next.

{ e.preventDefault(); if (canSave) create.mutate(); }} className="bg-card border rounded-lg p-5 space-y-4">
setFirstName(e.target.value)} required autoFocus />
setLastName(e.target.value)} required />
setDob(e.target.value)} />
); }