Skip to content

Commit 1e1a19c

Browse files
authored
fix: improve reCAPTCHA handling and update toast message styling (#190)
* fix: improve reCAPTCHA handling and update toast message styling * fix: remove unused newsletter toast element from footer
1 parent e64b6e0 commit 1e1a19c

File tree

1 file changed

+62
-21
lines changed

1 file changed

+62
-21
lines changed

overrides/partials/footer.html

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,6 @@
164164
updates from OpenObserve.
165165
</p>
166166
</form>
167-
<div
168-
id="newsletter-toast"
169-
class="tw-fixed tw-bottom-5 tw-right-2 tw-transform tw-bg-[#7782FF] tw-text-white tw-p-6 tw-rounded-lg tw-shadow-lg tw-z-50 tw-hidden tw-transition-opacity tw-duration-300"
170-
></div>
171167
</div>
172168

173169
<script>
@@ -306,7 +302,10 @@
306302
const recaptchaContainer = document.getElementById(
307303
"newsletter-recaptcha"
308304
);
309-
if (!recaptchaContainer || recaptchaWidgetId !== null) return;
305+
if (!recaptchaContainer) return;
306+
307+
// If widget already exists, don't create another one
308+
if (recaptchaWidgetId !== null) return;
310309

311310
try {
312311
recaptchaWidgetId = window.grecaptcha.render(recaptchaContainer, {
@@ -315,8 +314,7 @@
315314
"expired-callback": window.onRecaptchaExpired,
316315
theme: "dark",
317316
});
318-
} catch (error) {
319-
// Silent error handling
317+
} catch (error) {
320318
}
321319
}
322320

@@ -327,15 +325,25 @@
327325
"newsletter-recaptcha-container"
328326
);
329327
const recaptchaDiv = document.getElementById("newsletter-recaptcha");
328+
const loader = document.getElementById("newsletter-captcha-loader");
330329

331330
if (container) {
332331
container.classList.remove("tw-hidden");
333332
container.classList.add("tw-flex");
334333
}
335334

335+
// Hide loader when showing reCAPTCHA
336+
if (loader) {
337+
loader.style.display = "none";
338+
}
339+
336340
// Wait for DOM update, then render reCAPTCHA
337341
setTimeout(() => {
338-
if (recaptchaDiv && window.grecaptcha && !recaptchaWidgetId) {
342+
if (
343+
recaptchaDiv &&
344+
window.grecaptcha &&
345+
recaptchaWidgetId === null
346+
) {
339347
initializeRecaptcha();
340348
}
341349
}, 100);
@@ -352,7 +360,9 @@
352360
"Please complete the reCAPTCHA to proceed.",
353361
"error"
354362
);
355-
// Re-enable submit button
363+
// Show submit button again
364+
const buttonContainer = subscribeBtn.parentElement;
365+
buttonContainer.style.display = "block";
356366
subscribeBtn.disabled = false;
357367
subscribeBtn.textContent = "Subscribe";
358368
return;
@@ -373,7 +383,9 @@
373383
"Captcha validation failed. Please try again.",
374384
"error"
375385
);
376-
// Re-enable submit button
386+
// Show submit button again
387+
const buttonContainer = subscribeBtn.parentElement;
388+
buttonContainer.style.display = "block";
377389
subscribeBtn.disabled = false;
378390
subscribeBtn.textContent = "Subscribe";
379391
return;
@@ -384,7 +396,7 @@
384396

385397
// Success flow
386398
showToastMessage(
387-
`Successfully subscribed! Welcome to OpenObserve updates.`,
399+
`Subscribed successfully with: ${email}`,
388400
"success"
389401
);
390402

@@ -402,8 +414,20 @@
402414
container.classList.remove("tw-flex");
403415
}
404416

405-
if (window.grecaptcha && recaptchaWidgetId) {
406-
window.grecaptcha.reset(recaptchaWidgetId);
417+
// Completely destroy and recreate reCAPTCHA widget
418+
if (window.grecaptcha && recaptchaWidgetId !== null) {
419+
try {
420+
window.grecaptcha.reset(recaptchaWidgetId);
421+
// Clear the container completely
422+
const recaptchaDiv = document.getElementById(
423+
"newsletter-recaptcha"
424+
);
425+
if (recaptchaDiv) {
426+
recaptchaDiv.innerHTML = "";
427+
}
428+
recaptchaWidgetId = null;
429+
} catch (error) {
430+
}
407431
}
408432
recaptchaToken = "";
409433

@@ -443,8 +467,21 @@
443467
container.classList.remove("tw-flex");
444468
}
445469

446-
if (window.grecaptcha && recaptchaWidgetId) {
447-
window.grecaptcha.reset(recaptchaWidgetId);
470+
// Completely destroy and recreate reCAPTCHA widget on error
471+
if (window.grecaptcha && recaptchaWidgetId !== null) {
472+
try {
473+
window.grecaptcha.reset(recaptchaWidgetId);
474+
// Clear the container completely
475+
const recaptchaDiv = document.getElementById(
476+
"newsletter-recaptcha"
477+
);
478+
if (recaptchaDiv) {
479+
recaptchaDiv.innerHTML = "";
480+
}
481+
recaptchaWidgetId = null;
482+
} catch (error) {
483+
484+
}
448485
}
449486
recaptchaToken = "";
450487

@@ -459,7 +496,9 @@
459496
});
460497
} finally {
461498
isSubmitting = false;
462-
// Re-enable submit button
499+
// Show submit button again
500+
const buttonContainer = subscribeBtn.parentElement;
501+
buttonContainer.style.display = "block";
463502
subscribeBtn.disabled = false;
464503
subscribeBtn.textContent = "Subscribe";
465504
}
@@ -511,17 +550,19 @@
511550
return false; // Prevent further processing
512551
}
513552

514-
// Disable submit button to prevent multiple submissions
515-
subscribeBtn.disabled = true;
516-
subscribeBtn.textContent = "Processing...";
553+
// Hide submit button after click
554+
const buttonContainer = subscribeBtn.parentElement;
555+
buttonContainer.style.display = "none";
517556

518557
try {
519558
// Show reCAPTCHA (like Vue component submitForm)
520559
submitForm();
521560
} catch (error) {
522561
showToastMessage("An error occurred. Please try again.", "error");
523562

524-
// Re-enable submit button
563+
// Show submit button again on error
564+
const buttonContainer = subscribeBtn.parentElement;
565+
buttonContainer.style.display = "block";
525566
subscribeBtn.disabled = false;
526567
subscribeBtn.textContent = "Subscribe";
527568
}
@@ -535,7 +576,7 @@
535576
<!-- Toast -->
536577
<div
537578
id="newsletter-toast"
538-
class="tw-fixed tw-bottom-5 tw-right-2 tw-transform tw-bg-[#7782FF] tw-text-white tw-p-6 tw-rounded-lg tw-shadow-lg tw-z-50 tw-hidden tw-transition-opacity tw-duration-300"
579+
class="tw-fixed tw-bottom-5 tw-right-2 tw-transform tw-bg-[#7782FF] tw-text-white tw-p-6 tw-rounded-lg tw-shadow-lg tw-z-50 tw-hidden tw-transition-opacity tw-duration-300 tw-text-[15px]"
539580
></div>
540581

541582
<div class="tw-flex tw-justify-center lg:tw-justify-end">

0 commit comments

Comments
 (0)