"use client"; import React, { useCallback, useEffect, useState } from "react"; import { useAuth } from "@/hooks/useAuth"; import { errorMessage } from "@/lib/utils"; import { useVisiblePolling } from "@/hooks/useVisiblePolling"; import { useDismissingState } from "@/hooks/useDismissingState"; import { Baby, BellRing, Pencil, Plus, QrCode, Trash2, UserCheck } from "lucide-react"; import { BackLink } from "@/components/shared/BackLink"; import { QrImage } from "@/components/shared/QrImage"; import { ChildForm, type ChildInput } from "@/components/kids/ChildForm"; import { FAMILY_QR_PREFIX, type KidsAdult, type KidsChild, type KidsFamily } from "@/lib/kids"; import { apiV2Data } from "@/lib/apiV2"; type ActiveCode = { code: string; qr: string; event: { id: string; title: string }; children: { name: string; room: string | null; checkedInAt: string }[] }; type Mine = { family: KidsFamily | null; qrToken: string | null; codes: ActiveCode[]; pages: { id: string; message: string; createdAt: string }[] }; type Msg = { type: "ok" | "err"; text: string } | null; const inputCls = "w-full border rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-brand-500"; /** My account → Kids: the family's children, authorised adults, family QR and live pickup codes. */ export default function MyKidsPage() { const { token, user } = useAuth(); const [data, setData] = useState(null); const [msg, setMsg] = useDismissingState(null); const [busy, setBusy] = useState(false); const [editing, setEditing] = useState(null); const [adultForm, setAdultForm] = useState<{ id?: string; name: string; phone: string; relationship: string } | null>(null); const [familyName, setFamilyName] = useState(""); const load = useCallback(async () => { if (!token) return; try { setData(await (apiV2Data("get", "/me/family", { authToken: token }) as Promise)); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "Could not load your family" }); } }, [token, setMsg]); useEffect(() => { load(); }, [load]); // Codes and pages change while children are checked in. useVisiblePolling(() => { if (data?.codes.length) load(); }, 15000, !!token); const call = async (fn: () => Promise, ok: string) => { setBusy(true); try { await fn(); setMsg({ type: "ok", text: ok }); await load(); return true; } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "Something went wrong" }); return false; } finally { setBusy(false); } }; const startFamily = () => call(() => apiV2Data("post", "/me/family", { authToken: token, body: { name: familyName || `${user?.name?.split(" ").slice(-1)[0] || "My"} family` } }), "Family added — now add your children."); const saveChild = (child: KidsChild | null, input: ChildInput) => call( () => (child ? apiV2Data("put", "/me/family/children/{id}", { params: { id: child.id }, authToken: token, body: input }) : apiV2Data("post", "/me/family/children", { authToken: token, body: input })), child ? "Saved." : "Child added.", ).then(ok => { if (ok) setEditing(null); }); const removeChild = (child: KidsChild) => { if (!confirm(`Remove ${child.firstName}? Their details are deleted.`)) return; call(() => apiV2Data("delete", "/me/family/children/{id}", { params: { id: child.id }, authToken: token }), "Removed."); }; const saveAdult = () => adultForm && call( () => (adultForm.id ? apiV2Data("put", "/me/family/adults/{id}", { params: { id: adultForm.id }, authToken: token, body: adultForm }) : apiV2Data("post", "/me/family/adults", { authToken: token, body: adultForm })), "Saved.", ).then(ok => { if (ok) setAdultForm(null); }); const removeAdult = (a: KidsAdult) => { if (!confirm(`Remove ${a.name} from your authorised adults?`)) return; call(() => apiV2Data("delete", "/me/family/adults/{id}", { params: { id: a.id }, authToken: token }), "Removed."); }; const share = async (c: ActiveCode) => { const text = `Pickup code for ${c.children.map(k => k.name.split(" ")[0]).join(", ")} at ${c.event.title}: ${c.code}`; try { if (navigator.share) await navigator.share({ text }); else { await navigator.clipboard.writeText(text); setMsg({ type: "ok", text: "Copied — paste it into a message to whoever is collecting." }); } } catch { /* cancelled */ } }; const family = data?.family; return (

Kids

Your children for kids check-in, and their pickup codes

{msg &&
{msg.text}
} {data?.pages.map(p => (
{p.message}
{new Date(p.createdAt).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })}
))} {data?.codes.map(c => (
Checked in · {c.event.title}
Pickup code
{c.code}
    {c.children.map(k =>
  • {k.name}{k.room ? ` — ${k.room}` : ""}
  • )}

Show this code or QR to collect your children. Anyone you give the code to can collect them.

))} {data && !family && (

Add your family

Add your children once, and the kids desk can check them in quickly every time — scan your family QR code or give your phone number.

setFamilyName(e.target.value)} />
)} {family && ( <>

{family.name}

Parents: {family.parents.map(p => p.name).join(", ")}
{family.children.map(c => (
{editing === c.id ? ( saveChild(c, v)} onCancel={() => setEditing(null)} /> ) : (
{c.name}{c.age != null && · {c.age} yrs}
{c.allergies && Allergies: {c.allergies}} {c.hasMedical && Medical notes ✓} {c.hasCustody && Custody notes ✓} {!c.allergies && !c.hasMedical && !c.dateOfBirth && Add a date of birth so the right room is suggested}
)}
))} {editing === "new" ? (
saveChild(null, v)} onCancel={() => setEditing(null)} saveLabel="Add child" />
) : ( )}

Authorised adults

People who may collect your children (grandparents, friends). They don't need an account — the room leader sees this list, and the desk can send them the pickup code.

    {family.adults.map(a => (
  • {a.name}{a.relationship ? ` (${a.relationship})` : ""}{a.phone ? ` · ${a.phone}` : ""}
  • ))} {!family.adults.length &&
  • Nobody yet.
  • }
{adultForm ? (
setAdultForm({ ...adultForm, name: e.target.value })} /> setAdultForm({ ...adultForm, phone: e.target.value })} /> setAdultForm({ ...adultForm, relationship: e.target.value })} />
) : ( )}
{data?.qrToken && (
Family QR code
Show this at the kids desk to find your family quickly. It only finds your family — it doesn't collect children; that needs the pickup code.
)} )}
); }