From bdc679376dd433649aaf08a8394429f763c2274e Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Mon, 9 Sep 2024 15:56:37 +0200 Subject: [PATCH] fix: unsubscribe from the newsletter right after solving the CAPTCHA on the unsubscription form --- app/(root)/unsubscribe/page.tsx | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/app/(root)/unsubscribe/page.tsx b/app/(root)/unsubscribe/page.tsx index bd016ea..cd4712b 100644 --- a/app/(root)/unsubscribe/page.tsx +++ b/app/(root)/unsubscribe/page.tsx @@ -13,14 +13,13 @@ const UnsubscribePage = ({ const { toast } = useToast(); const [loading, setLoading] = useState(false); const [showCaptcha, setShowCaptcha] = useState(false); - const [captchaToken, setCaptchaToken] = useState(null); - const onSubmit = async () => { - if (!captchaToken) { - setShowCaptcha(true); - return; - } + 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", { @@ -33,7 +32,6 @@ const UnsubscribePage = ({ }); if (res.ok) { - setCaptchaToken(null); // Reset captcha token after successful submission toast({ description: "Unsubscribed successfully." }); @@ -55,11 +53,6 @@ const UnsubscribePage = ({ } }; - const handleCaptchaVerify = async (token: string) => { - setCaptchaToken(token); - await onSubmit(); // Trigger form submission after captcha is verified - }; - return (
{ e.preventDefault(); - onSubmit(); + setShowCaptcha(true); }} >