Archived
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
187 lines
11 KiB
TypeScript
187 lines
11 KiB
TypeScript
"use client";
|
|
|
|
import React, { useCallback, useEffect, useState } from "react";
|
|
import { useAuth } from "@/hooks/useAuth";
|
|
import { errorMessage } from "@/lib/utils";
|
|
import { formatDate } from "@/lib/date";
|
|
import { useDismissingState } from "@/hooks/useDismissingState";
|
|
import { BackLink } from "@/components/shared/BackLink";
|
|
import { CheckCircle2, HandHeart, QrCode, Repeat } from "lucide-react";
|
|
import type { CoverRequest, MySlot, Roster, ServingEvent } from "@/lib/volunteers";
|
|
import { apiV2Data } from "@/lib/apiV2";
|
|
|
|
type Mine = { slots: MySlot[]; cover: CoverRequest[]; events: ServingEvent[] };
|
|
type Msg = { type: "ok" | "err"; text: string } | null;
|
|
|
|
const btn = "px-3 py-1.5 rounded-lg text-sm border disabled:opacity-50";
|
|
|
|
/** My account → Serving: my shifts, cover requests I could take, and events looking for volunteers. */
|
|
export default function ServingPage() {
|
|
const { token } = useAuth();
|
|
const [data, setData] = useState<Mine | null>(null);
|
|
const [msg, setMsg] = useDismissingState<Msg>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
const [openEvent, setOpenEvent] = useState<string>("");
|
|
const [roster, setRoster] = useState<Roster | null>(null);
|
|
|
|
const load = useCallback(async () => {
|
|
if (!token) return;
|
|
try {
|
|
setData(await (apiV2Data("get", "/me/volunteering", { authToken: token }) as Promise<Mine>));
|
|
} catch (e) {
|
|
setMsg({ type: "err", text: errorMessage(e) || "Could not load your shifts" });
|
|
}
|
|
}, [token, setMsg]);
|
|
useEffect(() => { load(); }, [load]);
|
|
// ?event=<id>: the sign-up link a supervisor shares (works for hidden events too).
|
|
useEffect(() => {
|
|
const wanted = new URLSearchParams(window.location.search).get("event");
|
|
if (wanted) setOpenEvent(wanted);
|
|
}, []);
|
|
|
|
const loadRoster = useCallback(async (id: string) => {
|
|
if (!token || !id) { setRoster(null); return; }
|
|
try {
|
|
setRoster(await (apiV2Data("get", "/events/{id}/volunteer-roles", { params: { id: id }, authToken: token }) as Promise<Roster>));
|
|
} catch (e) {
|
|
setMsg({ type: "err", text: errorMessage(e) || "Could not load the roles" });
|
|
}
|
|
}, [token, setMsg]);
|
|
useEffect(() => { loadRoster(openEvent); }, [openEvent, loadRoster]);
|
|
|
|
const call = async (fn: () => Promise<unknown>, ok: string) => {
|
|
setBusy(true);
|
|
try {
|
|
await fn();
|
|
setMsg({ type: "ok", text: ok });
|
|
await Promise.all([load(), openEvent ? loadRoster(openEvent) : null]);
|
|
} catch (e) {
|
|
setMsg({ type: "err", text: errorMessage(e) || "Something went wrong" });
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
const askCover = (s: MySlot) => {
|
|
const note = prompt(`Ask the other volunteers to take over ${s.role.name}? The first to accept takes your place. Add a note (optional):`, "");
|
|
if (note === null) return;
|
|
call(() => apiV2Data("post", "/volunteer-slots/{id}/swap", { params: { id: s.slotId }, authToken: token, body: { note } }), "Cover requested — you stay on the roster until someone takes over.");
|
|
};
|
|
const withdraw = (s: MySlot) => confirm(`Withdraw from ${s.role.name} at ${s.event.title}? The organisers will be told.`)
|
|
&& call(() => apiV2Data("delete", "/volunteer-slots/{id}", { params: { id: s.slotId }, authToken: token }), "You're off the roster for this shift.");
|
|
|
|
const upcoming = (data?.slots || []).filter(s => !s.past);
|
|
const past = (data?.slots || []).filter(s => s.past);
|
|
|
|
return (
|
|
<div className="max-w-3xl mx-auto w-full p-6 space-y-6">
|
|
<div className="flex items-center gap-3">
|
|
<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/user" />
|
|
<h1 className="text-2xl font-semibold text-gray-900 mt-0.5">Serving</h1>
|
|
<p className="text-sm text-gray-500">Your volunteer shifts, and events looking for people to serve</p>
|
|
</div>
|
|
</div>
|
|
|
|
{msg && <div role="status" className={`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>}
|
|
{!data && <div className="text-sm text-gray-500">Loading…</div>}
|
|
|
|
{data && (
|
|
<section className="space-y-3">
|
|
<h2 className="font-semibold">My shifts</h2>
|
|
{upcoming.length === 0 && <p className="text-sm text-gray-500">You're not on any roster yet. Sign up for a role below.</p>}
|
|
{upcoming.map(s => (
|
|
<div key={s.slotId} className="border rounded-xl p-4 bg-white shadow-sm" data-testid="my-shift">
|
|
<div className="flex flex-wrap items-start justify-between gap-2">
|
|
<div>
|
|
<div className="font-semibold">{s.role.name} <span className="font-normal text-gray-500">· {s.event.title}</span></div>
|
|
<div className="text-sm text-gray-600">{s.role.sessionLabel ? `${s.role.sessionLabel} · ` : ""}{s.shift.text}{s.event.location ? ` · ${s.event.location}` : ""}</div>
|
|
{s.role.kidsRoom && <div className="text-xs text-gray-500">Kids room: {s.role.kidsRoom}</div>}
|
|
{s.role.description && <div className="text-xs text-gray-500 mt-1">{s.role.description}</div>}
|
|
{s.hasTicket && <div className="text-xs text-gray-500 mt-1">Your free volunteer ticket is in My Events.</div>}
|
|
</div>
|
|
{s.checkedInAt
|
|
? <span className="inline-flex items-center gap-1 text-sm text-green-700"><CheckCircle2 className="w-4 h-4" /> Checked in</span>
|
|
: s.swapId && <span className="text-xs px-2 py-0.5 rounded-full bg-amber-100 text-amber-800">Cover requested</span>}
|
|
</div>
|
|
{s.canCheckIn && <p className="mt-2 text-sm text-brand-700 flex items-center gap-1"><QrCode className="w-4 h-4" /> When you arrive, scan the volunteer check-in QR code with your phone's camera.</p>}
|
|
{!s.checkedInAt && (
|
|
<div className="mt-3 flex flex-wrap gap-2">
|
|
{s.swapId
|
|
? <button disabled={busy} onClick={() => call(() => apiV2Data("delete", "/volunteer-swaps/{id}", { params: { id: s.swapId! }, authToken: token }), "Cover request cancelled.")} className={btn}>I can make it after all</button>
|
|
: <button disabled={busy} onClick={() => askCover(s)} className={`${btn} inline-flex items-center gap-1`}><Repeat className="w-4 h-4" /> Can't make it</button>}
|
|
<button disabled={busy} onClick={() => withdraw(s)} className={`${btn} text-gray-600`}>Withdraw</button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
))}
|
|
{past.length > 0 && (
|
|
<details className="text-sm">
|
|
<summary className="cursor-pointer text-gray-500">Last week ({past.length})</summary>
|
|
<ul className="mt-1 space-y-0.5 text-gray-600">
|
|
{past.map(s => <li key={s.slotId}>{s.role.name} · {s.event.title} · {s.shift.text}{s.checkedInAt ? " · served" : ""}</li>)}
|
|
</ul>
|
|
</details>
|
|
)}
|
|
</section>
|
|
)}
|
|
|
|
{data && data.cover.length > 0 && (
|
|
<section className="space-y-3">
|
|
<h2 className="font-semibold">Can you take over?</h2>
|
|
{data.cover.map(c => (
|
|
<div key={c.swapId} className="border border-amber-300 rounded-xl p-4 bg-amber-50 flex flex-wrap items-center justify-between gap-3" data-testid="cover-request">
|
|
<div>
|
|
<div className="font-semibold">{c.role.name} <span className="font-normal text-gray-600">· {c.event.title}</span></div>
|
|
<div className="text-sm text-gray-700">{c.role.sessionLabel ? `${c.role.sessionLabel} · ` : ""}{c.shift.text}</div>
|
|
<div className="text-xs text-gray-600">{c.from || "A volunteer"} can't make it{c.note ? `: “${c.note}”` : ""}</div>
|
|
</div>
|
|
<button disabled={busy} onClick={() => call(() => apiV2Data("post", "/volunteer-swaps/{id}/accept", { params: { id: c.swapId }, authToken: token, body: {} }), `You've taken over ${c.role.name} — thank you!`)} className="px-3 py-1.5 rounded-lg text-sm bg-brand-600 text-white disabled:opacity-50">Take over</button>
|
|
</div>
|
|
))}
|
|
</section>
|
|
)}
|
|
|
|
{data && (
|
|
<section className="space-y-3">
|
|
<h2 className="font-semibold">Serve at an event</h2>
|
|
{data.events.length === 0 && !openEvent && <p className="text-sm text-gray-500">No events are looking for volunteers right now.</p>}
|
|
{(data.events.length > 0 || openEvent) && (
|
|
<label className="text-sm block max-w-md">
|
|
<span className="block text-xs text-gray-500 mb-1">Event</span>
|
|
<select className="w-full border rounded-lg px-3 py-2 text-sm" value={openEvent} onChange={e => setOpenEvent(e.target.value)}>
|
|
<option value="">Choose an event…</option>
|
|
{roster && !data.events.some(e => e.id === roster.event.id) && <option value={roster.event.id}>{roster.event.title} · {formatDate(new Date(roster.event.startDate))}</option>}
|
|
{data.events.map(e => <option key={e.id} value={e.id}>{e.title} · {formatDate(new Date(e.startDate))} ({e.roles} role{e.roles === 1 ? "" : "s"})</option>)}
|
|
</select>
|
|
</label>
|
|
)}
|
|
{roster && roster.event.id === openEvent && (
|
|
<div className="space-y-2">
|
|
{roster.roles.filter(r => !r.past).map(r => (
|
|
<div key={r.id} className="border rounded-xl p-3 bg-white flex flex-wrap items-center justify-between gap-3" data-testid="open-role">
|
|
<div>
|
|
<div className="font-medium">{r.name}{r.kidsRoom ? <span className="text-xs text-gray-500"> · kids room {r.kidsRoom.name}</span> : null}</div>
|
|
<div className="text-sm text-gray-600">{r.sessionLabel ? `${r.sessionLabel} · ` : ""}{r.shift.text}</div>
|
|
{r.description && <div className="text-xs text-gray-500">{r.description}</div>}
|
|
<div className="text-xs text-gray-500">{r.filled} of {r.needed} signed up</div>
|
|
</div>
|
|
{r.mine
|
|
? <span className="inline-flex items-center gap-1 text-sm text-green-700"><CheckCircle2 className="w-4 h-4" /> You're serving</span>
|
|
: r.coverNeeded.length > 0
|
|
? <button disabled={busy} onClick={() => call(() => apiV2Data("post", "/volunteer-swaps/{id}/accept", { params: { id: r.coverNeeded[0].swapId }, authToken: token, body: {} }), `You've taken over ${r.name} — thank you!`)} className="px-3 py-1.5 rounded-lg text-sm bg-brand-600 text-white disabled:opacity-50">Take over a place</button>
|
|
: r.full
|
|
? <span className="text-sm text-gray-500">Full</span>
|
|
: <button disabled={busy} onClick={() => call(() => apiV2Data("post", "/volunteer-roles/{id}/sign-up", { params: { id: r.id }, authToken: token, body: {} }), `Thank you — you're on the roster for ${r.name}.`)} className="px-3 py-1.5 rounded-lg text-sm bg-brand-600 text-white disabled:opacity-50">Sign up</button>}
|
|
</div>
|
|
))}
|
|
{roster.roles.every(r => r.past) && <p className="text-sm text-gray-500">All shifts at this event are over.</p>}
|
|
</div>
|
|
)}
|
|
</section>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|