"use client"; import React, { useCallback, useEffect, useState } from "react"; import Link from "next/link"; import { useAuth } from "@/hooks/useAuth"; import { apiFetch } from "@/lib/api"; import { errorMessage } from "@/lib/utils"; import { formatDate } from "@/lib/date"; import { useDismissingState } from "@/hooks/useDismissingState"; import { Baby, Plus, Trash2 } from "lucide-react"; import { BackLink } from "@/components/shared/BackLink"; type Room = { id: string; name: string; capacity: number | null; minAge: number | null; maxAge: number | null; ratio: number | null; order: number }; type Settings = { event: { id: string; title: string; scheduleType: string; kidsCheckIn: boolean; kidsOptionId: string | null; kidsRoomSetId: string | null }; options: { id: string; name: string; price: number }[]; sets: { id: string; name: string; groups: Room[] }[]; }; type SetupEvent = { id: string; title: string; startDate: string; kidsCheckIn: boolean }; const inputCls = "w-full border rounded-lg px-2 py-1.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-500"; const num = (v: string) => (v.trim() === "" ? null : Number(v)); /** Supervisor → Kids check-in setup: switch it on for an event, choose the kids ticket and the rooms. */ export default function KidsSetupPage() { const { token } = useAuth(); const [events, setEvents] = useState([]); const [eventId, setEventId] = useState(""); const [s, setS] = useState(null); const [msg, setMsg] = useDismissingState<{ type: "ok" | "err"; text: string } | null>(null); const [busy, setBusy] = useState(false); const [draft, setDraft] = useState>({}); const [newRoom, setNewRoom] = useState({ name: "", minAge: "", maxAge: "", capacity: "", ratio: "" }); useEffect(() => { if (!token) return; apiFetch("/api/kids/setup-events", { authToken: token }).then(setEvents).catch(e => setMsg({ type: "err", text: errorMessage(e) || "Could not load events" })); }, [token, setMsg]); const load = useCallback(async () => { if (!token || !eventId) return; try { const data = await apiFetch(`/api/kids/events/${eventId}/settings`, { authToken: token }); setS(data); const set = data.sets.find(x => x.id === data.event.kidsRoomSetId); setDraft(Object.fromEntries((set?.groups || []).map(g => [g.id, { name: g.name, minAge: g.minAge?.toString() ?? "", maxAge: g.maxAge?.toString() ?? "", capacity: g.capacity?.toString() ?? "", ratio: g.ratio?.toString() ?? "" }]))); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "Could not load the settings" }); } }, [token, eventId, setMsg]); useEffect(() => { setS(null); load(); }, [load]); const run = 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 save = (body: Record, ok = "Saved.") => run(() => apiFetch(`/api/kids/events/${eventId}/settings`, { method: "PUT", authToken: token, body }), ok); const rooms = s?.sets.find(x => x.id === s.event.kidsRoomSetId)?.groups || []; const roomBody = (d: { name: string; minAge: string; maxAge: string; capacity: string; ratio: string }) => ({ name: d.name, minAge: num(d.minAge), maxAge: num(d.maxAge), capacity: num(d.capacity), ratio: num(d.ratio) }); const addRoom = () => s?.event.kidsRoomSetId && run( () => apiFetch(`/api/allocations/sets/${s.event.kidsRoomSetId}/groups`, { method: "POST", authToken: token, body: roomBody(newRoom) }), "Room added.", ).then(ok => { if (ok) setNewRoom({ name: "", minAge: "", maxAge: "", capacity: "", ratio: "" }); }); const saveRoom = (id: string) => run(() => apiFetch(`/api/allocations/groups/${id}`, { method: "PUT", authToken: token, body: roomBody(draft[id]) }), "Room saved."); const deleteRoom = (r: Room) => confirm(`Delete ${r.name}? Children checked in to it now will need a new room.`) && run(() => apiFetch(`/api/allocations/groups/${r.id}`, { method: "DELETE", authToken: token }), "Room deleted."); const roomRow = (d: { name: string; minAge: string; maxAge: string; capacity: string; ratio: string }, set: (v: typeof d) => void) => ( <> set({ ...d, name: e.target.value })} placeholder="Toddlers" /> set({ ...d, minAge: e.target.value })} placeholder="from" /> set({ ...d, maxAge: e.target.value })} placeholder="to" /> set({ ...d, capacity: e.target.value })} placeholder="—" /> set({ ...d, ratio: e.target.value })} placeholder="—" /> ); return (

Kids check-in setup

Switch kids check-in on for an event and set up its rooms

{msg &&
{msg.text}
} {s && (

1. Kids ticket

Each child gets a free place on this ticket the first time they're checked in, so they appear on the attendance register and reports. Parents don't buy it.

{!s.event.kidsOptionId && }

2. Rooms

{!s.event.kidsRoomSetId && }
{s.event.kidsRoomSetId && (
RoomAge fromAge toCapacityPer leader {rooms.map(r => draft[r.id] && ( {roomRow(draft[r.id], v => setDraft(d => ({ ...d, [r.id]: v })))} ))} {roomRow(newRoom, setNewRoom)}

Ages are whole years (both included). The desk suggests the room with the narrowest age range that fits and isn't full. "Per leader" is the ratio: the room screen warns when there are more children than the leaders on duty can look after.

)}

3. Switch on

{s.event.kidsCheckIn ? "Kids check-in is on — staff use Staff → Kids check-in." : "Staff can check children in once this is on."}

{s.event.kidsCheckIn && Open the desk}
)}
); }