From 03d73c824c40c83375b523b2ca983cfdb47b115f Mon Sep 17 00:00:00 2001 From: isaconantonis Date: Fri, 25 Aug 2023 15:28:06 +0200 Subject: [PATCH] fix contact form --- app/contact/page.tsx | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/app/contact/page.tsx b/app/contact/page.tsx index a6ae5d0..f2e7df7 100644 --- a/app/contact/page.tsx +++ b/app/contact/page.tsx @@ -1,13 +1,23 @@ "use client" import { BuildingOffice2Icon, EnvelopeIcon, PhoneIcon } from "@heroicons/react/24/outline" -import { FormEvent } from "react" +import { FormEvent, useState } from "react" export default function Contact() { - function handleSubmit(event: FormEvent) { + const [isSent, setIsSent] = useState(false) + const [isLoading, setIsLoading] = useState(false) + + async function handleSubmit(event: FormEvent) { event.preventDefault() const form = event.target as HTMLFormElement + const isValid = form.checkValidity() - fetch("/api/send", { + if (!isValid) { + return + } + + setIsLoading(true) + + await fetch("/api/send", { method: "POST", headers: { "Content-Type": "application/json", @@ -20,10 +30,25 @@ export default function Contact() { phoneNumber: form.phoneNumber.value, }), }) + + form.reset() + setIsSent(true) + setIsLoading(false) } return (
+ {isSent && ( +
setIsSent(false)} + className="cursor-pointer bg-primary-700 py-4 text-center text-primary-800 lg:px-4" + > +
+ Sent + Your message has been sent. +
+
+ )}
@@ -77,6 +102,7 @@ export default function Contact() {