From a48a2c7f7ea1ac650ce1817af704bdf5138a9e73 Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Tue, 3 May 2022 15:57:42 -0600 Subject: Add contact page --- src/routes/contact/index.svelte | 128 ++++++++++++++++++++++++++++++++++++++-- src/routes/contact/submit.js | 69 ++++++++++++++++++++++ 2 files changed, 193 insertions(+), 4 deletions(-) create mode 100644 src/routes/contact/submit.js (limited to 'src/routes/contact') diff --git a/src/routes/contact/index.svelte b/src/routes/contact/index.svelte index cbedec9..84d9621 100644 --- a/src/routes/contact/index.svelte +++ b/src/routes/contact/index.svelte @@ -1,8 +1,128 @@ -
-
+ -
+ let captcha; + + function handleSubmit (e) { + e.preventDefault(); + + if (browser) { + const sendToast = toast.push('Sending...', { + duration: 300, + initial: 0, + next: 0.2, + dismissable: false + }); + fetch('/contact/submit', { + method: "POST", + body: JSON.stringify(submission) + }) + .then((x) => x.json()) + .then((x) => { + toast.set(sendToast, { next: 1 }); + + if (x.success) { + toast.push('Success! Reloading...', { + theme: { + '--toastBackground': '#48BB78', + '--toastBarBackground': '#2F855A' + }, + duration: 1000, + onpop: () => { window.location.reload(); }, + }); + } else if (x.error) { + toast.push(x.error, { + theme: { + '--toastBackground': '#F56565', + '--toastBarBackground': '#C53030' + } + }); + } + }) + .catch((err) => console.log(err)); + } + } + function onCaptchaError () { + toast.push('Failed to verify captcha, try again.', { + theme: { + '--toastBackground': '#F56565', + '--toastBarBackground': '#C53030' + } + }); + captcha.reset(); + } + + function onCaptchaSuccess ({ detail: { token } }) { + submission.captchaToken = token; + } + + +
+

Let's get in touch

+
+ +
+

+

+ Call or text +
+ (530) 638 - 3546 +

+
+
+

+

+ Email +
+ jeffer@mistymountainstherapy.com +

+
+
+
+

Or send us a message

+
+
+
+ + +
+
+ + +
+
+
+ + +
+
+ +
+ +
+
+
diff --git a/src/routes/contact/submit.js b/src/routes/contact/submit.js new file mode 100644 index 0000000..cf0d65c --- /dev/null +++ b/src/routes/contact/submit.js @@ -0,0 +1,69 @@ +import 'dotenv/config'; +import sgMail from '@sendgrid/mail'; +sgMail.setApiKey(process.env.SENDGRID_API_KEY); + +export async function get() { + const items = [ + {a: 1, b: 2}, + {a: 3, b: 4}, + {a: 5, b: 6}, + ]; + return { + body: items + }; +} + +export async function post({ request }) { + const body = await request.json(); + const { HCAPTCHA_SECRET, FORM_FROM_EMAIL, FORM_TO_EMAIL } = process.env; + + const captchaVerified = await fetch(`https://hcaptcha.com/siteverify?response=${body.captchaToken}&secret=${HCAPTCHA_SECRET}`, { + method: 'POST', + }) + .then((res) => res.json()) + .then((json) => json.success) + .catch(() => false); + + if (!captchaVerified) { + return { + statusCode: 400, + body: { + error: 'Captcha verification failed', + }, + }; + } + + const msg = { + to: FORM_TO_EMAIL, + from: FORM_FROM_EMAIL, + subject: `Form Submission from ${body.name}`, + text: ` + Name: ${body.name} + Email: ${body.email} + Message: ${body.message} + `, + }; + + const messageSent = await sgMail + .send(msg) + .then(() => true) + .catch((error) => { + console.error(error); + return false; + }); + + if (!messageSent) { + return { + statusCode: 500, + body: { + error: 'Message could not be sent', + }, + }; + } + + return { + body: { + success: true, + }, + }; +} -- cgit v1.2.3-70-g09d2