Skip to content

Commit 095b584

Browse files
authored
fix: enhance newsletter form with submit button and reset functionality (#199)
1 parent d95d06b commit 095b584

File tree

1 file changed

+124
-46
lines changed

1 file changed

+124
-46
lines changed

overrides/partials/footer.html

Lines changed: 124 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@
125125
class="tw-text-left tw-ml-2 tw-text-red-400 tw-text-sm tw-hidden"
126126
></div>
127127

128+
<!-- Submit button container - can be hidden during subscription -->
129+
<div
130+
id="newsletter-submit-container"
131+
class="tw-px-6 tw-py-2 tw-rounded-full tw-bg-[#7782FF] tw-text-white tw-text-sm tw-font-medium hover:tw-bg-[#6672fa]"
132+
>
133+
<button
134+
type="submit"
135+
id="newsletter-submit"
136+
class="tw-transition active:tw-scale-98 tw-w-full tw-cursor-pointer"
137+
>
138+
Subscribe
139+
</button>
140+
</div>
141+
128142
<!-- reCAPTCHA v2 Container - Hidden by default -->
129143
<div
130144
id="newsletter-recaptcha-container"
@@ -145,18 +159,6 @@
145159
</div>
146160
</div>
147161
</div>
148-
149-
<div
150-
class="tw-px-6 tw-py-2 tw-rounded-full tw-bg-[#7782FF] tw-text-white tw-text-sm tw-font-medium hover:tw-bg-[#6672fa]"
151-
>
152-
<button
153-
type="submit"
154-
id="newsletter-submit"
155-
class="tw-transition active:tw-scale-98 tw-w-full tw-cursor-pointer"
156-
>
157-
Subscribe
158-
</button>
159-
</div>
160162
</div>
161163

162164
<p class="tw-text-xs tw-text-[rgba(255,255,255,0.6)]">
@@ -320,9 +322,75 @@
320322
}
321323
}
322324

325+
// Reset reCAPTCHA widget
326+
function resetRecaptcha() {
327+
if (window.grecaptcha && recaptchaWidgetId !== null) {
328+
try {
329+
window.grecaptcha.reset(recaptchaWidgetId);
330+
} catch (error) {
331+
// If reset fails, destroy and recreate the widget
332+
try {
333+
const recaptchaContainer = document.getElementById(
334+
"newsletter-recaptcha"
335+
);
336+
if (recaptchaContainer) {
337+
recaptchaContainer.innerHTML = "";
338+
recaptchaWidgetId = null;
339+
// Re-initialize after a short delay
340+
setTimeout(() => {
341+
initializeRecaptcha();
342+
}, 100);
343+
}
344+
} catch (recreateError) {
345+
// Silent error handling
346+
}
347+
}
348+
}
349+
recaptchaToken = "";
350+
}
351+
352+
// Reset the entire newsletter form state
353+
function resetNewsletterForm() {
354+
showCaptcha = false;
355+
356+
// Show submit button container again
357+
const submitContainer = document.getElementById(
358+
"newsletter-submit-container"
359+
);
360+
if (submitContainer) {
361+
submitContainer.classList.remove("tw-hidden");
362+
}
363+
364+
const container = document.getElementById(
365+
"newsletter-recaptcha-container"
366+
);
367+
if (container) {
368+
container.classList.add("tw-hidden");
369+
container.classList.remove("tw-flex");
370+
}
371+
372+
// Hide loader
373+
const loader = document.getElementById("newsletter-captcha-loader");
374+
if (loader) {
375+
loader.classList.add("tw-hidden");
376+
loader.classList.remove("tw-flex");
377+
}
378+
379+
resetRecaptcha();
380+
}
381+
323382
// Submit form function (like Vue component)
324383
function submitForm() {
325384
showCaptcha = true;
385+
386+
// Hide only submit button container during subscription process
387+
const submitContainer = document.getElementById(
388+
"newsletter-submit-container"
389+
);
390+
if (submitContainer) {
391+
submitContainer.classList.add("tw-hidden");
392+
}
393+
326394
const container = document.getElementById(
327395
"newsletter-recaptcha-container"
328396
);
@@ -335,8 +403,22 @@
335403

336404
// Wait for DOM update, then render reCAPTCHA
337405
setTimeout(() => {
338-
if (recaptchaDiv && window.grecaptcha && !recaptchaWidgetId) {
339-
initializeRecaptcha();
406+
if (recaptchaDiv && window.grecaptcha) {
407+
// If widget doesn't exist or is null, create it
408+
if (recaptchaWidgetId === null) {
409+
initializeRecaptcha();
410+
} else {
411+
// If widget exists, just reset it to allow new interaction
412+
try {
413+
window.grecaptcha.reset(recaptchaWidgetId);
414+
recaptchaToken = "";
415+
} catch (error) {
416+
// If reset fails, recreate the widget
417+
recaptchaDiv.innerHTML = "";
418+
recaptchaWidgetId = null;
419+
initializeRecaptcha();
420+
}
421+
}
340422
}
341423
}, 100);
342424
}
@@ -352,7 +434,13 @@
352434
"Please complete the reCAPTCHA to proceed.",
353435
"error"
354436
);
355-
// Re-enable submit button
437+
// Show submit button container again and re-enable submit button
438+
const submitContainer = document.getElementById(
439+
"newsletter-submit-container"
440+
);
441+
if (submitContainer) {
442+
submitContainer.classList.remove("tw-hidden");
443+
}
356444
subscribeBtn.disabled = false;
357445
subscribeBtn.textContent = "Subscribe";
358446
return;
@@ -373,9 +461,17 @@
373461
"Captcha validation failed. Please try again.",
374462
"error"
375463
);
376-
// Re-enable submit button
464+
// Show submit button container again and re-enable submit button
465+
const submitContainer = document.getElementById(
466+
"newsletter-submit-container"
467+
);
468+
if (submitContainer) {
469+
submitContainer.classList.remove("tw-hidden");
470+
}
377471
subscribeBtn.disabled = false;
378472
subscribeBtn.textContent = "Subscribe";
473+
// Reset recaptcha for retry
474+
resetRecaptcha();
379475
return;
380476
}
381477

