Archived
- The 192 places the rules flagged are fixed and the rules are errors again (npx eslint src: nothing). lib/hooks.ts: useStateSync (state that follows other state, set while rendering), useExternalEffect (API loads and browser reads), useNow; lib/navigation.ts hardNavigate for full reloads after sign-out or identity switches; other pages use the router. Resets moved into the handlers that cause them. - The banner shows and hides at its times on open pages. - HANDOVER, roadmap, README, FEATURES, SETTINGS, CHANGELOG updated; 2.0.0 section drafted in WHATS-NEW. e2e 102/102 (PayFast sandbox skipped); production build passes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
166 lines
11 KiB
TypeScript
166 lines
11 KiB
TypeScript
"use client";
|
|
|
|
import React, { useCallback, useEffect, useState } from "react";
|
|
import Link from "next/link";
|
|
import { useAuth } from "@/hooks/useAuth";
|
|
import { errorMessage } from "@/lib/utils";
|
|
import { useDismissingState } from "@/hooks/useDismissingState";
|
|
import { Baby, Plus, Trash2 } from "lucide-react";
|
|
import { BackLink } from "@/components/shared/BackLink";
|
|
import { EventPicker } from "@/components/shared/EventPicker";
|
|
import { apiV2Data } from "@/lib/apiV2";
|
|
import { useExternalEffect } from "@/lib/hooks";
|
|
|
|
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<SetupEvent[]>([]);
|
|
const [eventId, setEventId] = useState("");
|
|
const [s, setS] = useState<Settings | null>(null);
|
|
const [msg, setMsg] = useDismissingState<{ type: "ok" | "err"; text: string } | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
const [draft, setDraft] = useState<Record<string, { name: string; minAge: string; maxAge: string; capacity: string; ratio: string }>>({});
|
|
const [newRoom, setNewRoom] = useState({ name: "", minAge: "", maxAge: "", capacity: "", ratio: "" });
|
|
|
|
useEffect(() => {
|
|
if (!token) return;
|
|
(apiV2Data("get", "/kids/setup-events", { authToken: token }) as Promise<SetupEvent[]>).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 (apiV2Data("get", "/events/{id}/kids/settings", { params: { id: eventId }, authToken: token }) as Promise<Settings>);
|
|
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]);
|
|
useExternalEffect(() => { setS(null); load(); }, [load]);
|
|
|
|
const run = async (fn: () => Promise<unknown>, 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<string, unknown>, ok = "Saved.") => run(() => apiV2Data("put", "/events/{id}/kids/settings", { params: { id: eventId }, 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(
|
|
() => apiV2Data("post", "/allocation-sets/{id}/groups", { params: { id: s.event.kidsRoomSetId! }, authToken: token, body: roomBody(newRoom) }),
|
|
"Room added.",
|
|
).then(ok => { if (ok) setNewRoom({ name: "", minAge: "", maxAge: "", capacity: "", ratio: "" }); });
|
|
const saveRoom = (id: string) => run(() => apiV2Data("patch", "/allocation-groups/{id}", { params: { id: id }, 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(() => apiV2Data("delete", "/allocation-groups/{id}", { params: { id: r.id }, authToken: token }), "Room deleted.");
|
|
|
|
const roomRow = (d: { name: string; minAge: string; maxAge: string; capacity: string; ratio: string }, set: (v: typeof d) => void) => (
|
|
<>
|
|
<input aria-label="Room name" className={inputCls} value={d.name} onChange={e => set({ ...d, name: e.target.value })} placeholder="Toddlers" />
|
|
<input aria-label="Youngest age" className={inputCls} inputMode="numeric" value={d.minAge} onChange={e => set({ ...d, minAge: e.target.value })} placeholder="from" />
|
|
<input aria-label="Oldest age" className={inputCls} inputMode="numeric" value={d.maxAge} onChange={e => set({ ...d, maxAge: e.target.value })} placeholder="to" />
|
|
<input aria-label="Capacity" className={inputCls} inputMode="numeric" value={d.capacity} onChange={e => set({ ...d, capacity: e.target.value })} placeholder="—" />
|
|
<input aria-label="Children per leader" className={inputCls} inputMode="numeric" value={d.ratio} onChange={e => set({ ...d, ratio: e.target.value })} placeholder="—" />
|
|
</>
|
|
);
|
|
|
|
return (
|
|
<div className="max-w-4xl mx-auto w-full p-6">
|
|
<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"><Baby className="w-5 h-5 text-brand-600" /></div>
|
|
<div>
|
|
<BackLink href="/dashboard" />
|
|
<h1 className="text-2xl font-semibold text-gray-900 mt-0.5">Kids check-in setup</h1>
|
|
<p className="text-sm text-gray-500">Switch kids check-in on for an event and set up its rooms</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="text-sm block max-w-md">
|
|
<label className="block text-xs text-gray-500 mb-1" htmlFor="kids-setup-event">Event</label>
|
|
<EventPicker id="kids-setup-event" className="w-full" events={events} value={eventId} onChange={setEventId} describe={e => (e.kidsCheckIn ? "kids check-in on" : null)} />
|
|
</div>
|
|
|
|
{msg && <div role="status" className={`mt-3 p-3 rounded-lg text-sm ${msg.type === "ok" ? "bg-green-50 text-green-800 border border-green-200" : "bg-red-50 text-red-800 border border-red-200"}`}>{msg.text}</div>}
|
|
|
|
{s && (
|
|
<div className="space-y-4 mt-4">
|
|
<section className="border rounded-xl p-4 bg-white shadow-sm space-y-3">
|
|
<h2 className="font-semibold">1. Kids ticket</h2>
|
|
<p className="text-sm text-gray-600">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.</p>
|
|
<div className="flex flex-wrap gap-2">
|
|
<select className="border rounded-lg px-3 py-2 text-sm" value={s.event.kidsOptionId || ""} onChange={e => save({ optionId: e.target.value || null })} aria-label="Kids ticket">
|
|
<option value="">Choose…</option>
|
|
{s.options.map(o => <option key={o.id} value={o.id}>{o.name}{o.price ? ` (R${o.price})` : " (free)"}</option>)}
|
|
</select>
|
|
{!s.event.kidsOptionId && <button disabled={busy} onClick={() => save({ optionId: "new" }, "Added a free \"Kids check-in\" ticket.")} className="px-3 py-2 rounded-lg border text-sm">Add a free "Kids check-in" ticket</button>}
|
|
</div>
|
|
</section>
|
|
|
|
<section className="border rounded-xl p-4 bg-white shadow-sm space-y-3">
|
|
<h2 className="font-semibold">2. Rooms</h2>
|
|
<div className="flex flex-wrap gap-2">
|
|
<select className="border rounded-lg px-3 py-2 text-sm" value={s.event.kidsRoomSetId || ""} onChange={e => save({ roomSetId: e.target.value || null })} aria-label="Rooms set">
|
|
<option value="">Choose…</option>
|
|
{s.sets.map(x => <option key={x.id} value={x.id}>{x.name} ({x.groups.length} rooms)</option>)}
|
|
</select>
|
|
{!s.event.kidsRoomSetId && <button disabled={busy} onClick={() => save({ roomSetId: "new" }, "Added \"Kids rooms\" — now add the rooms.")} className="px-3 py-2 rounded-lg border text-sm">Add "Kids rooms"</button>}
|
|
</div>
|
|
{s.event.kidsRoomSetId && (
|
|
<div className="overflow-x-auto">
|
|
<div className="grid grid-cols-[2fr_repeat(4,minmax(4rem,1fr))_auto] gap-2 items-center min-w-[36rem] text-xs text-gray-500">
|
|
<span>Room</span><span>Age from</span><span>Age to</span><span>Capacity</span><span>Per leader</span><span />
|
|
{rooms.map(r => draft[r.id] && (
|
|
<React.Fragment key={r.id}>
|
|
{roomRow(draft[r.id], v => setDraft(d => ({ ...d, [r.id]: v })))}
|
|
<span className="flex gap-1">
|
|
<button disabled={busy} onClick={() => saveRoom(r.id)} className="px-2 py-1.5 rounded-lg border text-sm text-gray-800">Save</button>
|
|
<button disabled={busy} aria-label={`Delete ${r.name}`} onClick={() => deleteRoom(r)} className="p-1.5 rounded-lg border"><Trash2 className="w-4 h-4" /></button>
|
|
</span>
|
|
</React.Fragment>
|
|
))}
|
|
{roomRow(newRoom, setNewRoom)}
|
|
<button disabled={busy || !newRoom.name.trim()} onClick={addRoom} className="px-2 py-1.5 rounded-lg bg-brand-600 text-white text-sm inline-flex items-center gap-1 disabled:opacity-50"><Plus className="w-4 h-4" /> Add</button>
|
|
</div>
|
|
<p className="text-xs text-gray-500 mt-2">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.</p>
|
|
</div>
|
|
)}
|
|
</section>
|
|
|
|
<section className="border rounded-xl p-4 bg-white shadow-sm flex flex-wrap items-center gap-3">
|
|
<div className="flex-1 min-w-[14rem]">
|
|
<h2 className="font-semibold">3. Switch on</h2>
|
|
<p className="text-sm text-gray-600">{s.event.kidsCheckIn ? "Kids check-in is on — staff use Staff → Kids check-in." : "Staff can check children in once this is on."}</p>
|
|
</div>
|
|
<button disabled={busy} onClick={() => save({ enabled: !s.event.kidsCheckIn }, s.event.kidsCheckIn ? "Kids check-in switched off." : "Kids check-in switched on.")} className={`px-4 py-2 rounded-lg text-sm font-medium ${s.event.kidsCheckIn ? "border" : "bg-brand-600 text-white"}`}>
|
|
{s.event.kidsCheckIn ? "Switch off" : "Switch on"}
|
|
</button>
|
|
{s.event.kidsCheckIn && <Link href="/dashboard/staff/kids" className="px-4 py-2 rounded-lg border text-sm">Open the desk</Link>}
|
|
</section>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|