|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useTransition } from "react"; |
| 4 | +import { resendPurchaseConfirmationEmail } from "@/actions/purchase-email"; |
| 5 | +import { TurnstileField } from "@/components/molecules/TurnstileField"; |
| 6 | +import { isTurnstileClientEnabled } from "@/lib/turnstile"; |
| 7 | +import { cn } from "@/utils"; |
| 8 | + |
| 9 | +const turnstileEnabled = isTurnstileClientEnabled(); |
| 10 | + |
| 11 | +interface PurchaseEmailResendProps { |
| 12 | + sessionId: string; |
| 13 | + maskedEmail?: string | null; |
| 14 | +} |
| 15 | + |
| 16 | +export function PurchaseEmailResend({ |
| 17 | + sessionId, |
| 18 | + maskedEmail, |
| 19 | +}: PurchaseEmailResendProps) { |
| 20 | + const [feedback, setFeedback] = useState<{ |
| 21 | + type: "success" | "error"; |
| 22 | + text: string; |
| 23 | + } | null>(null); |
| 24 | + const [isPending, startTransition] = useTransition(); |
| 25 | + const [turnstileReady, setTurnstileReady] = useState(!turnstileEnabled); |
| 26 | + const [turnstileToken, setTurnstileToken] = useState(""); |
| 27 | + const [turnstileResetKey, setTurnstileResetKey] = useState(0); |
| 28 | + |
| 29 | + const bumpTurnstile = () => { |
| 30 | + setTurnstileReady(!turnstileEnabled); |
| 31 | + setTurnstileToken(""); |
| 32 | + setTurnstileResetKey((key) => key + 1); |
| 33 | + }; |
| 34 | + |
| 35 | + const handleResend = () => { |
| 36 | + setFeedback(null); |
| 37 | + startTransition(async () => { |
| 38 | + const result = await resendPurchaseConfirmationEmail( |
| 39 | + sessionId, |
| 40 | + turnstileToken || null, |
| 41 | + ); |
| 42 | + // Turnstile tokens are single-use — always refresh after an attempt. |
| 43 | + bumpTurnstile(); |
| 44 | + |
| 45 | + if (result.ok) { |
| 46 | + setFeedback({ type: "success", text: result.message }); |
| 47 | + } else { |
| 48 | + setFeedback({ type: "error", text: result.error }); |
| 49 | + } |
| 50 | + }); |
| 51 | + }; |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className="mt-8 rounded-none border border-zinc-100 bg-zinc-50 p-5 text-left"> |
| 55 | + <p className="text-sm font-medium text-zinc-800"> |
| 56 | + Didn't get the email? |
| 57 | + </p> |
| 58 | + <p className="mt-1 text-sm text-zinc-600"> |
| 59 | + {maskedEmail |
| 60 | + ? `We can email a fresh download link to ${maskedEmail}. Any older link will stop working. Check spam first.` |
| 61 | + : "We can email a fresh download link. Any older link will stop working. Check spam first."} |
| 62 | + </p> |
| 63 | + |
| 64 | + <TurnstileField |
| 65 | + resetKey={turnstileResetKey} |
| 66 | + onVerifiedChange={setTurnstileReady} |
| 67 | + onTokenChange={setTurnstileToken} |
| 68 | + /> |
| 69 | + |
| 70 | + <button |
| 71 | + type="button" |
| 72 | + onClick={handleResend} |
| 73 | + disabled={isPending || !turnstileReady} |
| 74 | + className={cn( |
| 75 | + "mt-4 inline-flex w-full items-center justify-center border border-zinc-300 bg-white px-5 py-2.5 text-sm font-medium tracking-wide text-zinc-800 uppercase transition-colors hover:bg-zinc-100 disabled:opacity-50 sm:w-auto", |
| 76 | + )} |
| 77 | + > |
| 78 | + {isPending ? ( |
| 79 | + <span className="h-4 w-4 animate-spin rounded-full border-2 border-zinc-300 border-t-zinc-800" /> |
| 80 | + ) : ( |
| 81 | + "Send new download link" |
| 82 | + )} |
| 83 | + </button> |
| 84 | + {feedback && ( |
| 85 | + <p |
| 86 | + className={cn( |
| 87 | + "mt-3 text-sm", |
| 88 | + feedback.type === "success" ? "text-emerald-700" : "text-red-600", |
| 89 | + )} |
| 90 | + role="status" |
| 91 | + > |
| 92 | + {feedback.text} |
| 93 | + </p> |
| 94 | + )} |
| 95 | + </div> |
| 96 | + ); |
| 97 | +} |
0 commit comments