diff --git a/components/Contact.tsx b/components/Contact.tsx index c1521f4..86b2302 100644 --- a/components/Contact.tsx +++ b/components/Contact.tsx @@ -9,33 +9,33 @@ the axios.post here, line 18. import axios from 'axios'; import { toast } from 'react-toastify'; -const sendEmail = (): void => { +const sendEmail = async (): Promise => { const name = (document.querySelector('#name') as HTMLInputElement).value; const email = (document.querySelector('#email') as HTMLInputElement).value; const message = (document.querySelector('#message') as HTMLInputElement) .value; + interface EmailData { + success: boolean; + message: string; + } + if (name && email && message) { - axios - .post('/api/sendgrid', { email, name, message }) - .then( - (result: { - data: { - success: boolean; - message: string; - }; - }) => { - if (result.data.success === true) { - toast.success(result.data.message); - (document.querySelector('#name') as HTMLInputElement).value = ''; - (document.querySelector('#email') as HTMLInputElement).value = ''; - (document.querySelector('#message') as HTMLInputElement).value = ''; - } - } - ) - .catch((error) => { - console.log(error); + try { + const mail = await axios.post('/api/sendgrid', { + email, + name, + message, }); + if (mail.data.success === true) { + toast.success(mail.data.message); + (document.querySelector('#name') as HTMLInputElement).value = ''; + (document.querySelector('#email') as HTMLInputElement).value = ''; + (document.querySelector('#message') as HTMLInputElement).value = ''; + } + } catch (error) { + console.log(error); + } } else { toast.info('Please fill all the fields ', { position: 'top-center', @@ -97,7 +97,7 @@ const Contact = (): JSX.Element => { className="btn btn-primary btn-sm" onClick={(event) => { event.preventDefault(); - sendEmail(); + void sendEmail(); }}> Submit{' '} diff --git a/components/MailingList.tsx b/components/MailingList.tsx index a029011..b8b197e 100644 --- a/components/MailingList.tsx +++ b/components/MailingList.tsx @@ -21,7 +21,7 @@ const MailingList = (): JSX.Element => { if (regex.test(mail)) { // this is a valid email address - subscribe(); + void subscribe(); setValid(true); } else { // invalid email. @@ -30,24 +30,21 @@ const MailingList = (): JSX.Element => { } }; - const subscribe = (): void => { + const subscribe = async (): Promise => { setLoading(true); - axios - .put('api/mailingList', { - mail, - }) - .then((result) => { - if (result.status === 200) { - toast.success( - 'Your email has been succesfully added to the mailing list. Welcome 👋' - ); - setLoading(false); - } - }) - .catch((error) => { - console.log(error); - setLoading(false); - }); + + const data = await axios.put('api/mailingList', { + mail, + }); + + if (data.status === 200) { + toast.success('You have been added to our mailing list. Welcome 👋'); + setLoading(false); + setMail(''); + } else { + toast.error('Something went wrong'); + setLoading(false); + } }; return (
diff --git a/components/Pricing.tsx b/components/Pricing.tsx index c791ea6..cd6499b 100644 --- a/components/Pricing.tsx +++ b/components/Pricing.tsx @@ -21,15 +21,20 @@ const Pricing = (): JSX.Element => { useEffect(() => { if (user) { - void getSub().then((result) => setSub(result)); - void supabase - .from('subscriptions') - .select(`customer_id`) - .eq('id', user.id) - .single() - .then((result) => { - setCustomerId(result.data?.customer_id); - }); + const subFunction = async (): Promise => { + const sub = await getSub(); + if (sub) { + setSub(sub); + } + const subSupa = await supabase + .from('subscriptions') + .select(`customer_id`) + .eq('id', user.id) + .single(); + + setCustomerId(subSupa.data?.customer_id); + }; + void subFunction(); } }, [user]);