From 114a13e05d9f29a1e403abdb529b00ed21a4be8c Mon Sep 17 00:00:00 2001 From: John Paul Larkin Date: Tue, 27 Feb 2024 21:28:55 +0000 Subject: [PATCH] refactor slide2 to add useform --- .../alpha/additional-details/_client.tsx | 275 ++++++++++-------- 1 file changed, 147 insertions(+), 128 deletions(-) diff --git a/app/(app)/alpha/additional-details/_client.tsx b/app/(app)/alpha/additional-details/_client.tsx index 74c25f78..cdef119b 100644 --- a/app/(app)/alpha/additional-details/_client.tsx +++ b/app/(app)/alpha/additional-details/_client.tsx @@ -5,7 +5,9 @@ import { redirect, useRouter, useSearchParams } from "next/navigation"; import { useSession } from "next-auth/react"; import { type TypeSlideOneSchema, + type TypeSlideTwoSchema, slideOneSchema, + slideTwoSchema, } from "@/schema/additionalUserDetails"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm, useFormContext } from "react-hook-form"; @@ -50,7 +52,7 @@ export default function AdditionalSignUpDetails({ {slide === 1 && } - {/* {slide === 2 && } */} + {slide === 2 && } {/* {slide === 3 && } */} ); @@ -192,21 +194,27 @@ function SlideOne({ details }: { details: UserDetails }) { ); } -function SlideTwo() { +function SlideTwo({ details }: { details: UserDetails }) { const router = useRouter(); + const { dateOfBirth, gender } = details; const { - getValues, - setValue, register, - trigger, - formState: { errors }, - } = useFormContext(); + handleSubmit, + setValue, + formState: { errors, isSubmitting }, + } = useForm({ + resolver: zodResolver(slideTwoSchema), + defaultValues: { dateOfBirth, gender }, + }); - const dob = getValues("dateOfBirth"); - const [year, setYear] = useState(dob?.getFullYear()); - const [month, setMonth] = useState(dob?.getMonth()); - const [day, setDay] = useState(dob?.getDate()); + const [year, setYear] = useState( + dateOfBirth?.getFullYear(), + ); + const [month, setMonth] = useState( + dateOfBirth?.getMonth(), + ); + const [day, setDay] = useState(dateOfBirth?.getDate()); const [listOfDaysInSelectedMonth, setListOfDaysInSelectedMonth] = useState([ 0, @@ -246,139 +254,150 @@ function SlideTwo() { (_, index) => startYearAgeDropdown + index, ).reverse(); - const handleClickNextSlide = async ( - e: React.MouseEvent, - ) => { - e.preventDefault(); - - const isError = await trigger(["dateOfBirth", "gender"], { - shouldFocus: true, - }); - - if (isError && dob !== undefined) { - router.push(`?slide=${3}`, { scroll: false }); + const onFormSubmit = async (data: TypeSlideTwoSchema) => { + try { + // const isSuccess = await handleFormSlideTwoSubmit(data); TODO add server action + const isSuccess = true; + if (isSuccess) { + toast.success("Saved"); + router.push(`?slide=${3}`, { scroll: false }); + } else { + toast.error("Error, saving was unsuccessful."); + } + } catch (error) { + toast.error("An unexpected error occurred."); } }; - const handleClickPreviousSlide = () => { - router.push(`?slide=${1}`, { scroll: false }); - }; - return ( -
-
-

- Demographic -

-

This information is private, but helps us improve

-
+
+
+
+

+ Demographic +

+

This information is private, but helps us improve

+
-
-
-
- - + + - {errors.gender &&

{`${errors.gender.message}`}

} -
+ {errors.gender &&

{`${errors.gender.message}`}

} +
-
- Date of Birth +
+ Date of Birth -
-
- - -
+ + +
-
- - -
- -
- - + setMonth(monthsOptions.indexOf(e.target.value)) + } + > + + {monthsOptions.map((month) => ( + + ))} + +
- {listOfDaysInSelectedMonth.map((day) => ( - - ))} - +
+ + +
-
- {errors.dateOfBirth &&

{`${errors.dateOfBirth.message}`}

} - -
+ {errors.dateOfBirth &&

{`${errors.dateOfBirth.message}`}

} + + - +
+ + +
+ - + ); }