"use client"; import Newsletter from "@/components/shared/Newsletter"; import React, { useState, use } from "react"; import { Button } from "@/components/ui/button"; import { useToast } from "@/components/ui/use-toast"; import HCaptcha from "@hcaptcha/react-hcaptcha"; const UnsubscribePage = (props: { searchParams: Promise<{ id: string | undefined }>; }) => { const searchParams = use(props.searchParams); 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: searchParams.id, 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 (

Unsubscribe from newsletter

Are you sure to unsubscribe from the newsletter? You will no longer receive updates from the newsletter.

{ e.preventDefault(); setShowCaptcha(true); }} > {showCaptcha && ( )}
); }; export default UnsubscribePage;