diff --git a/src/scripts/nx-contact-form.ts b/src/scripts/nx-contact-form.ts index 2c8c9f7..cfa2808 100644 --- a/src/scripts/nx-contact-form.ts +++ b/src/scripts/nx-contact-form.ts @@ -59,6 +59,29 @@ function showError(message: string): void { err.classList.remove('hidden'); } +/** Dopo un POST fallito il token Turnstile è già consumato: ne serve uno nuovo. */ +function resetTurnstile(form: HTMLFormElement): void { + const widget = form.querySelector('.cf-turnstile'); + const turnstile = ( + window as Window & { + turnstile?: { reset: (target?: string | HTMLElement) => void }; + } + ).turnstile; + if (turnstile?.reset) { + try { + if (widget) turnstile.reset(widget); + else turnstile.reset(); + return; + } catch { + // fall through + } + } + const tokenInput = form.querySelector( + 'input[name="cf-turnstile-response"]', + ); + if (tokenInput) tokenInput.value = ''; +} + function bind(): void { const form = document.getElementById('nx-contact-form') as HTMLFormElement | null; if (!form) return; @@ -99,6 +122,7 @@ function bind(): void { (data && typeof data.error === 'string' && data.error) || 'Invio non riuscito. Riprova tra poco.', ); + if (hasTurnstile) resetTurnstile(form); return; } const target = @@ -108,6 +132,7 @@ function bind(): void { window.location.assign(target); } catch { showError('Errore di rete. Controlla la connessione e riprova.'); + if (hasTurnstile) resetTurnstile(form); } finally { if (submitBtn) submitBtn.disabled = false; }