mirror of
https://github.com/fergalmoran/supanextail.git
synced 2026-01-06 08:34:40 +00:00
Clean callback functions (replaced by async/await)
This commit is contained in:
@@ -6,36 +6,46 @@ If you want to change the email provider, don't hesitate to create a new api rou
|
|||||||
the axios.post here, line 18.
|
the axios.post here, line 18.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import axios from 'axios';
|
import axios, { AxiosError } from 'axios';
|
||||||
|
|
||||||
import { toast } from 'react-toastify';
|
import { toast } from 'react-toastify';
|
||||||
|
|
||||||
const sendEmail = (): void => {
|
const sendEmail = async (): Promise<void> => {
|
||||||
const name = (document.querySelector('#name') as HTMLInputElement).value;
|
const name = (document.querySelector('#name') as HTMLInputElement).value;
|
||||||
const email = (document.querySelector('#email') as HTMLInputElement).value;
|
const email = (document.querySelector('#email') as HTMLInputElement).value;
|
||||||
const message = (document.querySelector('#message') as HTMLInputElement)
|
const message = (document.querySelector('#message') as HTMLInputElement)
|
||||||
.value;
|
.value;
|
||||||
|
|
||||||
|
interface EmailData {
|
||||||
|
success: boolean;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ErrorAxios {
|
||||||
|
data: {
|
||||||
|
error: string;
|
||||||
|
success: boolean;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if (name && email && message) {
|
if (name && email && message) {
|
||||||
axios
|
try {
|
||||||
.post('/api/sendgrid', { email, name, message })
|
const mail = await axios.post<EmailData>('/api/sendgrid', {
|
||||||
.then(
|
email,
|
||||||
(result: {
|
name,
|
||||||
data: {
|
message,
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
|
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: unknown) {
|
||||||
|
const errorChecked = error as AxiosError;
|
||||||
|
const errorMessage = errorChecked.response as ErrorAxios;
|
||||||
|
toast.error(errorMessage.data.error);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
toast.info('Please fill all the fields ', {
|
toast.info('Please fill all the fields ', {
|
||||||
position: 'top-center',
|
position: 'top-center',
|
||||||
@@ -97,7 +107,7 @@ const Contact = (): JSX.Element => {
|
|||||||
className="btn btn-primary btn-sm"
|
className="btn btn-primary btn-sm"
|
||||||
onClick={(event) => {
|
onClick={(event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
sendEmail();
|
void sendEmail();
|
||||||
}}>
|
}}>
|
||||||
Submit{' '}
|
Submit{' '}
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -3,9 +3,10 @@ This is the form component to register an email adress to your mailing list.
|
|||||||
This is just the frontend, and the email will be send to our backend API (/api/mailingList)
|
This is just the frontend, and the email will be send to our backend API (/api/mailingList)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import axios, { AxiosError } from 'axios';
|
||||||
|
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
import Mailing from 'public/landing/mailing.svg';
|
import Mailing from 'public/landing/mailing.svg';
|
||||||
import axios from 'axios';
|
|
||||||
import { toast } from 'react-toastify';
|
import { toast } from 'react-toastify';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
|
||||||
@@ -14,6 +15,12 @@ const MailingList = (): JSX.Element => {
|
|||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [valid, setValid] = useState(true);
|
const [valid, setValid] = useState(true);
|
||||||
|
|
||||||
|
interface ErrorAxios {
|
||||||
|
data: {
|
||||||
|
error: string;
|
||||||
|
success: boolean;
|
||||||
|
};
|
||||||
|
}
|
||||||
const validateEmail = (): void => {
|
const validateEmail = (): void => {
|
||||||
// Regex patern for email validation
|
// Regex patern for email validation
|
||||||
const regex =
|
const regex =
|
||||||
@@ -21,7 +28,7 @@ const MailingList = (): JSX.Element => {
|
|||||||
|
|
||||||
if (regex.test(mail)) {
|
if (regex.test(mail)) {
|
||||||
// this is a valid email address
|
// this is a valid email address
|
||||||
subscribe();
|
void subscribe();
|
||||||
setValid(true);
|
setValid(true);
|
||||||
} else {
|
} else {
|
||||||
// invalid email.
|
// invalid email.
|
||||||
@@ -30,24 +37,28 @@ const MailingList = (): JSX.Element => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const subscribe = (): void => {
|
const subscribe = async (): Promise<void> => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
axios
|
|
||||||
.put('api/mailingList', {
|
try {
|
||||||
|
const data = await axios.put('api/mailingList', {
|
||||||
mail,
|
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);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (data.status === 200) {
|
||||||
|
toast.success('You have been added to our mailing list. Welcome 👋');
|
||||||
|
setLoading(false);
|
||||||
|
setMail('');
|
||||||
|
}
|
||||||
|
} catch (error: unknown) {
|
||||||
|
const errorChecked = error as AxiosError;
|
||||||
|
const errorMessage = errorChecked.response as ErrorAxios;
|
||||||
|
|
||||||
|
if (errorMessage.data.error) {
|
||||||
|
toast.error(errorMessage.data.error);
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col m-auto my-10 mt-24">
|
<div className="flex flex-col m-auto my-10 mt-24">
|
||||||
|
|||||||
@@ -21,15 +21,20 @@ const Pricing = (): JSX.Element => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (user) {
|
if (user) {
|
||||||
void getSub().then((result) => setSub(result));
|
const subFunction = async (): Promise<void> => {
|
||||||
void supabase
|
const sub = await getSub();
|
||||||
.from<definitions['subscriptions']>('subscriptions')
|
if (sub) {
|
||||||
.select(`customer_id`)
|
setSub(sub);
|
||||||
.eq('id', user.id)
|
}
|
||||||
.single()
|
const subSupa = await supabase
|
||||||
.then((result) => {
|
.from<definitions['subscriptions']>('subscriptions')
|
||||||
setCustomerId(result.data?.customer_id);
|
.select(`customer_id`)
|
||||||
});
|
.eq('id', user.id)
|
||||||
|
.single();
|
||||||
|
|
||||||
|
setCustomerId(subSupa.data?.customer_id);
|
||||||
|
};
|
||||||
|
void subFunction();
|
||||||
}
|
}
|
||||||
}, [user]);
|
}, [user]);
|
||||||
|
|
||||||
|
|||||||
@@ -24,11 +24,15 @@ const Login = ({ resetPassword, signIn }: LoginProperties): JSX.Element => {
|
|||||||
const [forgot, setForgot] = useState(false);
|
const [forgot, setForgot] = useState(false);
|
||||||
|
|
||||||
const resetPasswordLogin = async (): Promise<void> => {
|
const resetPasswordLogin = async (): Promise<void> => {
|
||||||
await resetPassword(email).then((result: { error: ApiError | null }) => {
|
const result = await resetPassword(email);
|
||||||
if (result.error) {
|
|
||||||
toast.error(result.error.message);
|
if (result.error) {
|
||||||
} else toast.success('Check your email to reset your password!');
|
toast.error(result.error.message);
|
||||||
});
|
} else if (result.data) {
|
||||||
|
toast.success(
|
||||||
|
'A password reset email has been sent to you, watch your mailbox!'
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const login = async (
|
const login = async (
|
||||||
@@ -37,17 +41,16 @@ const Login = ({ resetPassword, signIn }: LoginProperties): JSX.Element => {
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
// Handle the login. Go to the homepage if success or display an error.
|
// Handle the login. Go to the homepage if success or display an error.
|
||||||
await signIn({
|
const result = await signIn({
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
}).then((result) => {
|
|
||||||
if (result) {
|
|
||||||
void router.push('/');
|
|
||||||
}
|
|
||||||
if (result.error) {
|
|
||||||
toast.error(result.error.message);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (result.error) {
|
||||||
|
toast.error(result.error.message);
|
||||||
|
} else if (result.user) {
|
||||||
|
void router.push('/');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -32,21 +32,20 @@ const SignUpPanel = ({
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
// Handle the login. Go to the homepage if success or display an error.
|
// Handle the login. Go to the homepage if success or display an error.
|
||||||
await signUp({
|
const result = await signUp({
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
}).then((result) => {
|
|
||||||
console.log(result);
|
|
||||||
if (result.error) {
|
|
||||||
toast.error(result.error.message);
|
|
||||||
} else if (result.user?.confirmation_sent_at) {
|
|
||||||
toast.success(
|
|
||||||
'A confirmation email has been sent to you, watch your mailbox!'
|
|
||||||
);
|
|
||||||
} else if (result.user) {
|
|
||||||
void router.push('/');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (result.error) {
|
||||||
|
toast.error(result.error.message);
|
||||||
|
} else if (result.user?.confirmation_sent_at) {
|
||||||
|
toast.success(
|
||||||
|
'A confirmation email has been sent to you, watch your mailbox!'
|
||||||
|
);
|
||||||
|
} else if (result.user) {
|
||||||
|
void router.push('/');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -37,33 +37,30 @@ export default async function handler(
|
|||||||
await cors(request, response);
|
await cors(request, response);
|
||||||
await limiter(request, response);
|
await limiter(request, response);
|
||||||
if (request.method === 'PUT') {
|
if (request.method === 'PUT') {
|
||||||
axios
|
const result = await axios.put(
|
||||||
.put(
|
'https://api.sendgrid.com/v3/marketing/contacts',
|
||||||
'https://api.sendgrid.com/v3/marketing/contacts',
|
{
|
||||||
{
|
contacts: [{ email: `${request.body.mail}` }],
|
||||||
contacts: [{ email: `${request.body.mail}` }],
|
list_ids: [process.env.SENDGRID_MAILING_ID],
|
||||||
list_ids: [process.env.SENDGRID_MAILING_ID],
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'content-type': 'application/json',
|
||||||
|
Authorization: `Bearer ${process.env.SENDGRID_SECRET || ''}`,
|
||||||
},
|
},
|
||||||
{
|
}
|
||||||
headers: {
|
);
|
||||||
'content-type': 'application/json',
|
|
||||||
Authorization: `Bearer ${process.env.SENDGRID_SECRET || ''}`,
|
if (result.status === 200) {
|
||||||
},
|
response.status(200).json({
|
||||||
}
|
message:
|
||||||
)
|
'Your email has been succesfully added to the mailing list. Welcome 👋',
|
||||||
.then((result) => {
|
|
||||||
console.log(result);
|
|
||||||
response.status(200).send({
|
|
||||||
message:
|
|
||||||
'Your email has been succesfully added to the mailing list. Welcome 👋',
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
response.status(500).send({
|
|
||||||
message:
|
|
||||||
'Oups, there was a problem with your subscription, please try again or contact us',
|
|
||||||
error: error as string,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
response.status(500).json({
|
||||||
|
error:
|
||||||
|
'Oups, there was a problem with your subscription, please try again or contact us',
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,10 @@ interface Request extends NextApiRequest {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const sendGrid = (request: Request, response: NextApiResponse): void => {
|
const sendGrid = async (
|
||||||
|
request: Request,
|
||||||
|
response: NextApiResponse
|
||||||
|
): Promise<void> => {
|
||||||
if (request.method === 'POST') {
|
if (request.method === 'POST') {
|
||||||
sgMail.setApiKey(process.env.SENDGRID_SECRET || '');
|
sgMail.setApiKey(process.env.SENDGRID_SECRET || '');
|
||||||
|
|
||||||
@@ -29,20 +32,25 @@ const sendGrid = (request: Request, response: NextApiResponse): void => {
|
|||||||
reply_to: request.body.email,
|
reply_to: request.body.email,
|
||||||
};
|
};
|
||||||
|
|
||||||
sgMail
|
try {
|
||||||
.send(message)
|
const result = await sgMail.send(message);
|
||||||
.then(() => {
|
console.log(result);
|
||||||
response
|
if (result) {
|
||||||
.status(200)
|
response.status(200).json({
|
||||||
.send({ message: 'Your email has been sent', success: true });
|
message:
|
||||||
})
|
'Your message has been succesfully sent. Thank you for your feedback.',
|
||||||
.catch((error: string) => {
|
success: true,
|
||||||
console.error(error);
|
|
||||||
response.status(500).send({
|
|
||||||
message: 'There was an issue with your email... please retry',
|
|
||||||
error,
|
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
} catch (error: unknown) {
|
||||||
|
if (error) {
|
||||||
|
response.status(500).json({
|
||||||
|
error:
|
||||||
|
'Oups, there was a problem with your email, please try again or contact us',
|
||||||
|
success: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
export default sendGrid;
|
export default sendGrid;
|
||||||
|
|||||||
Reference in New Issue
Block a user