From 4b1a08abfaadfced072acd892031c795d2e25e91 Mon Sep 17 00:00:00 2001 From: Caleb Ledi Date: Tue, 12 Mar 2024 23:55:24 -0400 Subject: [PATCH] Edit existing senior --- src/components/AddSenior.tsx | 55 +++++++++++++++++++------ src/components/SeniorView.tsx | 53 ++++++++++++++++++++---- src/components/TileGrid/index.ts | 1 + src/components/senior/DisplaySenior.tsx | 4 +- 4 files changed, 91 insertions(+), 22 deletions(-) diff --git a/src/components/AddSenior.tsx b/src/components/AddSenior.tsx index 18b9a5db..22dcac7c 100644 --- a/src/components/AddSenior.tsx +++ b/src/components/AddSenior.tsx @@ -1,4 +1,10 @@ -import React, { Dispatch, SetStateAction, useState } from "react"; +import React, { + Dispatch, + SetStateAction, + useEffect, + useMemo, + useState, +} from "react"; import Image, { StaticImageData } from "next/legacy/image"; import cn from "classnames"; import FilterDropdown from "@components/FilterDropdown"; @@ -86,11 +92,6 @@ const StudentSelector = ({ ); }; -// type SeniorData = Omit< -// Extract, { code: "SUCCESS" }>["data"], -// "StudentIDs" -// >; - type SeniorData = Pick< z.infer, "firstname" | "lastname" | "location" | "description" @@ -105,12 +106,14 @@ const AddSenior = ({ seniorPatch, setSeniorPatch, }: AddSeniorProps) => { - const emptySenior: SeniorData = { - firstname: "", - lastname: "", - location: "", - description: "", - }; + const emptySenior = useMemo(() => { + return { + firstname: "", + lastname: "", + location: "", + description: "", + }; + }, []); const [seniorData, setSeniorData] = useState(emptySenior); const [selectedStudents, setSelectedStudents] = useState([]); const [currentImage, setCurrentImage] = useState( @@ -119,6 +122,31 @@ const AddSenior = ({ const [confirm, setConfirm] = useState(false); const [error, setError] = useState(false); + const initialSenior: Senior | undefined = useMemo(() => { + const senior = seniors.find((senior) => senior.id === seniorPatch); + return senior; + }, [seniorPatch, seniors]); + + useEffect(() => { + if (initialSenior) + setSeniorData({ + firstname: initialSenior.firstname, + lastname: initialSenior.lastname, + location: initialSenior.location, + description: initialSenior.description, + }); + }, [initialSenior, emptySenior]); + + useEffect(() => { + if (initialSenior) { + setSelectedStudents( + students.filter((student) => + initialSenior.StudentIDs.includes(student.id) + ) + ); + } + }, [students, initialSenior]); + const handlePopUp = () => { setShowAddSeniorPopUp(!showAddSeniorPopUp); setSeniorData(emptySenior); @@ -248,6 +276,7 @@ const AddSenior = ({ ) => setSeniorData({ ...seniorData, @@ -265,6 +294,7 @@ const AddSenior = ({ ) => setSeniorData((seniorData) => ({ ...seniorData, @@ -296,6 +326,7 @@ const AddSenior = ({