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/shared/NewsPrompt.tsx
T

88 lines
4.0 KiB
TypeScript

"use client";
import React, { useEffect, useState } from "react";
import Link from "next/link";
import { Megaphone } from "lucide-react";
import { useAuth } from "@/hooks/useAuth";
import { errorMessage } from "@/lib/utils";
import { apiV2Data } from "@/lib/apiV2";
type News = { email: boolean; whatsapp: boolean };
/**
* POPIA: asks once, after login, whether the person wants News & announcements — for
* accounts that never answered (existing accounts, ones made at the door). Their current
* setting is pre-filled; saving records the answer so it isn't asked again.
*/
export function NewsPrompt() {
const { user, token } = useAuth();
const [news, setNews] = useState<News | null>(null);
const [canWhatsApp, setCanWhatsApp] = useState(false);
const [done, setDone] = useState(false);
const [saving, setSaving] = useState(false);
const [error, setError] = useState<string | null>(null);
const pending = !!user?.newsPromptPending && !done;
useEffect(() => {
if (!pending || !token || news) return;
(apiV2Data("get", "/me/privacy", { authToken: token }) as Promise<{ news: News; canWhatsApp: boolean }>)
.then(res => { setNews(res.news); setCanWhatsApp(res.canWhatsApp); })
.catch(() => setDone(true)); // never block the dashboard over this
}, [pending, token, news]);
if (!pending || !news) return null;
const save = async (choice: News) => {
if (!token) return;
setSaving(true);
setError(null);
try {
await apiV2Data("put", "/me/privacy/news", { authToken: token, body: { ...choice, source: "prompt" } });
setDone(true);
} catch (e) {
setError(errorMessage(e) || "Could not save your choice.");
} finally {
setSaving(false);
}
};
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4" role="dialog" aria-modal="true" aria-labelledby="news-prompt-title">
<div className="w-full max-w-md rounded-xl bg-white p-6 shadow-xl">
<div className="flex items-center gap-2 mb-3">
<div className="w-9 h-9 rounded-lg flex items-center justify-center bg-brand-50 text-brand-600 shrink-0">
<Megaphone className="w-5 h-5" />
</div>
<h2 id="news-prompt-title" className="text-lg font-semibold text-gray-900">Hear about upcoming events?</h2>
</div>
<p className="text-sm text-gray-600 mb-4">
We&apos;d like to send you occasional news and announcements. Messages about events you register for are
sent either way. You can change this any time under <Link href="/dashboard/user/privacy" className="text-brand-600 hover:underline">Privacy</Link>.
</p>
<div className="space-y-3 mb-5">
<label className="flex items-center gap-3 text-sm text-gray-700 cursor-pointer">
<input type="checkbox" className="h-4 w-4 accent-brand-600" checked={news.email} onChange={e => setNews({ ...news, email: e.target.checked })} />
By email
</label>
{canWhatsApp && (
<label className="flex items-center gap-3 text-sm text-gray-700 cursor-pointer">
<input type="checkbox" className="h-4 w-4 accent-brand-600" checked={news.whatsapp} onChange={e => setNews({ ...news, whatsapp: e.target.checked })} />
By WhatsApp
</label>
)}
</div>
{error && <p className="text-sm text-red-600 mb-3">{error}</p>}
<div className="flex flex-wrap justify-end gap-3">
<button onClick={() => save({ email: false, whatsapp: false })} disabled={saving} className="px-4 py-2 rounded-lg bg-gray-100 text-sm font-medium hover:bg-gray-200 disabled:opacity-50">
No thanks
</button>
<button onClick={() => save({ email: news.email, whatsapp: canWhatsApp && news.whatsapp })} disabled={saving} className="px-4 py-2 rounded-lg bg-brand-600 text-white text-sm font-medium hover:bg-brand-700 disabled:opacity-50">
{saving ? "Saving…" : "Save my choice"}
</button>
</div>
</div>
</div>
);
}