This repository has been archived on 2026-11-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
hope-events/frontend/src/app/consent/[token]/page.tsx
T
joshuaandClaude Opus 5.5 809b6a11d7 Add parental consent for youth events
Youth events need a name and date of birth for every attendee, and a
parent/guardian's details for under-18s. Parents get an email and
WhatsApp link to a consent page that records the signed name, time, IP,
device and exact consent text; medical and emergency details are stored
encrypted. Tickets are issued but check-in is blocked until consent is
given (supervisor override audited). A daily job sends reminders and
purges medical details after the retention period. Adds a supervisor
consent tracker, consent status and medical list reports, and clears
consent data when an account is anonymised.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 13:24:44 +02:00

182 lines
10 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"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<ConsentInfo | null>(null);
const [loadError, setLoadError] = useState<string | null>(null);
const [error, setError] = useState<string | null>(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<boolean | null>(null);
useEffect(() => {
if (!token) return;
apiFetch<ConsentInfo>(`/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 (
<div className="min-h-screen flex flex-col">
<Navbar />
<main className="flex-1 px-4 py-10 max-w-2xl mx-auto w-full">
<div className="flex items-center gap-3 mb-4">
<div className="w-10 h-10 rounded-xl bg-brand-50 flex items-center justify-center shrink-0">
<ShieldCheck className="w-5 h-5 text-brand-600" />
</div>
<h1 className="text-2xl font-semibold text-gray-900">Parental consent</h1>
</div>
{loadError && <div className="border rounded-xl p-4 bg-red-50 text-red-800 text-sm">{loadError}</div>}
{!info && !loadError && <div className="text-sm text-gray-600">Loading…</div>}
{info && (
<div className="border rounded-xl p-5 bg-white shadow-sm space-y-4">
<div>
<div className="text-sm text-gray-500">Event</div>
<div className="font-semibold text-gray-900">{info.event.title}</div>
<div className="text-sm text-gray-600">
{formatDate(new Date(info.event.startDate))}{info.event.endDate ? ` – ${formatDate(new Date(info.event.endDate))}` : ""}
{info.event.location ? ` · ${info.event.location}` : ""}
</div>
</div>
<div>
<div className="text-sm text-gray-500">Attendee</div>
<div className="font-semibold text-gray-900">{info.attendeeName}</div>
</div>
{(done || info.status !== "pending") && (
<div className={`flex items-start gap-2 rounded-lg p-3 text-sm ${(done || info.status) === "given" ? "bg-green-50 text-green-800" : "bg-amber-50 text-amber-900"}`}>
{(done || info.status) === "given" ? <CheckCircle2 className="w-5 h-5 shrink-0" /> : <XCircle className="w-5 h-5 shrink-0" />}
<span>
{(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))})`}
</span>
</div>
)}
{!done && info.status === "pending" && info.superseded && (
<div className="rounded-lg p-3 text-sm bg-amber-50 text-amber-900">A newer consent request has replaced this one. Please use the link in your latest message.</div>
)}
{!done && info.status === "pending" && info.expired && (
<div className="rounded-lg p-3 text-sm bg-gray-100 text-gray-700">This consent link has expired because the event has ended.</div>
)}
{open && (
<>
<div>
<div className="text-sm font-medium text-gray-800 mb-1">Please read</div>
<div className="text-sm text-gray-700 whitespace-pre-line border rounded-lg p-3 bg-gray-50 max-h-72 overflow-y-auto">{info.consentText || "The organisers haven't added any consent text."}</div>
</div>
{info.collect.medical && (
<fieldset className="space-y-2">
<legend className="text-sm font-medium text-gray-800">Medical information <span className="font-normal text-gray-500">(kept confidential and deleted after the event)</span></legend>
<textarea className={inputCls} rows={2} placeholder="Allergies" value={medical.allergies} onChange={e => setMedical({ ...medical, allergies: e.target.value })} aria-label="Allergies" />
<textarea className={inputCls} rows={2} placeholder="Medication" value={medical.medication} onChange={e => setMedical({ ...medical, medication: e.target.value })} aria-label="Medication" />
<textarea className={inputCls} rows={2} placeholder="Medical conditions" value={medical.conditions} onChange={e => setMedical({ ...medical, conditions: e.target.value })} aria-label="Medical conditions" />
<input className={inputCls} placeholder="Medical aid (scheme and number)" value={medical.medicalAid} onChange={e => setMedical({ ...medical, medicalAid: e.target.value })} aria-label="Medical aid" />
</fieldset>
)}
{info.collect.emergency && (
<fieldset className="space-y-2">
<legend className="text-sm font-medium text-gray-800">Emergency contact</legend>
<div className="grid grid-cols-1 sm:grid-cols-3 gap-2">
<input className={inputCls} placeholder="Name" value={emergency.name} onChange={e => setEmergency({ ...emergency, name: e.target.value })} aria-label="Emergency contact name" />
<input type="tel" className={inputCls} placeholder="Phone" value={emergency.phone} onChange={e => setEmergency({ ...emergency, phone: e.target.value })} aria-label="Emergency contact phone" />
<input className={inputCls} placeholder="Relationship" value={emergency.relationship} onChange={e => setEmergency({ ...emergency, relationship: e.target.value })} aria-label="Relationship" />
</div>
</fieldset>
)}
{info.collect.photo && (
<fieldset>
<legend className="text-sm font-medium text-gray-800 mb-1">May {info.attendeeName} appear in event photos and videos?</legend>
<label className="inline-flex items-center gap-2 text-sm mr-4"><input type="radio" name="photo" checked={photoConsent === true} onChange={() => setPhotoConsent(true)} /> Yes</label>
<label className="inline-flex items-center gap-2 text-sm"><input type="radio" name="photo" checked={photoConsent === false} onChange={() => setPhotoConsent(false)} /> No</label>
</fieldset>
)}
<div className="space-y-2 border-t pt-4">
<label className="block text-sm font-medium text-gray-800">Type your full name to sign</label>
<input className={inputCls} value={signedName} onChange={e => setSignedName(e.target.value)} aria-label="Your full name" />
<label className="flex items-start gap-2 text-sm text-gray-700">
<input type="checkbox" className="mt-0.5" checked={isGuardian} onChange={e => setIsGuardian(e.target.checked)} />
<span>I am the parent or legal guardian of {info.attendeeName}.</span>
</label>
</div>
{error && <div className="p-3 border rounded bg-red-50 text-red-800 text-sm">{error}</div>}
<div className="flex flex-wrap gap-3">
<button onClick={() => submit("give")} disabled={busy || !isGuardian || signedName.trim().length < 2} className="px-4 py-2 rounded-lg bg-brand-600 text-white text-sm font-medium hover:bg-brand-700 disabled:opacity-50">
{busy ? "Saving…" : "Give consent"}
</button>
<button onClick={() => submit("decline")} disabled={busy || !isGuardian || signedName.trim().length < 2} className="px-4 py-2 rounded-lg border border-gray-300 text-gray-700 text-sm font-medium hover:bg-gray-50 disabled:opacity-50">
Decline
</button>
</div>
<p className="text-xs text-gray-500">We record your typed name, the time, and the device you used, together with the text above.</p>
</>
)}
</div>
)}
</main>
<Footer />
</div>
);
}