diff --git a/src/components/NewChapterForm.tsx b/src/components/NewChapterForm.tsx index 125ddef0..20169488 100644 --- a/src/components/NewChapterForm.tsx +++ b/src/components/NewChapterForm.tsx @@ -7,6 +7,7 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { ChapterRequest } from "src/app/api/chapter-request/route.schema"; import { ErrorMessage } from "@hookform/error-message"; import { createChapterRequest } from "@api/chapter-request/route.client"; +import { useApiThrottle } from "@hooks"; type ValidationSchema = z.infer; @@ -29,20 +30,26 @@ const NewChapterForm = () => { resolver: zodResolver(ChapterRequest), }); + const { fn: throttleCreateChapterRequest } = useApiThrottle({ + fn: createChapterRequest, + callback: (res) => + setFormSubmitted( + res.code === "SUCCESS" ? FormSubmission.SUBMITTED : FormSubmission.ERROR + ), + }); + const onSubmit: SubmitHandler> = async ( data, event ) => { event?.preventDefault(); - const response = await createChapterRequest({ body: data }); - if (response.code === "SUCCESS") { - setFormSubmitted(FormSubmission.SUBMITTED); - } else { - setFormSubmitted(FormSubmission.ERROR); - } - //TODO: revisit all possible return/calls + //TODO(nickbar01234): revisit all possible return/calls + // i.e - If duplicated email, show a different message. + throttleCreateChapterRequest({ body: data }); }; + const resetForm = () => setFormSubmitted(FormSubmission.NOT_SUBMITTED); + return (
@@ -51,7 +58,7 @@ const NewChapterForm = () => {
First Name
@@ -71,7 +78,7 @@ const NewChapterForm = () => {
Last Name
@@ -90,7 +97,7 @@ const NewChapterForm = () => {
University Email
@@ -109,7 +116,7 @@ const NewChapterForm = () => {
Phone Number
@@ -128,7 +135,7 @@ const NewChapterForm = () => {
College / University
@@ -147,7 +154,7 @@ const NewChapterForm = () => {
College / University Address
@@ -171,7 +178,7 @@ const NewChapterForm = () => { organization / storytelling?{" "}