"use client"; import React, { useEffect, useState } from "react"; import { useParams } from "next/navigation"; import { Navbar } from "@/components/layout/Navbar"; import { Footer } from "@/components/layout/Footer"; import { apiFetch, ApiError } from "@/lib/api"; import { formatDate } from "@/lib/date"; import { ShieldCheck, CheckCircle2, XCircle } from "lucide-react"; type ConsentInfo = { status: "pending" | "given" | "declined"; superseded: boolean; expired: boolean; respondedAt: string | null; attendeeName: string; parentName: string | null; consentText: string; event: { title: string; startDate: string; endDate: string; location: string | null }; collect: { medical: boolean; emergency: boolean; photo: boolean }; }; const inputCls = "w-full border rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-brand-400"; /** Public page a parent/guardian opens from their consent email or WhatsApp. */ export default function ConsentPage() { const params = useParams<{ token: string }>(); const token = params?.token; const [info, setInfo] = useState(null); const [loadError, setLoadError] = useState(null); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); const [done, setDone] = useState<"given" | "declined" | null>(null); const [signedName, setSignedName] = useState(""); const [isGuardian, setIsGuardian] = useState(false); const [medical, setMedical] = useState({ allergies: "", medication: "", conditions: "", medicalAid: "" }); const [emergency, setEmergency] = useState({ name: "", phone: "", relationship: "" }); const [photoConsent, setPhotoConsent] = useState(null); useEffect(() => { if (!token) return; apiFetch(`/api/consent/${encodeURIComponent(token)}`) .then(res => { setInfo(res); setSignedName(res.parentName || ""); }) .catch(e => setLoadError(e instanceof ApiError ? e.message : "Couldn't load this consent request.")); }, [token]); const submit = async (decision: "give" | "decline") => { if (!token || !info) return; setError(null); if (decision === "give" && info.collect.photo && photoConsent === null) { setError("Please answer the photo consent question."); return; } setBusy(true); try { await apiFetch(`/api/consent/${encodeURIComponent(token)}`, { method: "POST", body: { decision, signedName, isGuardian, ...(decision === "give" && info.collect.medical ? { medical } : {}), ...(decision === "give" && info.collect.emergency ? { emergency } : {}), ...(decision === "give" && info.collect.photo ? { photoConsent } : {}), }, }); setDone(decision === "give" ? "given" : "declined"); } catch (e) { setError(e instanceof ApiError ? e.message : "Couldn't save your answer. Please try again."); } finally { setBusy(false); } }; const open = info && info.status === "pending" && !info.superseded && !info.expired && !done; return (

Parental consent

{loadError &&
{loadError}
} {!info && !loadError &&
Loading…
} {info && (
Event
{info.event.title}
{formatDate(new Date(info.event.startDate))}{info.event.endDate ? ` – ${formatDate(new Date(info.event.endDate))}` : ""} {info.event.location ? ` · ${info.event.location}` : ""}
Attendee
{info.attendeeName}
{(done || info.status !== "pending") && (
{(done || info.status) === "given" ? : } {(done || info.status) === "given" ? "Consent has been given. Thank you!" : "Consent has been declined. The organisers have been told."} {!done && info.respondedAt && ` (${formatDate(new Date(info.respondedAt))})`}
)} {!done && info.status === "pending" && info.superseded && (
A newer consent request has replaced this one. Please use the link in your latest message.
)} {!done && info.status === "pending" && info.expired && (
This consent link has expired because the event has ended.
)} {open && ( <>
Please read
{info.consentText || "The organisers haven't added any consent text."}
{info.collect.medical && (
Medical information (kept confidential and deleted after the event)