Archived
148 lines
9.0 KiB
TypeScript
148 lines
9.0 KiB
TypeScript
"use client";
|
|
|
|
import React, { useState } from "react";
|
|
import { apiFetch } from "@/lib/api";
|
|
import { errorMessage } from "@/lib/utils";
|
|
import { AlertTriangle, BellRing, HeartPulse, LogOut, ShieldAlert, Users } from "lucide-react";
|
|
import { ageRange, type KidsBoard, type KidsCheckIn, type KidsRoom } from "@/lib/kids";
|
|
|
|
type Props = {
|
|
board: KidsBoard;
|
|
token: string | null;
|
|
/** Only this room ('' = every room). */
|
|
roomId: string;
|
|
onChanged: () => void;
|
|
onMessage: (m: { type: "ok" | "err"; text: string }) => void;
|
|
};
|
|
|
|
const time = (iso: string) => new Date(iso).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
|
|
|
|
export function RoomStatusBadges({ room }: { room: KidsRoom }) {
|
|
return (
|
|
<span className="inline-flex flex-wrap gap-1 text-xs">
|
|
<span className="px-2 py-0.5 rounded-full bg-gray-100 text-gray-700">{room.children}{room.capacity ? ` / ${room.capacity}` : ""} in</span>
|
|
<span className={`px-2 py-0.5 rounded-full ${room.overRatio ? "bg-red-100 text-red-800" : "bg-gray-100 text-gray-700"}`}>
|
|
{room.leaders} leader{room.leaders === 1 ? "" : "s"}{room.ratio ? ` (needs ${room.leadersNeeded})` : ""}
|
|
</span>
|
|
{room.full && <span className="px-2 py-0.5 rounded-full bg-amber-100 text-amber-800">Full</span>}
|
|
{room.overRatio && <span className="px-2 py-0.5 rounded-full bg-red-100 text-red-800">Over ratio</span>}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
/** Live cards of the children in the rooms, with paging, moving and checking out without the code. */
|
|
export function RoomBoard({ board, token, roomId, onChanged, onMessage }: Props) {
|
|
const [busy, setBusy] = useState<string | null>(null);
|
|
const rooms = board.rooms.filter(r => !roomId || r.id === roomId);
|
|
const alerts = board.alerts.filter(a => !a.resolvedAt && a.kind === "wrong_code" && (!roomId || !a.roomId || a.roomId === roomId));
|
|
|
|
const act = async (id: string, fn: () => Promise<unknown>, ok: string) => {
|
|
setBusy(id);
|
|
try {
|
|
await fn();
|
|
onMessage({ type: "ok", text: ok });
|
|
onChanged();
|
|
} catch (e) {
|
|
onMessage({ type: "err", text: errorMessage(e) || "Something went wrong" });
|
|
} finally {
|
|
setBusy(null);
|
|
}
|
|
};
|
|
|
|
const page = (c: KidsCheckIn) => {
|
|
const extra = prompt(`Message the parents of ${c.firstName}? They'll be asked to come to the room. Add a note (optional):`, "");
|
|
if (extra === null) return;
|
|
act(c.id, () => apiFetch(`/api/kids/check-ins/${c.id}/page`, { method: "POST", authToken: token, body: { message: extra } }), `Message sent to ${c.firstName}'s parents.`);
|
|
};
|
|
const override = (c: KidsCheckIn) => {
|
|
const who = prompt(`Who is collecting ${c.firstName}?`, c.family?.parents[0]?.name || "");
|
|
if (!who) return;
|
|
const reason = prompt("Why without the pickup code? (recorded in the audit log)", "");
|
|
if (!reason) return;
|
|
act(c.id, () => apiFetch(`/api/kids/check-ins/${c.id}/override`, { method: "POST", authToken: token, body: { collectedBy: who, reason } }), `${c.firstName} checked out.`);
|
|
};
|
|
const move = (c: KidsCheckIn, to: string) => act(c.id, () => apiFetch(`/api/kids/check-ins/${c.id}/room`, { method: "PUT", authToken: token, body: { roomId: to } }), `${c.firstName} moved.`);
|
|
const resolve = (id: string) => act(id, () => apiFetch(`/api/kids/alerts/${id}/resolve`, { method: "POST", authToken: token }), "Alert cleared.");
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
{alerts.map(a => (
|
|
<div key={a.id} role="alert" className="p-3 rounded-xl border-2 border-red-500 bg-red-50 flex items-start justify-between gap-3">
|
|
<div className="flex gap-2"><ShieldAlert className="w-5 h-5 text-red-600 shrink-0" />
|
|
<div><div className="font-semibold text-red-900">{a.message}</div><div className="text-xs text-red-700">{time(a.at)}{a.by ? ` · ${a.by}` : ""} — check who is collecting before handing over any child.</div></div>
|
|
</div>
|
|
<button disabled={busy === a.id} onClick={() => resolve(a.id)} className="text-sm px-3 py-1 rounded-lg border border-red-300 bg-white">Dealt with</button>
|
|
</div>
|
|
))}
|
|
{rooms.map(r => {
|
|
const inRoom = board.checkIns.filter(c => c.roomId === r.id && !c.checkedOutAt);
|
|
const out = board.checkIns.filter(c => c.roomId === r.id && c.checkedOutAt);
|
|
return (
|
|
<section key={r.id} className="border rounded-xl p-4 bg-white shadow-sm">
|
|
<div className="flex flex-wrap items-center justify-between gap-2 mb-3">
|
|
<h2 className="font-semibold text-lg">{r.name} <span className="text-sm font-normal text-gray-500">{ageRange(r)}</span></h2>
|
|
<RoomStatusBadges room={r} />
|
|
</div>
|
|
{r.leaderNames.length > 0 && <div className="text-xs text-gray-500 mb-2 flex items-center gap-1"><Users className="w-3.5 h-3.5" /> On duty: {r.leaderNames.join(", ")}</div>}
|
|
{inRoom.length === 0 && <p className="text-sm text-gray-500">No children in this room.</p>}
|
|
<div className="grid sm:grid-cols-2 gap-3">
|
|
{inRoom.map(c => (
|
|
<div key={c.id} className={`border rounded-lg p-3 ${c.custodyNotes ? "border-red-400" : c.allergies || c.medicalNotes ? "border-amber-400" : ""}`} data-testid="kids-card">
|
|
<div className="flex items-start justify-between gap-2">
|
|
<div>
|
|
<div className="font-semibold">{c.name}{c.age != null && <span className="text-sm font-normal text-gray-500"> · {c.age}</span>}</div>
|
|
<div className="text-xs text-gray-500">In {time(c.checkedInAt)} · code <span className="font-mono font-semibold text-gray-800">{c.code}</span>{c.offline ? " · offline" : ""}</div>
|
|
</div>
|
|
</div>
|
|
{c.allergies && <div className="mt-1 text-sm text-red-800 flex gap-1"><AlertTriangle className="w-4 h-4 shrink-0" /> Allergies: {c.allergies}</div>}
|
|
{c.medicalNotes && <div className="mt-1 text-sm text-amber-800 flex gap-1"><HeartPulse className="w-4 h-4 shrink-0" /> {c.medicalNotes}</div>}
|
|
{c.custodyNotes && <div className="mt-1 text-sm text-red-900 bg-red-50 rounded px-2 py-1 flex gap-1"><ShieldAlert className="w-4 h-4 shrink-0" /> {c.custodyNotes}</div>}
|
|
{c.family && (
|
|
<div className="mt-2 text-xs text-gray-600">
|
|
{c.family.parents.map(p => <div key={p.userId}>{p.name}{p.phone ? ` · ${p.phone}` : ""}</div>)}
|
|
{c.family.adults.length > 0 && <div>May also collect: {c.family.adults.map(a => `${a.name}${a.relationship ? ` (${a.relationship})` : ""}`).join(", ")}</div>}
|
|
</div>
|
|
)}
|
|
<div className="mt-2 flex flex-wrap gap-1.5">
|
|
<button disabled={busy === c.id} onClick={() => page(c)} className="inline-flex items-center gap-1 text-xs px-2 py-1 rounded-lg border"><BellRing className="w-3.5 h-3.5" /> Call parent</button>
|
|
<select aria-label={`Move ${c.firstName}`} value="" onChange={e => e.target.value && move(c, e.target.value)} className="text-xs border rounded-lg px-2 py-1">
|
|
<option value="">Move to…</option>
|
|
{board.rooms.filter(o => o.id !== r.id).map(o => <option key={o.id} value={o.id}>{o.name}</option>)}
|
|
</select>
|
|
<button disabled={busy === c.id} onClick={() => override(c)} className="inline-flex items-center gap-1 text-xs px-2 py-1 rounded-lg border text-gray-600"><LogOut className="w-3.5 h-3.5" /> Without code</button>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
{out.length > 0 && (
|
|
<details className="mt-3 text-sm">
|
|
<summary className="cursor-pointer text-gray-500">Collected ({out.length})</summary>
|
|
<ul className="mt-1 space-y-0.5 text-xs text-gray-600">
|
|
{out.map(c => <li key={c.id}>{c.name} — {time(c.checkedOutAt!)} by {c.collectedBy || "—"}{c.override ? " (without code)" : ""}</li>)}
|
|
</ul>
|
|
</details>
|
|
)}
|
|
</section>
|
|
);
|
|
})}
|
|
{!roomId && board.checkIns.some(c => !c.roomId && !c.checkedOutAt) && (
|
|
<section className="border border-amber-300 rounded-xl p-4 bg-amber-50">
|
|
<h2 className="font-semibold mb-2">No room</h2>
|
|
<p className="text-xs text-amber-800 mb-2">Their room was deleted — choose a new one.</p>
|
|
<ul className="space-y-1 text-sm">
|
|
{board.checkIns.filter(c => !c.roomId && !c.checkedOutAt).map(c => (
|
|
<li key={c.id} className="flex items-center gap-2">
|
|
<span>{c.name}</span>
|
|
<select aria-label={`Move ${c.firstName}`} value="" onChange={e => e.target.value && move(c, e.target.value)} className="text-xs border rounded-lg px-2 py-1 bg-white">
|
|
<option value="">Move to…</option>
|
|
{board.rooms.map(o => <option key={o.id} value={o.id}>{o.name}</option>)}
|
|
</select>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</section>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|