Archived
68 lines
2.9 KiB
TypeScript
68 lines
2.9 KiB
TypeScript
"use client";
|
|
|
|
import React, { useEffect, useState } from "react";
|
|
import { apiFetch } from "@/lib/api";
|
|
import { formatDate } from "@/lib/date";
|
|
import type { KidsEvent } from "@/lib/kids";
|
|
|
|
type Props = {
|
|
token: string | null;
|
|
eventId: string;
|
|
sessionId: string;
|
|
onChange: (eventId: string, sessionId: string) => void;
|
|
};
|
|
|
|
const STORAGE = "hope_events_kids_event";
|
|
|
|
/** Picks the kids event (and session) the desk or room screen works on; remembers the choice. */
|
|
export function KidsEventPicker({ token, eventId, sessionId, onChange }: Props) {
|
|
const [events, setEvents] = useState<KidsEvent[] | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!token) return;
|
|
apiFetch<KidsEvent[]>("/api/kids/events", { authToken: token })
|
|
.then(list => {
|
|
setEvents(list);
|
|
if (eventId || !list.length) return;
|
|
let remembered = "";
|
|
try { remembered = localStorage.getItem(STORAGE) || ""; } catch { /* ignore */ }
|
|
const now = Date.now();
|
|
const current = list.find(e => e.id === remembered) || list.find(e => new Date(e.endDate).getTime() >= now) || list[0];
|
|
onChange(current.id, "");
|
|
})
|
|
.catch(e => setError(e?.message || "Could not load events"));
|
|
}, [token]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
|
|
const event = events?.find(e => e.id === eventId);
|
|
const pick = (id: string, sid = "") => {
|
|
try { localStorage.setItem(STORAGE, id); } catch { /* ignore */ }
|
|
onChange(id, sid);
|
|
};
|
|
|
|
if (error) return <div className="p-3 border rounded bg-red-50 text-red-800 text-sm">{error}</div>;
|
|
if (events && !events.length) {
|
|
return <div className="p-3 border rounded bg-amber-50 text-amber-800 text-sm">No events have kids check-in switched on. A supervisor sets it up under Supervisor → Kids check-in setup.</div>;
|
|
}
|
|
return (
|
|
<div className="flex flex-wrap gap-2 items-end">
|
|
<label className="text-sm">
|
|
<span className="block text-xs text-gray-500 mb-1">Event</span>
|
|
<select className="border rounded-lg px-3 py-2 text-sm min-w-[14rem]" value={eventId} onChange={e => pick(e.target.value)}>
|
|
{!eventId && <option value="">Choose an event…</option>}
|
|
{events?.map(e => <option key={e.id} value={e.id}>{e.title} · {formatDate(new Date(e.startDate))}</option>)}
|
|
</select>
|
|
</label>
|
|
{event?.scheduleType === "sessions" && (
|
|
<label className="text-sm">
|
|
<span className="block text-xs text-gray-500 mb-1">Session</span>
|
|
<select className="border rounded-lg px-3 py-2 text-sm" value={sessionId} onChange={e => pick(event.id, e.target.value)}>
|
|
<option value="">Today's session</option>
|
|
{event.sessions.map((s, i) => <option key={s.id} value={s.id}>{s.label || `Session ${i + 1}`} · {formatDate(new Date(s.startsAt))}</option>)}
|
|
</select>
|
|
</label>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|