From e6e871a94b7f6ce3e4c84a873cf9ba106fcb88ac Mon Sep 17 00:00:00 2001 From: Willi Date: Mon, 27 Feb 2023 17:10:42 -0500 Subject: [PATCH] better errors and notifications --- .../sections/contact/form/index.tsx | 12 ++------ .../sections/partners/contact/index.tsx | 18 ++++++----- src/pages/api/mailchimpcontact.js | 23 +++++++------- src/pages/api/mailchimpinquiry.js | 30 +++++++++++-------- 4 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/components/sections/contact/form/index.tsx b/src/components/sections/contact/form/index.tsx index 529c6f1..4841b37 100644 --- a/src/components/sections/contact/form/index.tsx +++ b/src/components/sections/contact/form/index.tsx @@ -55,11 +55,7 @@ const CustomForm = ({ data }: FormProps) => { message: text, inquiry: selectedOption.value }) - }) - .then(() => { - setNotification("Thanks! We'll be in touch soon.") - }) - .catch((err) => console.error(err)) + }).catch((err) => console.error(err)) } const handleSubmitForm = useCallback( @@ -99,11 +95,7 @@ const CustomForm = ({ data }: FormProps) => { message: text, inquiry: selectedOption.value }) - }) - .then(() => { - setNotification("Thanks! We'll be in touch soon.") - }) - .catch((err) => console.error(err)) + }).catch((err) => console.error(err)) setIsSending(false) setIsFormSent(true) diff --git a/src/components/sections/partners/contact/index.tsx b/src/components/sections/partners/contact/index.tsx index 881145e..b42892f 100644 --- a/src/components/sections/partners/contact/index.tsx +++ b/src/components/sections/partners/contact/index.tsx @@ -74,11 +74,12 @@ const CustomForm = ({ data }: FormProps) => { inquiry: selectedOption.value, gReCaptchaToken: gReCaptchaToken }) + }).catch((err) => { + console.error(err) + setNotification( + 'Sorry, we couldnt send your message. Please try again later.' + ) }) - .then(() => { - setNotification("Thanks! We'll be in touch soon.") - }) - .catch((err) => console.error(err)) } const handleSubmitForm = useCallback( @@ -122,11 +123,12 @@ const CustomForm = ({ data }: FormProps) => { message: text, inquiry: selectedOption.value }) + }).catch((err) => { + console.error(err) + setNotification( + 'Sorry, we couldnt send your message. Please try again later.' + ) }) - .then(() => { - setNotification("Thanks! We'll be in touch soon.") - }) - .catch((err) => console.error(err)) setIsSending(false) setIsFormSent(true) diff --git a/src/pages/api/mailchimpcontact.js b/src/pages/api/mailchimpcontact.js index 971cc3c..7e6fa75 100644 --- a/src/pages/api/mailchimpcontact.js +++ b/src/pages/api/mailchimpcontact.js @@ -12,14 +12,17 @@ export default async function handler(req, res) { const message = body.message // eslint-disable-next-line no-console - const response = await mailchimp.lists.addListMember('5b23f4626d', { - email_address: email, // MERGE0 - status: 'subscribed', - merge_fields: { - INQUIRY: inquiry, // MERGE3 - MESSAGE: message // MERGE4 - } - }) - - res.status(200).json({ response }) + try { + const response = await mailchimp.lists.addListMember('5b23f4626d', { + email_address: email, // MERGE0 + status: 'subscribed', + merge_fields: { + INQUIRY: inquiry, // MERGE3 + MESSAGE: message // MERGE4 + } + }) + res.status(200).json({ response }) + } catch (err) { + res.status(500).json(err) + } } diff --git a/src/pages/api/mailchimpinquiry.js b/src/pages/api/mailchimpinquiry.js index 9500958..b309d6f 100644 --- a/src/pages/api/mailchimpinquiry.js +++ b/src/pages/api/mailchimpinquiry.js @@ -15,17 +15,21 @@ export default async function handler(req, res) { const role = body.role const company = body.company const jurisdiction = body.jurisdiction // JURIS - const response = await mailchimp.lists.addListMember('0c1f26e807', { - email_address: email, // MERGE0 - status: 'subscribed', - merge_fields: { - NAME: name, // MERGE1 - ROLE: role, // MERGE2 - COMPANY: company, // MERGE3 - JURIS: jurisdiction, // MERGE4 - INQUIRY: inquiry, // MERGE5 - MESSAGE: message // MERGE6 - } - }) - res.status(200).json({ name: response }) + try { + await mailchimp.lists.addListMember('0c1f26e807', { + email_address: email, // MERGE0 + status: 'subscribed', + merge_fields: { + NAME: name, // MERGE1 + ROLE: role, // MERGE2 + COMPANY: company, // MERGE3 + JURIS: jurisdiction, // MERGE4 + INQUIRY: inquiry, // MERGE5 + MESSAGE: message // MERGE6 + } + }) + res.status(200) + } catch (err) { + res.status(500).json(err) + } }