From ce657d8b175f3c8dc9e7b952a8e13548888d8820 Mon Sep 17 00:00:00 2001 From: Graham Stewart Date: Fri, 12 Jul 2024 16:52:36 -0700 Subject: [PATCH] Removed memoized lookups in favor of enums --- .../src/components/projects/AddProject.tsx | 39 +++++++------------ 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/react-app/src/components/projects/AddProject.tsx b/react-app/src/components/projects/AddProject.tsx index afc746da0..7020dc6eb 100644 --- a/react-app/src/components/projects/AddProject.tsx +++ b/react-app/src/components/projects/AddProject.tsx @@ -1,5 +1,5 @@ import { Box, Grid, InputAdornment, Typography, useTheme } from '@mui/material'; -import { useContext, useEffect, useMemo, useState } from 'react'; +import { useContext, useEffect, useState } from 'react'; import { NavigateBackButton } from '../display/DetailViewNavigation'; import TextFormField from '../form/TextFormField'; import AutocompleteFormField from '../form/AutocompleteFormField'; @@ -16,6 +16,9 @@ import React from 'react'; import useDataSubmitter from '@/hooks/useDataSubmitter'; import { LoadingButton } from '@mui/lab'; import { LookupContext } from '@/contexts/lookupContext'; +import { ProjectTask } from '@/constants/projectTasks'; +import { MonetaryType } from '@/constants/monetaryTypes'; +import { NoteTypes } from '@/constants/noteTypes'; const AddProject = () => { const navigate = useNavigate(); @@ -43,22 +46,7 @@ const AddProject = () => { const { data: lookupData } = useContext(LookupContext); const [tasksForAddState, setTasksForAddState] = useState([]); const { submit, submitting } = useDataSubmitter(api.projects.postProject); - const exemptionTask = useMemo( - () => lookupData?.Tasks.find((a) => a.Name === 'Exemption requested'), - [lookupData], - ); - const exemptionNote = useMemo( - () => lookupData?.NoteTypes.find((a) => a.Name === 'Exemption'), - [lookupData], - ); - const salesCostType = useMemo( - () => lookupData?.MonetaryTypes?.find((a) => a.Name === 'SalesCost'), - [lookupData], - ); - const programCostType = useMemo( - () => lookupData?.MonetaryTypes?.find((a) => a.Name === 'ProgramCost'), - [lookupData], - ); + useEffect(() => { if (!lookupData) { return; @@ -67,16 +55,17 @@ const AddProject = () => { (a) => a.Name === 'Required Documentation', ); const addTasks = lookupData?.Tasks.filter((task) => task.StatusId === defaultState.Id); - if (exemptionTask) { - addTasks.push({ ...exemptionTask, Name: 'Apply for Enhanced Referral Process exemption' }); - } + addTasks.push({ + Id: ProjectTask.EXEMPTION_REQUESTED, + Name: 'Apply for Enhanced Referral Process exemption', + }); addTasks.forEach((t, idx) => formMethods.setValue(`Tasks.${idx}.TaskId`, t.Id)); setTasksForAddState(addTasks); } - }, [lookupData, exemptionTask, formMethods]); + }, [lookupData, formMethods]); const watch = formMethods.watch('Tasks'); const exemptionWasRequested = - watch.find((a) => a.TaskId === exemptionTask.Id)?.IsCompleted === true; + watch.find((a) => a.TaskId === ProjectTask.EXEMPTION_REQUESTED)?.IsCompleted === true; return ( { ReportedFiscalYear: new Date().getFullYear(), ActualFiscalYear: new Date().getFullYear(), Monetaries: [ - { MonetaryTypeId: programCostType.Id, Value: formValues.ProgramCost }, - { MonetaryTypeId: salesCostType.Id, Value: formValues.SalesCost }, + { MonetaryTypeId: MonetaryType.PROGRAM_COST, Value: formValues.ProgramCost }, + { MonetaryTypeId: MonetaryType.SALES_COST, Value: formValues.SalesCost }, ], Tasks: formValues.Tasks.filter((a) => a.IsCompleted), Notes: exemptionWasRequested - ? [{ NoteTypeId: exemptionNote.Id, Note: formValues.ExemptionNote }] + ? [{ NoteTypeId: NoteTypes.EXEMPTION, Note: formValues.ExemptionNote }] : undefined, }, projectProperties,