Skip to content

Commit

Permalink
Removed memoized lookups in favor of enums
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamS-Quartech committed Jul 12, 2024
1 parent c6fd4d7 commit ce657d8
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions react-app/src/components/projects/AddProject.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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 (
<Box
display={'flex'}
Expand Down Expand Up @@ -287,12 +276,12 @@ const AddProject = () => {
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,
Expand Down

0 comments on commit ce657d8

Please sign in to comment.