Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix recaptcha completion & render #104

Merged
merged 1 commit into from
May 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/lib/components/Contact.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,22 @@
triggerToast('Please enter a valid email address', 'error');
return;
}
window.grecaptcha = window.grecaptcha || {};

const recaptchaResponse = window.document.getElementById('recaptchaResponse').value;
Copy link
Contributor

@Green-Ranger11 Green-Ranger11 May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null check via optional chain?

if (!recaptchaResponse) {
triggerToast('Please complete the ReCAPTCHA', 'error');
return;
}
const response = await fetch(formSpree, {
method: 'POST',
body: JSON.stringify({
firstname,
lastname,
email,
phone,
message
message,
'g-recaptcha-response': recaptchaResponse
}),
headers: {
Accept: 'application/json'
Expand All @@ -85,13 +93,22 @@
}
} catch (error) {
triggerToast('Oops! There was a problem submitting your form', 'error');
} finally {
if (window.grecaptcha) {
window.grecaptcha.reset();
}
}
}
</script>

<svelte:head>
{#if env !== 'development'}
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
{#if recaptchaSiteKey && env !== 'development'}
<script src="https://www.google.com/recaptcha/api.js"></script>
<script>
function recaptchaCallback(token) {
window.document.getElementById('recaptchaResponse').value = token;
}
</script>
{/if}
</svelte:head>
<section id={Section.Contact} class="px-3 py-5 rounded-none lg:py-10 lg:px-5 card bg-surface-400">
Expand Down Expand Up @@ -184,7 +201,12 @@
/>
</label>
{#if recaptchaSiteKey && env !== 'development'}
<div class="g-recaptcha" data-sitekey={recaptchaSiteKey}></div>
<div
class="g-recaptcha"
data-sitekey={recaptchaSiteKey}
data-callback="recaptchaCallback"
></div>
<input id="recaptchaResponse" name="recaptchaResponse" type="hidden" />
{/if}
<div class="mx-2 mt-2 mb-6 text-white bg-blue-500 rounded-xl md:justify-self-end btn">
<button type="submit"> Send message </button>
Expand Down
Loading