From b946974267cb5f7ee652577956c671ce35ecea35 Mon Sep 17 00:00:00 2001 From: DrMint Date: Wed, 23 Mar 2022 16:45:41 +0100 Subject: [PATCH] Added more states to the form --- src/pages/about-us/contact.tsx | 46 +++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/pages/about-us/contact.tsx b/src/pages/about-us/contact.tsx index 1121dba..071f133 100644 --- a/src/pages/about-us/contact.tsx +++ b/src/pages/about-us/contact.tsx @@ -26,7 +26,9 @@ export default function AboutUs(props: ContactProps): JSX.Element { const { langui, post, locales } = props; const router = useRouter(); const [formResponse, setFormResponse] = useState(""); - const [formCompleted, setFormCompleted] = useState(false); + const [formState, setFormState] = useState<"stale" | "ongoing" | "completed">( + "stale" + ); const random1 = randomInt(0, 10); const random2 = randomInt(0, 10); @@ -73,17 +75,29 @@ export default function AboutUs(props: ContactProps): JSX.Element {
{ e.preventDefault(); - if (e.target.verif.value == random1 + random2 && !formCompleted) { + const fields = e.target as unknown as { + verif: HTMLInputElement; + name: HTMLInputElement; + email: HTMLInputElement; + message: HTMLInputElement; + }; + + setFormState("ongoing"); + + if ( + parseInt(fields.verif.value) == random1 + random2 && + formState !== "completed" + ) { const content: RequestMailProps = { - name: e.target.name.value, - email: e.target.email.value, - message: e.target.message.value, + name: fields.name.value, + email: fields.email.value, + message: fields.message.value, formName: "Contact Form", }; fetch("/api/mail", { @@ -98,24 +112,28 @@ export default function AboutUs(props: ContactProps): JSX.Element { switch (data.code) { case "OKAY": setFormResponse(langui.response_email_success); - setFormCompleted(true); + setFormState("completed"); + break; case "EENVELOPE": - langui.response_invalid_email; + setFormResponse(langui.response_invalid_email); + setFormState("stale"); break; default: setFormResponse(data.message || ""); + setFormState("stale"); break; } }); } else { setFormResponse(langui.response_invalid_code); + setFormState("stale"); } router.replace("#send-response"); - e.target.verif.value = ""; + fields.verif.value = ""; }} >
@@ -126,7 +144,7 @@ export default function AboutUs(props: ContactProps): JSX.Element { name="name" id="name" required - disabled={formCompleted} + disabled={formState !== "stale"} />
@@ -138,7 +156,7 @@ export default function AboutUs(props: ContactProps): JSX.Element { name="email" id="email" required - disabled={formCompleted} + disabled={formState !== "stale"} />

{langui.email_gdpr_notice} @@ -153,7 +171,7 @@ export default function AboutUs(props: ContactProps): JSX.Element { className="w-full" rows={8} required - disabled={formCompleted} + disabled={formState !== "stale"} />

@@ -169,7 +187,7 @@ export default function AboutUs(props: ContactProps): JSX.Element { name="verif" id="verif" required - disabled={formCompleted} + disabled={formState !== "stale"} /> @@ -177,7 +195,7 @@ export default function AboutUs(props: ContactProps): JSX.Element { type="submit" value={langui.send} className="w-min !px-6" - disabled={formCompleted} + disabled={formState !== "stale"} />