diff --git a/src/routes/_authenticated/students.new.tsx b/src/routes/_authenticated/students.new.tsx new file mode 100644 index 0000000..9b90e68 --- /dev/null +++ b/src/routes/_authenticated/students.new.tsx @@ -0,0 +1,67 @@ +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
Only admins can add students.
Start with a name — you'll fill out the full profile (or send it to a parent) next.
+ + +