Archived
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
105 lines
4.7 KiB
TypeScript
105 lines
4.7 KiB
TypeScript
"use client";
|
|
import React, { Suspense, useEffect, useState } from "react";
|
|
import Link from "next/link";
|
|
import { useSearchParams } from "next/navigation";
|
|
import { Navbar } from "@/components/layout/Navbar";
|
|
import { Footer } from "@/components/layout/Footer";
|
|
import { ApiError } from "@/lib/api";
|
|
import { MailX } from "lucide-react";
|
|
import { apiV2Data } from "@/lib/apiV2";
|
|
|
|
type Info =
|
|
| { kind: "account"; email: string; hasPhone: boolean; news: { email: boolean; whatsapp: boolean } }
|
|
| { kind: "address"; channel: "email" | "whatsapp"; address: string; optedOut: boolean };
|
|
|
|
/** Landing page for the unsubscribe link in News & announcements emails and WhatsApps. */
|
|
function Unsubscribe() {
|
|
const token = useSearchParams().get("t") || "";
|
|
const [info, setInfo] = useState<Info | null>(null);
|
|
const [error, setError] = useState<string | null>(token ? null : "This unsubscribe link is incomplete.");
|
|
const [busy, setBusy] = useState(false);
|
|
const [done, setDone] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!token) return;
|
|
(apiV2Data("get", "/unsubscribe/{token}", { params: { token: token } }) as Promise<Info>)
|
|
.then(setInfo)
|
|
.catch(e => setError(e instanceof ApiError ? e.message : "Couldn't load this link."));
|
|
}, [token]);
|
|
|
|
const unsubscribe = async (channels?: ("email" | "whatsapp")[]) => {
|
|
setBusy(true);
|
|
try {
|
|
await apiV2Data("post", "/unsubscribe/{token}", { params: { token: token }, body: channels ? { channels } : {} });
|
|
setDone(true);
|
|
} catch (e) {
|
|
setError(e instanceof ApiError ? e.message : "Couldn't unsubscribe. Please try again.");
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
const alreadyOff = info && (info.kind === "account" ? !info.news.email && !info.news.whatsapp : info.optedOut);
|
|
const btn = "px-4 py-2 rounded-lg text-sm font-medium disabled:opacity-50";
|
|
|
|
return (
|
|
<>
|
|
{error && <div className="border rounded-xl p-4 bg-red-50 text-red-800 text-sm">{error}</div>}
|
|
{!info && !error && <div className="text-sm text-gray-600">Loading…</div>}
|
|
{info && (
|
|
<div className="border rounded-xl p-5 bg-white shadow-sm space-y-4">
|
|
{done || alreadyOff ? (
|
|
<p className="text-sm text-gray-700">
|
|
You're unsubscribed from news and announcements{info.kind === "address" ? ` for ${info.address}` : ""}.
|
|
You'll still get messages about events you register for.
|
|
</p>
|
|
) : (
|
|
<>
|
|
<p className="text-sm text-gray-700">
|
|
Stop news and announcements to <strong>{info.kind === "account" ? info.email : info.address}</strong>?
|
|
Messages about events you register for — tickets, reminders, changes — still come through.
|
|
</p>
|
|
<div className="flex flex-wrap gap-3">
|
|
{info.kind === "account" && info.news.email && info.news.whatsapp ? (
|
|
<>
|
|
<button onClick={() => unsubscribe()} disabled={busy} className={`${btn} bg-brand-600 text-white hover:bg-brand-700`}>Unsubscribe from all news</button>
|
|
<button onClick={() => unsubscribe(["email"])} disabled={busy} className={`${btn} border border-gray-300 text-gray-700 hover:bg-gray-50`}>Email only</button>
|
|
<button onClick={() => unsubscribe(["whatsapp"])} disabled={busy} className={`${btn} border border-gray-300 text-gray-700 hover:bg-gray-50`}>WhatsApp only</button>
|
|
</>
|
|
) : (
|
|
<button onClick={() => unsubscribe()} disabled={busy} className={`${btn} bg-brand-600 text-white hover:bg-brand-700`}>{busy ? "Unsubscribing…" : "Unsubscribe"}</button>
|
|
)}
|
|
</div>
|
|
</>
|
|
)}
|
|
{info.kind === "account" && (
|
|
<p className="text-xs text-gray-500">
|
|
<Link href="/dashboard/user/privacy" className="text-brand-600 hover:underline">Log in to manage all your preferences</Link>
|
|
</p>
|
|
)}
|
|
</div>
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default function UnsubscribePage() {
|
|
return (
|
|
<div className="min-h-screen flex flex-col">
|
|
<Navbar />
|
|
<main className="flex-1 px-4 py-10 max-w-xl mx-auto w-full">
|
|
<div className="flex items-center gap-3 mb-4">
|
|
<div className="w-10 h-10 rounded-xl bg-brand-50 flex items-center justify-center shrink-0">
|
|
<MailX className="w-5 h-5 text-brand-600" />
|
|
</div>
|
|
<h1 className="text-2xl font-semibold text-gray-900">Unsubscribe</h1>
|
|
</div>
|
|
<Suspense fallback={<div className="text-sm text-gray-600">Loading…</div>}>
|
|
<Unsubscribe />
|
|
</Suspense>
|
|
</main>
|
|
<Footer />
|
|
</div>
|
|
);
|
|
}
|