Skip to content

Commit f0f5cea

Browse files
feat: improve Brevo form UX and add Turnstile captcha
Refactor Brevo form button to show loader icon only when loading, hiding the text accordingly. Integrate Cloudflare Turnstile captcha and update callback logic to handle token storage and event dispatch. Load Brevo form script dynamically after DOM is ready and only if the form exists. Update error messages for better clarity. Signed-off-by: Dipankar Das <[email protected]>
1 parent d2cc3e7 commit f0f5cea

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

src/components/AppFooter.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ const isProduction = import.meta.env.PROD;
156156
form="sib-form"
157157
type="submit"
158158
>
159-
<svg class="icon clickable__icon progress-indicator__icon sib-hide-loader-icon w-5 h-5 animate-spin" viewBox="0 0 512 512">
159+
<svg class="icon clickable__icon progress-indicator__icon sib-hide-loader-icon hidden w-5 h-5 animate-spin" viewBox="0 0 512 512">
160160
<path d="M460.116 373.846l-20.823-12.022c-5.541-3.199-7.54-10.159-4.663-15.874 30.137-59.886 28.343-131.652-5.386-189.946-33.641-58.394-94.896-95.833-161.827-99.676C261.028 55.961 256 50.751 256 44.352V20.309c0-6.904 5.808-12.337 12.703-11.982 83.556 4.306 160.163 50.864 202.11 123.677 42.063 72.696 44.079 162.316 6.031 236.832-3.14 6.148-10.75 8.461-16.728 5.01z" />
161161
</svg>
162-
SUBSCRIBE
162+
<span class="sib-form-block__button-text">SUBSCRIBE</span>
163163
</button>
164164
</div>
165165
<input type="text" name="email_address_check" value="" class="input--hidden" style="display: none;">

src/layouts/Layout.astro

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,18 @@ const { title = "ksctl - Strive towards Fast, Efficient and Sustainable Computin
5151
color: #9ca3af;
5252
}
5353

54-
.sib-hide-loader-icon {
54+
.sib-form-block__button .sib-hide-loader-icon {
5555
display: none !important;
5656
}
57-
.sib-show-loader-icon {
57+
.sib-form-block__button .sib-show-loader-icon {
5858
display: inline-block !important;
5959
}
60+
.sib-form-block__button[data-loading="true"] .sib-hide-loader-icon {
61+
display: inline-block !important;
62+
}
63+
.sib-form-block__button[data-loading="true"] .sib-form-block__button-text {
64+
display: none !important;
65+
}
6066

6167
/* Style error and success messages */
6268
.sib-form-message-panel {
@@ -170,22 +176,28 @@ const { title = "ksctl - Strive towards Fast, Efficient and Sustainable Computin
170176
});
171177
</script>
172178

179+
<!-- Cloudflare Turnstile -->
180+
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
181+
173182
<!-- Brevo Form Scripts -->
174183
<script is:inline>
175-
// reCAPTCHA callback function
176-
function handleCaptchaResponse() {
184+
// Turnstile/reCAPTCHA callback function
185+
window.handleCaptchaResponse = function(token) {
186+
console.log('Captcha completed:', token);
177187
var event = new Event('captchaChange');
178188
var captchaElement = document.getElementById('sib-captcha');
179189
if (captchaElement) {
190+
// Store the token for Brevo form validation
191+
captchaElement.dataset.captchaToken = token;
180192
captchaElement.dispatchEvent(event);
181193
}
182-
}
194+
};
183195

184-
// Brevo form configuration
196+
// Brevo form configuration (must be set before main.js loads)
185197
window.REQUIRED_CODE_ERROR_MESSAGE = 'Please choose a country code';
186198
window.LOCALE = 'en';
187199
window.EMAIL_INVALID_MESSAGE = window.SMS_INVALID_MESSAGE = "The information provided is invalid. Please review the field format and try again.";
188-
window.REQUIRED_ERROR_MESSAGE = "This field cannot be left blank. ";
200+
window.REQUIRED_ERROR_MESSAGE = "Please complete the captcha verification.";
189201
window.GENERIC_INVALID_MESSAGE = "The information provided is invalid. Please review the field format and try again.";
190202
window.translation = {
191203
common: {
@@ -195,9 +207,24 @@ const { title = "ksctl - Strive towards Fast, Efficient and Sustainable Computin
195207
selectedOptions: '{quantity} selected',
196208
}
197209
};
198-
var AUTOHIDE = Boolean(0);
210+
window.AUTOHIDE = Boolean(0);
211+
212+
// Load Brevo script only after DOM is ready
213+
if (document.readyState === 'loading') {
214+
document.addEventListener('DOMContentLoaded', loadBrevoScript);
215+
} else {
216+
loadBrevoScript();
217+
}
218+
219+
function loadBrevoScript() {
220+
// Check if the form exists before loading the script
221+
if (document.getElementById('sib-form')) {
222+
var script = document.createElement('script');
223+
script.src = 'https://sibforms.com/forms/end-form/build/main.js';
224+
script.defer = true;
225+
document.body.appendChild(script);
226+
}
227+
}
199228
</script>
200-
<script defer src="https://sibforms.com/forms/end-form/build/main.js"></script>
201-
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
202229
</body>
203230
</html>

0 commit comments

Comments
 (0)