diff --git a/.github/workflows/prettier.yml b/.github/workflows/prettier.yml index 5f7fa4e3..010f3bab 100644 --- a/.github/workflows/prettier.yml +++ b/.github/workflows/prettier.yml @@ -9,12 +9,12 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: ref: ${{ github.head_ref }} fetch-depth: 0 - name: Use Node.js - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: "18.x" @@ -25,4 +25,4 @@ jobs: uses: creyD/prettier_action@v4.3 with: prettier_options: "--write **/*.tsx **/*.ts **/*.mjs" - prettier_version: '2.x.x' + prettier_version: "2.x.x" diff --git a/.github/workflows/unit-testing.yml b/.github/workflows/unit-testing.yml index 0f0f9310..40fd5b77 100644 --- a/.github/workflows/unit-testing.yml +++ b/.github/workflows/unit-testing.yml @@ -6,13 +6,12 @@ name: Unit Testing on: push: - branches: [ "main" ] + branches: ["main"] pull_request: - branches: [ "main" ] + branches: ["main"] jobs: build: - runs-on: ubuntu-latest strategy: @@ -21,18 +20,18 @@ jobs: # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: - - uses: actions/checkout@v3 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v3 - with: - node-version: ${{ matrix.node-version }} - cache: 'npm' - - run: npm ci - - run: npm run test - env: - DATABASE_URL: ${{ secrets.DATABASE_URL }} - NEXTAUTH_URL: ${{ secrets.NEXTAUTH_URL }} - NEXTAUTH_SECRET: ${{ secrets.NEXTAUTH_SECRET }} - GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }} - GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }} - SENDGRID_API_KEY: ${{ secrets.SENDGRID_API_KEY }} + - uses: actions/checkout@v4 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node-version }} + cache: "npm" + - run: npm ci + - run: npm run test + env: + DATABASE_URL: ${{ secrets.DATABASE_URL }} + NEXTAUTH_URL: ${{ secrets.NEXTAUTH_URL }} + NEXTAUTH_SECRET: ${{ secrets.NEXTAUTH_SECRET }} + GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }} + GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }} + SENDGRID_API_KEY: ${{ secrets.SENDGRID_API_KEY }} diff --git a/src/components/AddSenior.tsx b/src/components/AddSenior.tsx index 18b9a5db..c8d20732 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"; @@ -25,6 +31,18 @@ type AddSeniorTileProps = { setSeniorPatch: Dispatch>; }; +type SeniorData = Pick< + z.infer, + "firstname" | "lastname" | "location" | "description" +>; + +const EMPTY_SENIOR: SeniorData = { + firstname: "", + lastname: "", + location: "", + description: "", +}; + export const AddSeniorTile = ({ showAddSeniorPopUp, setShowAddSeniorPopUp, @@ -86,16 +104,6 @@ const StudentSelector = ({ ); }; -// type SeniorData = Omit< -// Extract, { code: "SUCCESS" }>["data"], -// "StudentIDs" -// >; - -type SeniorData = Pick< - z.infer, - "firstname" | "lastname" | "location" | "description" ->; - const AddSenior = ({ seniors, students, @@ -105,13 +113,7 @@ const AddSenior = ({ seniorPatch, setSeniorPatch, }: AddSeniorProps) => { - const emptySenior: SeniorData = { - firstname: "", - lastname: "", - location: "", - description: "", - }; - const [seniorData, setSeniorData] = useState(emptySenior); + const [seniorData, setSeniorData] = useState(EMPTY_SENIOR); const [selectedStudents, setSelectedStudents] = useState([]); const [currentImage, setCurrentImage] = useState( ImageIcon @@ -119,11 +121,37 @@ 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]); + + useEffect(() => { + if (initialSenior) { + setSelectedStudents( + students.filter((student) => + initialSenior.StudentIDs.includes(student.id) + ) + ); + } + }, [students, initialSenior]); + const handlePopUp = () => { setShowAddSeniorPopUp(!showAddSeniorPopUp); - setSeniorData(emptySenior); + setSeniorData(EMPTY_SENIOR); setSelectedStudents([]); setCurrentImage(ImageIcon); + setSeniorPatch(""); // empty string used as falsey value to indicate update or patch }; const handleConfirm = () => { @@ -151,14 +179,9 @@ const AddSenior = ({ setConfirm(true); const newSeniors = seniors.filter((i) => i.id !== newerSeniorObj.id); setSeniors([...newSeniors, newerSeniorObj]); - } - // check after both API calls - if (currRes.code != "SUCCESS") { + } else { setError(true); } - - setSeniorData(emptySenior); - setSeniorPatch(""); // empty string used as falsey value to indicate update or patch }; const postAddSenior = async () => { @@ -177,9 +200,6 @@ const AddSenior = ({ } else { setError(true); } - setSeniorData(emptySenior); - setSelectedStudents([]); - return null; }); }; @@ -248,6 +268,7 @@ const AddSenior = ({ ) => setSeniorData({ ...seniorData, @@ -265,6 +286,7 @@ const AddSenior = ({ ) => setSeniorData((seniorData) => ({ ...seniorData, @@ -296,6 +318,7 @@ const AddSenior = ({