This repository has been archived on 2026-11-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
hope-events/frontend/src/components/events/FollowSeriesButton.tsx
T
joshuaandClaude Opus 5.5 740897635f 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>
2026-10-28 10:29:59 +02:00

62 lines
2.6 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import { Bell, BellOff } from "lucide-react";
import { useAuth } from "@/hooks/useAuth";
import { apiFetch } from "@/lib/api";
import { errorMessage } from "@/lib/utils";
/** Follow / unfollow a series. Signed out, it links to the login page and back. */
export function FollowSeriesButton({ slug }: { slug: string }) {
const { user, token, loading } = useAuth();
const [following, setFollowing] = useState<boolean | null>(null);
const [seriesId, setSeriesId] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
// The page itself is cached for everyone, so whether this person follows (and the series as it
// is now) is asked separately.
useEffect(() => {
if (!token) return;
apiFetch<{ id: string; following: boolean }>(`/api/series/by-slug/${encodeURIComponent(slug)}`, { authToken: token })
.then(s => { setSeriesId(s.id); setFollowing(!!s.following); })
.catch(() => setFollowing(null));
}, [slug, token]);
if (loading) return null;
if (!user) {
return (
<a href={`/login?redirect=/series/${slug}`} className="inline-flex items-center gap-1.5 px-4 py-2 text-sm rounded-lg border bg-white hover:bg-gray-50">
<Bell className="w-4 h-4" /> Log in to follow
</a>
);
}
const toggle = async () => {
setBusy(true); setError(null);
try {
const res = await apiFetch<{ following: boolean }>(`/api/series/${seriesId}/follow`, { method: following ? "DELETE" : "POST", authToken: token || undefined });
setFollowing(res.following);
} catch (e) {
setError(errorMessage(e) || "Couldn't change that");
} finally {
setBusy(false);
}
};
return (
<div>
<button type="button" onClick={toggle} disabled={busy || following === null || !seriesId}
className={`inline-flex items-center gap-1.5 px-4 py-2 text-sm rounded-lg font-medium disabled:opacity-50 ${following ? "border bg-white text-gray-700 hover:bg-gray-50" : "bg-brand-600 text-white hover:bg-brand-700"}`}>
{following ? <><BellOff className="w-4 h-4" /> Unfollow</> : <><Bell className="w-4 h-4" /> Follow this series</>}
</button>
<p className="text-xs text-gray-500 mt-1.5" role="status">
{following
? "You'll get an email when a new event in this series is out (if you get news and announcements from us)."
: "Get an email when the next one is out."}
</p>
{error && <p className="text-xs text-red-600 mt-1">{error}</p>}
</div>
);
}