@@ -392,20 +488,8 @@
392488
const form = document.getElementById("newsletter-form");
393489
if (form) form.reset();
394490

395-
// Hide reCAPTCHA and reset
396-
showCaptcha = false;
397-
const container = document.getElementById(
398-
"newsletter-recaptcha-container"
399-
);
400-
if (container) {
401-
container.classList.add("tw-hidden");
402-
container.classList.remove("tw-flex");
403-
}
404-
405-
if (window.grecaptcha && recaptchaWidgetId) {
406-
window.grecaptcha.reset(recaptchaWidgetId);
407-
}
408-
recaptchaToken = "";
491+
// Reset newsletter form completely for next submission
492+
resetNewsletterForm();
409493

410494
// Analytics tracking
411495
if (
@@ -433,20 +517,8 @@
433517
"error"
434518
);
435519

436-
// Reset reCAPTCHA on error
437-
showCaptcha = false;
438-
const container = document.getElementById(
439-
"newsletter-recaptcha-container"
440-
);
441-
if (container) {
442-
container.classList.add("tw-hidden");
443-
container.classList.remove("tw-flex");
444-
}
445-
446-
if (window.grecaptcha && recaptchaWidgetId) {
447-
window.grecaptcha.reset(recaptchaWidgetId);
448-
}
449-
recaptchaToken = "";
520+
// Reset newsletter form completely for retry
521+
resetNewsletterForm();
450522

451523
// GTM error tracking
452524
window.dataLayer = window.dataLayer || [];
@@ -513,15 +585,21 @@
513585

514586
// Disable submit button to prevent multiple submissions
515587
subscribeBtn.disabled = true;
516-
subscribeBtn.textContent = "Processing...";
588+
subscribeBtn.textContent = "Subscribing...";
517589

518590
try {
519591
// Show reCAPTCHA (like Vue component submitForm)
520592
submitForm();
521593
} catch (error) {
522594
showToastMessage("An error occurred. Please try again.", "error");
523595

524-
// Re-enable submit button
596+
// Show submit button container again and re-enable submit button
597+
const submitContainer = document.getElementById(
598+
"newsletter-submit-container"
599+
);
600+
if (submitContainer) {
601+
submitContainer.classList.remove("tw-hidden");
602+
}
525603
subscribeBtn.disabled = false;
526604
subscribeBtn.textContent = "Subscribe";
527605
}

0 commit comments

Comments
 (0)