"use client"; import { useState, type FormEvent } from "react"; const fieldClass = "w-full rounded-2xl border border-anouma-taupe/30 bg-white px-4 py-2.5 text-sm text-anouma-plum placeholder:text-anouma-plum/50 focus:border-anouma-mauve-dark focus:outline-none"; async function callResend(email?: string): Promise<{ ok: boolean; error?: string }> { try { const res = await fetch("/api/auth/resend-verification", { method: "POST", credentials: "include", headers: { "Content-Type": "application/json" }, body: JSON.stringify(email ? { email } : {}), }); const data = await res.json().catch(() => ({})); if (!res.ok) return { ok: false, error: data.error || "Die Bestätigungs-E-Mail konnte nicht gesendet werden." }; return { ok: true }; } catch { return { ok: false, error: "Verbindung fehlgeschlagen." }; } } const GENERIC_SENT_MESSAGE = "Falls das Konto noch nicht bestätigt ist, wurde eine neue Bestätigungs-E-Mail gesendet."; /** * variant "session": a single button that resends for the currently logged-in * customer (used in /konto and the booking widget, where a session already * exists). * * variant "email-prompt": a toggle link that reveals an email field first * (used on /login, where the visitor may not be authenticated yet). */ export function ResendVerificationButton({ variant, triggerLabel = "E-Mail noch nicht bestätigt?", className, }: { variant: "session" | "email-prompt"; triggerLabel?: string; className?: string; }) { const [open, setOpen] = useState(false); const [status, setStatus] = useState<"idle" | "sending" | "sent" | "error">("idle"); const [error, setError] = useState(null); async function resendForSession() { setStatus("sending"); const result = await callResend(); if (result.ok) { setStatus("sent"); } else { setStatus("error"); setError(result.error ?? null); } } async function handleEmailSubmit(e: FormEvent) { e.preventDefault(); setStatus("sending"); setError(null); const email = String(new FormData(e.currentTarget).get("email") ?? ""); const result = await callResend(email); if (result.ok) { setStatus("sent"); } else { setStatus("error"); setError(result.error ?? null); } } if (variant === "session") { return (
{status === "sent" ? (

{GENERIC_SENT_MESSAGE}

) : ( )} {status === "error" && error && (

{error}

)}
); } return (
{!open ? ( ) : status === "sent" ? (

{GENERIC_SENT_MESSAGE}

) : (
)} {status === "error" && error && (

{error}

)}
); }