"use client"; import Newsletter from "@/components/shared/Newsletter"; import React, { Suspense, useState } from "react"; import { ReadonlyURLSearchParams, useSearchParams } from "next/navigation"; import { Button } from "@/components/ui/button"; import { useToast } from "@/components/ui/use-toast"; import HCaptcha from "@hcaptcha/react-hcaptcha"; const UnsubscribeForm = () => { const searchParams = useSearchParams() as ReadonlyURLSearchParams; const unsubscribeId = searchParams.has("id") ? searchParams.get("id") : undefined; const { toast } = useToast(); const [loading, setLoading] = useState(false); const [showCaptcha, setShowCaptcha] = useState(false); const handleCaptchaVerify = async (token: string) => { setShowCaptcha(false); await submit(token); // Trigger form submission after captcha is verified }; const submit = async (captchaToken: string) => { setLoading(true); try { const res = await fetch("/api/unsubscribe", { method: "POST", body: JSON.stringify({ unsubscribeId: unsubscribeId, captchaToken }), headers: { "Content-Type": "application/json", Accept: "application/json" } }); if (res.ok) { toast({ description: "Unsubscribed successfully." }); } else { toast({ title: "Uh oh! Something went wrong.", variant: "destructive" }); } } catch (error) { console.error(error); toast({ title: "Uh oh! Something went wrong.", variant: "destructive" }); } finally { setLoading(false); setShowCaptcha(false); // Hide captcha after submission attempt } }; return (
); }; const UnsubscribePage = () => { return (Are you sure to unsubscribe from the newsletter? You will no longer receive updates from the newsletter.