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

76 lines
3.5 KiB
TypeScript

"use client";
import React, { useEffect, useState } from "react";
import Link from "next/link";
import { ScrollText } from "lucide-react";
import { useAuth } from "@/hooks/useAuth";
import { errorMessage } from "@/lib/utils";
import type { PublicNotice } from "@/components/shared/PrivacyNoticeSection";
import { apiV2Data } from "@/lib/apiV2";
/**
* POPIA: asked once after login when this person never accepted the privacy notice (an account
* made for them, or from before acceptances were recorded) — reason "first" — or a version marked
* as a significant change came out after the one they accepted ("updated"). It can't be waved
* away, but the notice and the Privacy page (to delete the account instead) stay reachable.
*/
export function NoticePrompt({ reason = "updated", onDone }: { reason?: "first" | "updated"; onDone: () => void }) {
const { token } = useAuth();
const [notice, setNotice] = useState<PublicNotice | null>(null);
const [saving, setSaving] = useState(false);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
(apiV2Data("get", "/privacy/notice") as Promise<PublicNotice>).then(setNotice).catch(() => onDone());
}, [onDone]);
if (!notice) return null;
const accept = async () => {
if (!token) return;
setSaving(true);
setError(null);
try {
await apiV2Data("post", "/me/privacy/notice/accept", { authToken: token, body: {} });
onDone();
} catch (e) {
setError(errorMessage(e) || "Could not save. Please try again.");
} finally {
setSaving(false);
}
};
// The newest version that needed accepting, for "what changed".
const changes = reason === "updated" ? notice.versions.find(v => v.requiresAcceptance)?.changeSummary || notice.changeSummary : null;
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="notice-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">
<ScrollText className="w-5 h-5" />
</div>
<h2 id="notice-prompt-title" className="text-lg font-semibold text-gray-900">{reason === "first" ? "Our privacy notice" : <>We&apos;ve updated our privacy notice</>}</h2>
</div>
{changes && (
<div className="mb-3 p-3 rounded-lg bg-gray-50 border text-sm text-gray-700 whitespace-pre-line">
<span className="font-medium">What changed: </span>{changes}
</div>
)}
<p className="text-sm text-gray-600 mb-5">
{reason === "first" && <>It explains what we keep about you and why, and your rights. </>}
Please read the <Link href="/legal/privacy" target="_blank" className="text-brand-600 hover:underline">privacy notice</Link> and
accept it to carry on using your account. If you don&apos;t agree, you can{" "}
<Link href="/dashboard/user/privacy" className="text-brand-600 hover:underline">delete your account</Link> instead.
</p>
{error && <p className="text-sm text-red-600 mb-3">{error}</p>}
<div className="flex justify-end">
<button onClick={accept} 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…" : "I accept"}
</button>
</div>
</div>
</div>
);
}