personal-site/functions/frontend/submitContactForm.ts

41 lines
933 B
TypeScript
Raw Normal View History

2023-07-20 20:21:46 +00:00
import { Dispatch, SetStateAction } from "react";
export default async function submitContactForm(e: any, setSuccess: Dispatch<SetStateAction<string | null>>, setLoading: Dispatch<SetStateAction<boolean>>) {
2022-01-06 08:05:14 +00:00
e.preventDefault();
2023-07-20 20:21:46 +00:00
setLoading(true);
2022-01-06 08:05:14 +00:00
let name = e.target[0].value;
let email = e.target[1].value;
let message = e.target[2].value;
let body = {
name: name,
email: email,
message: message,
};
let res = await fetch("/api/contactForm", {
method: "post",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
let data = await res.json();
console.log(data);
if (data.msg === "Success") {
setSuccess("Success");
2022-01-06 08:14:36 +00:00
setTimeout(() => {
window.location.reload();
}, 1000);
2022-01-06 08:05:14 +00:00
} else {
setSuccess("Failed");
}
2023-07-20 20:21:46 +00:00
setLoading(false);
2022-01-06 08:05:14 +00:00
}