From 710d60b5edbf5ac967d9e2579ae97c070f106d10 Mon Sep 17 00:00:00 2001 From: StefanWout Date: Thu, 21 Nov 2024 16:44:34 +0000 Subject: [PATCH] got the react hook form with zod to work, FormProvder got rid of the getField errors somehow, all hail Overlord Claude, aesthetic clean up needed as well as linked up to trigger the engine --- .../components/RemoteAssessmentModal.tsx | 355 ++++++++++-------- 1 file changed, 208 insertions(+), 147 deletions(-) diff --git a/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx b/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx index 15d7810..fe736b7 100644 --- a/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx +++ b/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx @@ -8,6 +8,10 @@ import { Float } from "@headlessui-float/react"; import { ChevronDownIcon } from "@heroicons/react/20/solid"; import { useMutation } from "@tanstack/react-query"; import { useSession } from "next-auth/react"; +import { Form, useForm, FormProvider } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; +import * as z from "zod"; +import { FormField, FormItem, FormLabel, FormControl, FormMessage } from "@/app/shadcn_components/ui/form"; type Option = { label: string; @@ -291,6 +295,8 @@ function useCreateRemoteAssessment({ }, }); + + async function handleSubmit() { try { // Mutate presigned URL for asset list file @@ -325,71 +331,58 @@ export default function RemoteAssessmentModal({ setIsOpen: (isOpen: boolean) => void; portfolioId: string; }) { - const [scenario, setScenario] = useState(undefined); - const [housingType, sethousingType] = useState(""); - const [selectedGoal, setSelectedGoal] = useState(""); - const [goalValue, setGoalValue] = useState(""); - const [addressLineOne, setAddressLineOne] = useState(""); - const [postcode, setPostcode] = useState(""); - const [uprn, setUprn] = useState(null); - const [valuation, setValuation] = useState(""); - const [buttonDisabled, setButtonDisabled] = useState(true); + + const formSchema = z.object({ + scenario: z.string().min(1, "Scenario is required"), + goal: z.string().min(1, "Goal is required"), + goalValue: z.string().min(1, "Goal value is required"), + housingType: z.string().min(1, "Housing type is required"), + addressLineOne: z.string().min(1, "Address is required"), + postcode: z.string().min(1, "Postcode is required"), + uprn: z.number().min(1, "UPRN is required"), + valuation: z.number().min(1, "Valuation is required"), + }); - function handleScenarioChange(event: React.ChangeEvent) { - setScenario(event.target.value); - } + const form = useForm({ + resolver: zodResolver(formSchema), + defaultValues: { + scenario: "", + housingType: "", + goal: "", + goalValue: "", + addressLineOne: "", + postcode: "", + uprn: 0, + valuation: 0, + }, + }); - function handleAddressLineOneChange( - event: React.ChangeEvent - ) { - setAddressLineOne(event.target.value); - } + type FormValues = z.infer; - function handlePostcodeChange(event: React.ChangeEvent) { - setPostcode(event.target.value); - } - - function handleUprnChange(event: React.ChangeEvent) { - setUprn(Number(event.target.value)); - } - - function handleValuationChange(event: React.ChangeEvent) { - setValuation(event.target.value); - } + const onSubmit = async (data: FormValues) => { + try { + // First handle the form submission from react-hook-form + const formData = form.getValues(); + + // Then trigger the data upload using handleSubmit from useCreateRemoteAssessment + await handleSubmit(); + + // Close the modal on success + setIsOpen(false); + } catch (error) { + console.error('Error submitting form:', error); + } + }; const { handleSubmit, presignedUrlIsLoading, presignedUrlIsError } = useCreateRemoteAssessment({ portfolioId, - uprn, - addressLineOne, - postcode, - valuation, + uprn: form.watch("uprn"), + addressLineOne: form.watch("addressLineOne"), + postcode: form.watch("postcode"), + valuation: form.watch("valuation"), }); - useEffect(() => { - function handleButtonDisabled(): boolean { - return !( - scenario && - selectedGoal && - housingType && - addressLineOne && - postcode && - uprn && - valuation - ); - } - - setButtonDisabled(handleButtonDisabled()); - }, [ - scenario, - selectedGoal, - housingType, - addressLineOne, - postcode, - uprn, - valuation, - ]); - return ( <> @@ -422,101 +415,169 @@ export default function RemoteAssessmentModal({ leaveTo="opacity-0 scale-95" > - - {scenario} - -
- Scenario Name - + Remote Assessment Details + + +
+ ( + + Scenario Name + + + + + + )} /> -
-
- - sethousingType(option.value)} + + ( + + Housing Type + + field.onChange(option.value)} + /> + + + + )} /> -
-
- - setSelectedGoal(option.value)} + + ( + + Goal + + field.onChange(option.value)} + /> + + + + )} /> - {selectedGoal === "Increase EPC" && ( -
- - { - setGoalValue(option.value); - }} - /> -
+ + ( + + Goal Value + + field.onChange(option.value)} + /> + + + + )} + /> + + ( + + Address + + + + + + )} + /> + + ( + + Postcode + + + + + + )} + /> + + ( + + UPRN + + field.onChange(Number(e.target.value))} + /> + + + + )} + /> + + ( + + Valuation + + field.onChange(Number(e.target.value))} + /> + + + + )} + /> + +
+ + +
+ {presignedUrlIsError && ( +

Error uploading files

)} -
-
- Address Line 1 - -
-
- Postcode - -
-
- UPRN - -
-
- Valuation - -
-
- -
- -
+ + +