import { createFileRoute } from "@tanstack/react-router"; import { useQuery, 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 { Textarea } from "@/components/ui/textarea"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"; import { Plus, Trash2, FileText } from "lucide-react"; import { useState } from "react"; import { toast } from "sonner"; type Field = { label: string; type: "text" | "textarea" | "yesno" | "date" }; export const Route = createFileRoute("/_authenticated/forms")({ head: () => ({ meta: [{ title: "Forms — School Portal" }] }), component: FormsPage, }); function FormsPage() { const { user, roles } = useAuth(); const isAdmin = roles.includes("admin"); const qc = useQueryClient(); const [activeForm, setActiveForm] = useState(null); const { data: forms } = useQuery({ queryKey: ["forms"], queryFn: async () => (await supabase.from("forms").select("*").eq("active", true).order("created_at", { ascending: false })).data ?? [], }); return (

Forms

Push a form to families; they complete it in the portal.

{isAdmin && qc.invalidateQueries({ queryKey: ["forms"] })} />}
{(forms ?? []).map((f) => ( ))} {forms?.length === 0 &&
No active forms.
}
{activeForm && setActiveForm(null)} isAdmin={isAdmin} />}
); } function FormBuilder({ onCreated }: { onCreated: () => void }) { const { user } = useAuth(); const [open, setOpen] = useState(false); const [title, setTitle] = useState(""); const [description, setDescription] = useState(""); const [fields, setFields] = useState([{ label: "Full name", type: "text" }]); const create = useMutation({ mutationFn: async () => { const { error } = await supabase.from("forms").insert({ title, description, schema_json: fields, created_by: user!.id }); if (error) throw error; }, onSuccess: () => { setOpen(false); setTitle(""); setDescription(""); setFields([{ label: "Full name", type: "text" }]); onCreated(); toast.success("Form created"); }, onError: (e: Error) => toast.error(e.message), }); return ( Create form
setTitle(e.target.value)} />