Elimina il falso successo del form contatti e mostra conferma invio. (#25)
Rimuove l honeypot autofillato, rifiuta mode non-live in produzione e TO su pro@nexstudio.ai. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@ -5,6 +5,7 @@ interface ContactFormCfg {
|
||||
turnstileSiteKey: string;
|
||||
notifySubject: string;
|
||||
successRedirect: string;
|
||||
successMessage: string;
|
||||
fieldName: string;
|
||||
fieldCompany: string;
|
||||
fieldCountry: string;
|
||||
@ -32,6 +33,7 @@ function readCfg(): ContactFormCfg {
|
||||
turnstileSiteKey: '',
|
||||
notifySubject: '',
|
||||
successRedirect: '',
|
||||
successMessage: 'Message sent.',
|
||||
fieldName: 'name',
|
||||
fieldCompany: 'company',
|
||||
fieldCountry: 'country',
|
||||
@ -53,12 +55,23 @@ function readCfg(): ContactFormCfg {
|
||||
}
|
||||
|
||||
function showError(message: string): void {
|
||||
const ok = document.getElementById('nx-contact-form-success');
|
||||
if (ok) ok.classList.add('hidden');
|
||||
const err = document.getElementById('nx-contact-form-error');
|
||||
if (!err) return;
|
||||
err.textContent = message;
|
||||
err.classList.remove('hidden');
|
||||
}
|
||||
|
||||
function showSuccess(message: string): void {
|
||||
const err = document.getElementById('nx-contact-form-error');
|
||||
if (err) err.classList.add('hidden');
|
||||
const ok = document.getElementById('nx-contact-form-success');
|
||||
if (!ok) return;
|
||||
ok.textContent = message;
|
||||
ok.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<HTMLElement>('.cf-turnstile');
|
||||
@ -89,7 +102,9 @@ function bind(): void {
|
||||
form.addEventListener('submit', async (e) => {
|
||||
const cfg = readCfg();
|
||||
const err = document.getElementById('nx-contact-form-error');
|
||||
const ok = document.getElementById('nx-contact-form-success');
|
||||
if (err) err.classList.add('hidden');
|
||||
if (ok) ok.classList.add('hidden');
|
||||
|
||||
const hasTurnstile =
|
||||
typeof cfg.turnstileSiteKey === 'string' && cfg.turnstileSiteKey.length > 0;
|
||||
@ -125,11 +140,12 @@ function bind(): void {
|
||||
if (hasTurnstile) resetTurnstile(form);
|
||||
return;
|
||||
}
|
||||
const target =
|
||||
(data && typeof data.redirect === 'string' && data.redirect) ||
|
||||
cfg.successRedirect ||
|
||||
`${window.location.pathname}${window.location.search}#contatti`;
|
||||
window.location.assign(target);
|
||||
form.reset();
|
||||
if (hasTurnstile) resetTurnstile(form);
|
||||
showSuccess(
|
||||
cfg.successMessage || 'Messaggio inviato. Ti risponderemo al più presto.',
|
||||
);
|
||||
form.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
||||
} catch {
|
||||
showError('Errore di rete. Controlla la connessione e riprova.');
|
||||
if (hasTurnstile) resetTurnstile(form);
|
||||
|
||||
Reference in New Issue
Block a user