Archived
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
152 lines
8.8 KiB
TypeScript
152 lines
8.8 KiB
TypeScript
"use client";
|
|
|
|
import React, { useCallback, useEffect, useState } from "react";
|
|
import { useAuth } from "@/hooks/useAuth";
|
|
import { errorMessage } from "@/lib/utils";
|
|
import { useDismissingState } from "@/hooks/useDismissingState";
|
|
import { BackLink } from "@/components/shared/BackLink";
|
|
import { RosterSheet } from "@/components/volunteers/RosterSheet";
|
|
import { CheckInQr } from "@/components/volunteers/CheckInQr";
|
|
import { HandHeart } from "lucide-react";
|
|
import { hoursText, type HoursRow, type RosterEvent } from "@/lib/volunteers";
|
|
import { EventPicker } from "@/components/shared/EventPicker";
|
|
import { apiV2Data } from "@/lib/apiV2";
|
|
|
|
type Settings = {
|
|
event: { id: string; title: string; volunteerOptionId: string | null };
|
|
options: { id: string; name: string; price: number }[];
|
|
kidsRooms: { id: string; name: string }[];
|
|
};
|
|
type Tab = "roster" | "qr" | "ticket" | "hours";
|
|
const TABS: { id: Tab; label: string }[] = [
|
|
{ id: "roster", label: "Roster" },
|
|
{ id: "qr", label: "Check-in QR" },
|
|
{ id: "ticket", label: "Volunteer ticket" },
|
|
{ id: "hours", label: "Hours served" },
|
|
];
|
|
|
|
/** Supervisor → Volunteer rosters: serving roles per event or session, people, check-in QR, volunteer ticket, hours. */
|
|
export default function VolunteerRostersPage() {
|
|
const { token } = useAuth();
|
|
const [events, setEvents] = useState<RosterEvent[]>([]);
|
|
const [eventId, setEventId] = useState("");
|
|
const [tab, setTab] = useState<Tab>("roster");
|
|
const [settings, setSettings] = useState<Settings | null>(null);
|
|
const [hours, setHours] = useState<HoursRow[] | null>(null);
|
|
const [hoursAll, setHoursAll] = useState(true);
|
|
const [msg, setMsg] = useDismissingState<{ type: "ok" | "err"; text: string } | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!token) return;
|
|
(apiV2Data("get", "/volunteering/events", { query: { all: "1" }, authToken: token }) as Promise<RosterEvent[]>)
|
|
.then(list => {
|
|
setEvents(list);
|
|
const wanted = new URLSearchParams(window.location.search).get("event");
|
|
if (wanted && list.some(e => e.id === wanted)) setEventId(wanted);
|
|
})
|
|
.catch(e => setMsg({ type: "err", text: errorMessage(e) || "Could not load events" }));
|
|
}, [token, setMsg]);
|
|
|
|
const loadSettings = useCallback(async () => {
|
|
if (!token || !eventId) { setSettings(null); return; }
|
|
try {
|
|
setSettings(await (apiV2Data("get", "/events/{id}/volunteers/settings", { params: { id: eventId }, authToken: token }) as Promise<Settings>));
|
|
} catch (e) {
|
|
setMsg({ type: "err", text: errorMessage(e) || "Could not load the settings" });
|
|
}
|
|
}, [token, eventId, setMsg]);
|
|
useEffect(() => { loadSettings(); }, [loadSettings]);
|
|
|
|
useEffect(() => {
|
|
if (!token || tab !== "hours") return;
|
|
setHours(null);
|
|
(apiV2Data("get", "/volunteering/hours", { query: { eventId: !hoursAll && eventId ? eventId : undefined }, authToken: token }) as Promise<HoursRow[]>)
|
|
.then(setHours)
|
|
.catch(e => setMsg({ type: "err", text: errorMessage(e) || "Could not load hours" }));
|
|
}, [token, tab, hoursAll, eventId, setMsg]);
|
|
|
|
const saveTicket = async (optionId: string | null, ok: string) => {
|
|
setBusy(true);
|
|
try {
|
|
setSettings(await (apiV2Data("put", "/events/{id}/volunteers/settings", { params: { id: eventId }, authToken: token, body: { optionId } }) as Promise<Settings>));
|
|
setMsg({ type: "ok", text: ok });
|
|
} catch (e) {
|
|
setMsg({ type: "err", text: errorMessage(e) || "Could not save" });
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
const event = events.find(e => e.id === eventId);
|
|
|
|
return (
|
|
<div className="max-w-4xl mx-auto w-full p-6">
|
|
<div className="flex items-center gap-3 mb-4 print:hidden">
|
|
<div className="w-10 h-10 rounded-xl bg-brand-50 flex items-center justify-center shrink-0"><HandHeart 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">Volunteer rosters</h1>
|
|
<p className="text-sm text-gray-500">Who serves where, for an event or each session</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="text-sm block max-w-md print:hidden">
|
|
<label className="block text-xs text-gray-500 mb-1" htmlFor="volunteers-event">Event</label>
|
|
<EventPicker id="volunteers-event" className="w-full" events={events} value={eventId} onChange={setEventId} describe={e => (e.roles ? `${e.roles} role${e.roles === 1 ? "" : "s"}` : null)} />
|
|
</div>
|
|
|
|
{msg && <div role="status" className={`mt-3 p-3 rounded-lg text-sm print:hidden ${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>}
|
|
|
|
<div className="flex flex-wrap gap-1 mt-4 border-b print:hidden" role="tablist">
|
|
{TABS.map(t => (
|
|
<button key={t.id} role="tab" aria-selected={tab === t.id} onClick={() => setTab(t.id)} className={`px-3 py-2 text-sm -mb-px border-b-2 ${tab === t.id ? "border-brand-600 text-brand-700 font-medium" : "border-transparent text-gray-500"}`}>{t.label}</button>
|
|
))}
|
|
</div>
|
|
|
|
<div className="mt-4">
|
|
{tab !== "hours" && !eventId && <p className="text-sm text-gray-500">Choose an event.</p>}
|
|
{tab === "roster" && eventId && (
|
|
<p className="text-xs text-gray-500 mb-3 print:hidden">
|
|
Sign-up link to share: <span className="font-mono select-all break-all" data-testid="signup-link">{`${typeof window !== "undefined" ? window.location.origin : ""}/dashboard/user/serving?event=${eventId}`}</span>
|
|
</p>
|
|
)}
|
|
{tab === "roster" && eventId && <RosterSheet token={token} eventId={eventId} canEdit kidsRooms={settings?.kidsRooms || []} onMessage={setMsg} />}
|
|
{tab === "qr" && eventId && <CheckInQr token={token} eventId={eventId} title={event?.title || ""} />}
|
|
{tab === "ticket" && eventId && settings && (
|
|
<section className="border rounded-xl p-4 bg-white shadow-sm space-y-3">
|
|
<h2 className="font-semibold">Free volunteer ticket</h2>
|
|
<p className="text-sm text-gray-600">Off unless you choose a ticket. When it's on, everyone on this event's roster gets a free place on it (shown in their My Events), unless they already have a ticket. It's never offered to the public, and it's given back when they leave the roster.</p>
|
|
<div className="flex flex-wrap gap-2 items-center">
|
|
<select aria-label="Volunteer ticket" className="border rounded-lg px-3 py-2 text-sm" value={settings.event.volunteerOptionId || ""} disabled={busy}
|
|
onChange={e => saveTicket(e.target.value || null, e.target.value ? "Volunteers get a free place on this ticket." : "No free volunteer ticket.")}>
|
|
<option value="">Off — no free ticket</option>
|
|
{settings.options.map(o => <option key={o.id} value={o.id}>{o.name}{o.price ? ` (R${o.price})` : " (free)"}</option>)}
|
|
</select>
|
|
{!settings.event.volunteerOptionId && <button disabled={busy} onClick={() => saveTicket("new", "Added a free \"Volunteer\" ticket — volunteers get a place on it.")} className="px-3 py-2 rounded-lg border text-sm">Add a free "Volunteer" ticket</button>}
|
|
</div>
|
|
<p className="text-xs text-gray-500">Places already given stay when you switch it off.</p>
|
|
</section>
|
|
)}
|
|
{tab === "hours" && (
|
|
<section className="space-y-3">
|
|
<div className="flex gap-4 text-sm">
|
|
<label className="inline-flex items-center gap-1"><input type="radio" checked={hoursAll} onChange={() => setHoursAll(true)} /> All events</label>
|
|
<label className="inline-flex items-center gap-1"><input type="radio" checked={!hoursAll} disabled={!eventId} onChange={() => setHoursAll(false)} /> This event only</label>
|
|
</div>
|
|
<p className="text-xs text-gray-500">Shifts people checked in for, counted at the shift's length.</p>
|
|
{!hours && <div className="text-sm text-gray-500">Loading…</div>}
|
|
{hours && hours.length === 0 && <p className="text-sm text-gray-500">No checked-in shifts yet.</p>}
|
|
{hours && hours.length > 0 && (
|
|
<table className="w-full text-sm border rounded-xl overflow-hidden bg-white">
|
|
<thead className="bg-gray-50 text-left text-xs text-gray-500"><tr><th className="p-2">Volunteer</th><th className="p-2">Shifts</th><th className="p-2">Events</th><th className="p-2">Hours</th></tr></thead>
|
|
<tbody>{hours.map(h => <tr key={h.userId} className="border-t"><td className="p-2">{h.name}</td><td className="p-2">{h.shifts}</td><td className="p-2">{h.events}</td><td className="p-2 font-medium">{hoursText(h.hours)}</td></tr>)}</tbody>
|
|
</table>
|
|
)}
|
|
</section>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|