Added more states to the form

This commit is contained in:
DrMint 2022-03-23 16:45:41 +01:00
parent 844e3cb875
commit b946974267
1 changed files with 32 additions and 14 deletions

View File

@ -26,7 +26,9 @@ export default function AboutUs(props: ContactProps): JSX.Element {
const { langui, post, locales } = props; const { langui, post, locales } = props;
const router = useRouter(); const router = useRouter();
const [formResponse, setFormResponse] = useState(""); const [formResponse, setFormResponse] = useState("");
const [formCompleted, setFormCompleted] = useState(false); const [formState, setFormState] = useState<"stale" | "ongoing" | "completed">(
"stale"
);
const random1 = randomInt(0, 10); const random1 = randomInt(0, 10);
const random2 = randomInt(0, 10); const random2 = randomInt(0, 10);
@ -73,17 +75,29 @@ export default function AboutUs(props: ContactProps): JSX.Element {
<div className="flex flex-col gap-8 text-center"> <div className="flex flex-col gap-8 text-center">
<form <form
className={`gap-8 grid ${ className={`gap-8 grid ${
formCompleted && formState !== "stale" &&
"opacity-60 cursor-not-allowed touch-none pointer-events-none" "opacity-60 cursor-not-allowed touch-none pointer-events-none"
}`} }`}
onSubmit={(e) => { onSubmit={(e) => {
e.preventDefault(); 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 = { const content: RequestMailProps = {
name: e.target.name.value, name: fields.name.value,
email: e.target.email.value, email: fields.email.value,
message: e.target.message.value, message: fields.message.value,
formName: "Contact Form", formName: "Contact Form",
}; };
fetch("/api/mail", { fetch("/api/mail", {
@ -98,24 +112,28 @@ export default function AboutUs(props: ContactProps): JSX.Element {
switch (data.code) { switch (data.code) {
case "OKAY": case "OKAY":
setFormResponse(langui.response_email_success); setFormResponse(langui.response_email_success);
setFormCompleted(true); setFormState("completed");
break; break;
case "EENVELOPE": case "EENVELOPE":
langui.response_invalid_email; setFormResponse(langui.response_invalid_email);
setFormState("stale");
break; break;
default: default:
setFormResponse(data.message || ""); setFormResponse(data.message || "");
setFormState("stale");
break; break;
} }
}); });
} else { } else {
setFormResponse(langui.response_invalid_code); setFormResponse(langui.response_invalid_code);
setFormState("stale");
} }
router.replace("#send-response"); router.replace("#send-response");
e.target.verif.value = ""; fields.verif.value = "";
}} }}
> >
<div className="flex flex-col place-items-center gap-1"> <div className="flex flex-col place-items-center gap-1">
@ -126,7 +144,7 @@ export default function AboutUs(props: ContactProps): JSX.Element {
name="name" name="name"
id="name" id="name"
required required
disabled={formCompleted} disabled={formState !== "stale"}
/> />
</div> </div>
@ -138,7 +156,7 @@ export default function AboutUs(props: ContactProps): JSX.Element {
name="email" name="email"
id="email" id="email"
required required
disabled={formCompleted} disabled={formState !== "stale"}
/> />
<p className="text-sm text-dark italic opacity-70"> <p className="text-sm text-dark italic opacity-70">
{langui.email_gdpr_notice} {langui.email_gdpr_notice}
@ -153,7 +171,7 @@ export default function AboutUs(props: ContactProps): JSX.Element {
className="w-full" className="w-full"
rows={8} rows={8}
required required
disabled={formCompleted} disabled={formState !== "stale"}
/> />
</div> </div>
@ -169,7 +187,7 @@ export default function AboutUs(props: ContactProps): JSX.Element {
name="verif" name="verif"
id="verif" id="verif"
required required
disabled={formCompleted} disabled={formState !== "stale"}
/> />
</div> </div>
@ -177,7 +195,7 @@ export default function AboutUs(props: ContactProps): JSX.Element {
type="submit" type="submit"
value={langui.send} value={langui.send}
className="w-min !px-6" className="w-min !px-6"
disabled={formCompleted} disabled={formState !== "stale"}
/> />
</div> </div>
</form> </form>