|
125 | 125 | class="tw-text-left tw-ml-2 tw-text-red-400 tw-text-sm tw-hidden" |
126 | 126 | ></div> |
127 | 127 |
|
| 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 | + |
128 | 142 | <!-- reCAPTCHA v2 Container - Hidden by default --> |
129 | 143 | <div |
130 | 144 | id="newsletter-recaptcha-container" |
|
145 | 159 | </div> |
146 | 160 | </div> |
147 | 161 | </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> |
160 | 162 | </div> |
161 | 163 |
|
162 | 164 | <p class="tw-text-xs tw-text-[rgba(255,255,255,0.6)]"> |
|
320 | 322 | } |
321 | 323 | } |
322 | 324 |
|
| 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 | + |
323 | 382 | // Submit form function (like Vue component) |
324 | 383 | function submitForm() { |
325 | 384 | 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 | + |
326 | 394 | const container = document.getElementById( |
327 | 395 | "newsletter-recaptcha-container" |
328 | 396 | ); |
|
335 | 403 |
|
336 | 404 | // Wait for DOM update, then render reCAPTCHA |
337 | 405 | 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 | + } |
340 | 422 | } |
341 | 423 | }, 100); |
342 | 424 | } |
|
352 | 434 | "Please complete the reCAPTCHA to proceed.", |
353 | 435 | "error" |
354 | 436 | ); |
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 | + } |
356 | 444 | subscribeBtn.disabled = false; |
357 | 445 | subscribeBtn.textContent = "Subscribe"; |
358 | 446 | return; |
|
373 | 461 | "Captcha validation failed. Please try again.", |
374 | 462 | "error" |
375 | 463 | ); |
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 | + } |
377 | 471 | subscribeBtn.disabled = false; |
378 | 472 | subscribeBtn.textContent = "Subscribe"; |
| 473 | + // Reset recaptcha for retry |
| 474 | + resetRecaptcha(); |
379 | 475 | return; |
380 | 476 | } |
381 | 477 |
|
|
392 | 488 | const form = document.getElementById("newsletter-form"); |
393 | 489 | if (form) form.reset(); |
394 | 490 |
|
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(); |
409 | 493 |
|
410 | 494 | // Analytics tracking |
411 | 495 | if ( |
|
433 | 517 | "error" |
434 | 518 | ); |
435 | 519 |
|
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(); |
450 | 522 |
|
451 | 523 | // GTM error tracking |
452 | 524 | window.dataLayer = window.dataLayer || []; |
|
513 | 585 |
|
514 | 586 | // Disable submit button to prevent multiple submissions |
515 | 587 | subscribeBtn.disabled = true; |
516 | | - subscribeBtn.textContent = "Processing..."; |
| 588 | + subscribeBtn.textContent = "Subscribing..."; |
517 | 589 |
|
518 | 590 | try { |
519 | 591 | // Show reCAPTCHA (like Vue component submitForm) |
520 | 592 | submitForm(); |
521 | 593 | } catch (error) { |
522 | 594 | showToastMessage("An error occurred. Please try again.", "error"); |
523 | 595 |
|
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 | + } |
525 | 603 | subscribeBtn.disabled = false; |
526 | 604 | subscribeBtn.textContent = "Subscribe"; |
527 | 605 | } |
|
0 commit comments