Event series (1.47.1)

Group separate events (Camp 2025, Camp 2026) on a series page at
/series/<slug> with upcoming and past events. Events join a series from
the event editor (copies keep it); a series can span campuses. Event
pages show "Part of ..." and "Next in this series". Signed-in people can
follow a series; the new `series` job emails followers once when a new
event in it goes live, as News & announcements (opted-in only, with
unsubscribe). Follows are in the data export and removed on
anonymising. Migration 20261028140000_event_series.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-28 10:29:59 +02:00
co-authored by Claude Opus 5.5
parent 7ae0b6d23b
commit 740897635f
38 changed files with 1176 additions and 34 deletions
@@ -0,0 +1,190 @@
"use client";
import React, { useCallback, useEffect, useState } from "react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { Layers, ExternalLink, Users } from "lucide-react";
import { useAuth } from "@/hooks/useAuth";
import { apiFetch } from "@/lib/api";
import { errorMessage } from "@/lib/utils";
import { BackLink } from "@/components/shared/BackLink";
import { CampusBadge } from "@/components/shared/CampusFilter";
import type { EventSeries } from "@/types";
type Draft = { id?: string; name: string; slug: string; description: string; picture: string };
const blank: Draft = { name: "", slug: "", description: "", picture: "" };
function SeriesForm({ draft, onChange, onSave, onCancel, saving }: { draft: Draft; onChange: (d: Draft) => void; onSave: () => void; onCancel: () => void; saving: boolean }) {
const upd = (patch: Partial<Draft>) => onChange({ ...draft, ...patch });
return (
<div className="border rounded-xl p-4 bg-gray-50 space-y-3" data-testid="series-form">
<div className="grid sm:grid-cols-2 gap-3">
<div>
<label className="block text-xs text-gray-600 mb-1" htmlFor="series-name">Name</label>
<input id="series-name" className="w-full border rounded px-3 py-2 text-sm" value={draft.name} onChange={e => upd({ name: e.target.value })} placeholder="e.g. Family Camp" />
</div>
<div>
<label className="block text-xs text-gray-600 mb-1" htmlFor="series-slug">Web address</label>
<div className="flex items-center text-sm">
<span className="text-gray-400 mr-1">/series/</span>
<input id="series-slug" className="w-full border rounded px-3 py-2 text-sm" value={draft.slug} onChange={e => upd({ slug: e.target.value })} placeholder="made from the name" />
</div>
</div>
</div>
<div>
<label className="block text-xs text-gray-600 mb-1" htmlFor="series-description">Description</label>
<textarea id="series-description" rows={3} className="w-full border rounded px-3 py-2 text-sm" value={draft.description} onChange={e => upd({ description: e.target.value })} placeholder="Shown at the top of the series page" />
</div>
<div>
<label className="block text-xs text-gray-600 mb-1" htmlFor="series-picture">Picture (web address, optional)</label>
<input id="series-picture" className="w-full border rounded px-3 py-2 text-sm" value={draft.picture} onChange={e => upd({ picture: e.target.value })} placeholder="https://…" />
</div>
<div className="flex gap-2">
<button type="button" onClick={onSave} disabled={saving || !draft.name.trim()} className="px-4 py-1.5 text-sm rounded bg-brand-600 text-white hover:bg-brand-700 disabled:opacity-50 font-medium">
{saving ? "Saving…" : draft.id ? "Save series" : "Create series"}
</button>
<button type="button" onClick={onCancel} className="px-3 py-1.5 text-sm rounded border bg-white hover:bg-gray-50">Cancel</button>
</div>
</div>
);
}
export default function SeriesPage() {
const { user, loading, token } = useAuth();
const router = useRouter();
const isAdmin = user?.role === "admin";
const canView = user?.role === "admin" || user?.role === "supervisor";
useEffect(() => {
if (!loading && !user) router.replace("/login");
}, [user, loading, router]);
const [list, setList] = useState<EventSeries[]>([]);
const [error, setError] = useState<string | null>(null);
const [notice, setNotice] = useState<string | null>(null);
const [draft, setDraft] = useState<Draft | null>(null);
const [saving, setSaving] = useState(false);
const load = useCallback(async () => {
if (!token) return;
try {
setList(await apiFetch<EventSeries[]>("/api/series", { authToken: token }));
} catch (e) {
setError(errorMessage(e) || "Couldn't load the series");
}
}, [token]);
useEffect(() => { load(); }, [load]);
const save = async () => {
if (!draft) return;
setSaving(true); setError(null); setNotice(null);
try {
const body = { name: draft.name, slug: draft.slug || undefined, description: draft.description, picture: draft.picture };
await apiFetch(draft.id ? `/api/series/${draft.id}` : "/api/series", { method: draft.id ? "PUT" : "POST", authToken: token || undefined, body });
setNotice(draft.id ? "Series saved." : `"${draft.name.trim()}" created — choose it in the event editor to add events.`);
setDraft(null);
await load();
} catch (e) {
setError(errorMessage(e) || "Couldn't save the series");
} finally {
setSaving(false);
}
};
const remove = async (s: EventSeries) => {
if (!window.confirm(`Delete "${s.name}"? Its ${s.eventCount} event${s.eventCount === 1 ? "" : "s"} stay, just without a series, and ${s.followers} follower${s.followers === 1 ? "" : "s"} won't hear about new ones.`)) return;
setError(null); setNotice(null);
try {
await apiFetch(`/api/series/${s.id}`, { method: "DELETE", authToken: token || undefined });
setNotice(`"${s.name}" deleted.`);
await load();
} catch (e) {
setError(errorMessage(e) || "Couldn't delete the series");
}
};
const now = Date.now();
return (
<div className="max-w-4xl mx-auto w-full p-6">
<div className="flex items-center justify-between mb-6 flex-wrap gap-3">
<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">
<Layers 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">Event series</h1>
<p className="text-sm text-gray-500 mt-0.5">Group events like every year&apos;s camp on one page</p>
</div>
</div>
{!draft && (
<button type="button" className="px-4 py-2 text-sm rounded-lg bg-brand-600 text-white hover:bg-brand-700 shadow-sm font-medium" onClick={() => { setDraft({ ...blank }); setNotice(null); }}>+ New series</button>
)}
</div>
{!canView && !loading && <div className="p-3 border rounded bg-yellow-50 text-yellow-800 text-sm mb-4">You need supervisor or admin access.</div>}
{error && <div className="p-3 mb-3 border rounded bg-red-50 text-red-700 text-sm">{error}</div>}
{notice && <div className="p-3 mb-3 border rounded bg-green-50 text-green-800 text-sm" role="status">{notice}</div>}
{draft && !draft.id && (
<div className="mb-4"><SeriesForm draft={draft} onChange={setDraft} onSave={save} onCancel={() => setDraft(null)} saving={saving} /></div>
)}
{list.length === 0 ? (
<p className="text-sm text-gray-500 border rounded-xl bg-white p-4">No series yet. A series groups separate events — Camp 2025, Camp 2026 — on one page, with &quot;Next in this series&quot; and followers.</p>
) : (
<ul className="space-y-3">
{list.map(s => (
<li key={s.id} className="border rounded-xl bg-white shadow-sm p-4" data-testid="series-row">
{draft?.id === s.id ? (
<SeriesForm draft={draft} onChange={setDraft} onSave={save} onCancel={() => setDraft(null)} saving={saving} />
) : (
<>
<div className="flex items-start justify-between gap-3 flex-wrap">
<div className="min-w-0">
<div className="font-medium text-gray-900">{s.name}</div>
<div className="text-xs text-gray-500 mt-0.5 flex items-center gap-3 flex-wrap">
<Link href={`/series/${s.slug}`} target="_blank" className="inline-flex items-center gap-1 text-brand-700 hover:underline">/series/{s.slug} <ExternalLink className="w-3 h-3" /></Link>
<span className="inline-flex items-center gap-1"><Users className="w-3 h-3" /> {s.followers} follower{s.followers === 1 ? "" : "s"}</span>
</div>
{s.description && <p className="text-sm text-gray-600 mt-1 whitespace-pre-line">{s.description}</p>}
</div>
<div className="flex gap-2 shrink-0">
<button type="button" className="text-xs px-3 py-1.5 rounded border bg-white hover:bg-gray-50"
onClick={() => { setDraft({ id: s.id, name: s.name, slug: s.slug, description: s.description || "", picture: s.picture || "" }); setNotice(null); }}>Edit</button>
{isAdmin && <button type="button" className="text-xs px-3 py-1.5 rounded bg-red-50 text-red-700 border border-red-200" onClick={() => remove(s)}>Delete</button>}
</div>
</div>
<div className="mt-3">
{s.events.length === 0 ? (
<p className="text-xs text-gray-500">No events yet — choose this series in the event editor (Basic details).</p>
) : (
<ul className="divide-y border rounded-lg">
{s.events.map(e => (
<li key={e.id} className="px-3 py-2 flex items-center justify-between gap-2 text-sm flex-wrap">
<span className="flex items-center gap-2 min-w-0">
<span className="truncate">{e.title}</span>
{!e.isActive && <span className="text-[10px] px-1.5 py-0.5 rounded bg-gray-100 text-gray-500">Inactive</span>}
{e.isHidden && <span className="text-[10px] px-1.5 py-0.5 rounded bg-gray-100 text-gray-500">Hidden</span>}
{new Date(e.endDate).getTime() < now && <span className="text-[10px] px-1.5 py-0.5 rounded bg-amber-50 text-amber-700">Past</span>}
<CampusBadge campus={e.campus} />
</span>
<span className="text-xs text-gray-500">{new Date(e.startDate).toLocaleDateString()}</span>
</li>
))}
</ul>
)}
{s.eventCount > s.events.length && (
<p className="text-[11px] text-gray-400 mt-1">{s.eventCount - s.events.length} more at other campuses.</p>
)}
</div>
</>
)}
</li>
))}
</ul>
)}
</div>
);
}