From 54e012da3231f8ada1ec48f40ea2a001f50f045e Mon Sep 17 00:00:00 2001 From: Caleb Ledi Date: Wed, 10 Apr 2024 13:24:24 -0400 Subject: [PATCH] Add feedback to add senior form --- src/components/AddSenior.tsx | 105 +++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 49 deletions(-) diff --git a/src/components/AddSenior.tsx b/src/components/AddSenior.tsx index 3330e89e..b3b076f0 100644 --- a/src/components/AddSenior.tsx +++ b/src/components/AddSenior.tsx @@ -6,8 +6,11 @@ import React, { useState, } from "react"; import Image, { StaticImageData } from "next/legacy/image"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useForm, SubmitHandler } from "react-hook-form"; import FilterDropdown from "@components/FilterDropdown"; import { Senior, User } from "@prisma/client"; + import ImageIcon from "../../public/icons/icon_add_photo.png"; import { patchSenior } from "src/app/api/senior/[id]/route.client"; import { postSenior } from "src/app/api/senior/route.client"; @@ -32,10 +35,14 @@ type AddSeniorTileProps = { setSeniorPatch: Dispatch>; }; -type SeniorData = Pick< - z.infer, - "firstname" | "lastname" | "location" | "description" ->; +const seniorFormSchema = seniorSchema.pick({ + firstname: true, + lastname: true, + location: true, + description: true, +}); + +type SeniorData = z.infer; const EMPTY_SENIOR: SeniorData = { firstname: "", @@ -114,13 +121,26 @@ const AddSenior = ({ seniorPatch, setSeniorPatch, }: AddSeniorProps) => { - const [seniorData, setSeniorData] = useState(EMPTY_SENIOR); const [selectedStudents, setSelectedStudents] = useState([]); const [currentImage, setCurrentImage] = useState( ImageIcon ); const [confirm, setConfirm] = useState(false); const [error, setError] = useState(false); + const [edited, setEdited] = useState(false); + + const { register, handleSubmit, reset, setValue } = useForm({ + resolver: zodResolver(seniorFormSchema), + }); + + const onSubmit: SubmitHandler = async (data, event) => { + event?.preventDefault(); + if (seniorPatch) { + patchAddSenior(data); + } else { + postAddSenior(data); + } + }; const initialSenior: Senior | undefined = useMemo(() => { const senior = seniors.find((senior) => senior.id === seniorPatch); @@ -128,14 +148,15 @@ const AddSenior = ({ }, [seniorPatch, seniors]); useEffect(() => { - if (initialSenior) - setSeniorData({ - firstname: initialSenior.firstname, - lastname: initialSenior.lastname, - location: initialSenior.location, - description: initialSenior.description, + if (initialSenior) { + setValue("firstname", initialSenior.firstname, { shouldValidate: true }); + setValue("lastname", initialSenior.lastname, { shouldValidate: true }); + setValue("location", initialSenior.location, { shouldValidate: true }); + setValue("description", initialSenior.description, { + shouldValidate: true, }); - }, [initialSenior]); + } + }, [initialSenior, setValue]); useEffect(() => { if (initialSenior) { @@ -149,10 +170,11 @@ const AddSenior = ({ const handlePopUp = () => { setShowAddSeniorPopUp(!showAddSeniorPopUp); - setSeniorData(EMPTY_SENIOR); setSelectedStudents([]); setCurrentImage(ImageIcon); setSeniorPatch(""); // empty string used as falsey value to indicate update or patch + setEdited(false); + reset(); }; const handleConfirm = () => { @@ -161,7 +183,7 @@ const AddSenior = ({ setError(false); }; - const patchAddSenior = async () => { + const patchAddSenior = async (seniorData: SeniorData) => { // put accumulated students into senior model data const seniorModel = { ...seniorData, @@ -185,7 +207,7 @@ const AddSenior = ({ } }; - const postAddSenior = async () => { + const postAddSenior = async (seniorData: SeniorData) => { // put accumulated students into senior model data const seniorModel = { ...seniorData, @@ -227,7 +249,7 @@ const AddSenior = ({ {showAddSeniorPopUp && ( {!confirm && !error ? ( -
+
{seniorPatch ? "Update" : "Add New"} Senior
@@ -241,9 +263,6 @@ const AddSenior = ({ />
- - {/* Todo: First and Last name values are stored into the seniorData.name field. Seperate into two fields - later as seniorData.name propgates to backend*/}
@@ -252,13 +271,8 @@ const AddSenior = ({ ) => - setSeniorData({ - ...seniorData, - firstname: e.target.value, - }) - } + {...register("firstname")} + onChange={() => setEdited(true)} />
@@ -269,13 +283,8 @@ const AddSenior = ({ ) => - setSeniorData((seniorData) => ({ - ...seniorData, - lastname: e.target.value, - })) - } + {...register("lastname")} + onChange={() => setEdited(true)} />
@@ -287,11 +296,9 @@ const AddSenior = ({ ) => - setSeniorData({ ...seniorData, location: e.target.value }) - } + {...register("location")} + onChange={() => setEdited(true)} />
@@ -301,13 +308,8 @@ const AddSenior = ({