Skip to content

Commit

Permalink
refactor: change getValue into generic getDropdownOption
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgamache committed Nov 26, 2024
1 parent d77bae4 commit 69573d9
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 195 deletions.
4 changes: 4 additions & 0 deletions frontend/src/app/common/methods.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,7 @@ export const displayBackendErrors = (message: string) => {
default:
}
};

export function getDropdownOption(matchValue: string | undefined | null, optionsList: Option[]): Option | undefined {
return optionsList.find((item) => item.value === matchValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { getCaseFile, upsertDecisionOutcome } from "@store/reducers/case-thunks"
import { selectCaseId } from "@store/reducers/case-selectors";
import { UUID } from "crypto";
import { ValidationTextArea } from "@common/validation-textarea";
import { getDropdownOption } from "@/app/common/methods";

type props = {
leadIdentifier: string;
Expand Down Expand Up @@ -117,50 +118,6 @@ export const DecisionForm: FC<props> = ({
setData(model);
};

const getValue = (property: string): Option | undefined | null => {
let result: Option | undefined;

switch (property) {
case "schedule": {
const { schedule } = data;
result = schedulesOptions.find((item) => item.value === schedule);
break;
}

case "sector": {
const { sector } = data;
result = sectorsOptions.find((item) => item.value === sector);
break;
}

case "discharge": {
const { discharge } = data;
result = dischargesOptions.find((item) => item.value === discharge);
break;
}

case "nonCompliance": {
const { nonCompliance } = data;
result = nonComplianceOptions.find((item) => item.value === nonCompliance);
break;
}

case "leadAgency": {
const { leadAgency } = data;
result = leadAgencyOptions.find((item) => item.value === leadAgency);
break;
}

case "actionTaken": {
const { actionTaken } = data;
result = decisionTypeOptions.find((item) => item.value === actionTaken);
break;
}
}

return !result ? null : result;
};

const handleRationaleChange = (value: string) => {
updateModel("rationale", value.trim());
};
Expand Down Expand Up @@ -336,7 +293,7 @@ export const DecisionForm: FC<props> = ({
handleScheduleChange(evt.value);
}
}}
value={getValue("schedule")}
value={getDropdownOption(schedule, schedulesOptions)}
/>
</div>
</div>
Expand All @@ -359,7 +316,7 @@ export const DecisionForm: FC<props> = ({
onChange={(evt) => {
updateModel("sector", evt?.value);
}}
value={getValue("sector")}
value={getDropdownOption(sector, sectorsOptions)}
/>
</div>
</div>
Expand All @@ -382,7 +339,7 @@ export const DecisionForm: FC<props> = ({
onChange={(evt) => {
updateModel("discharge", evt?.value);
}}
value={getValue("discharge")}
value={getDropdownOption(discharge, dischargesOptions)}
/>
</div>
</div>
Expand All @@ -405,7 +362,7 @@ export const DecisionForm: FC<props> = ({
const action = evt?.value ? evt?.value : "";
handleActionTakenChange(action);
}}
value={getValue("actionTaken")}
value={getDropdownOption(actionTaken, decisionTypeOptions)}
/>
</div>
</div>
Expand All @@ -427,7 +384,7 @@ export const DecisionForm: FC<props> = ({
onChange={(evt) => {
updateModel("leadAgency", evt?.value);
}}
value={getValue("leadAgency")}
value={getDropdownOption(leadAgency, leadAgencyOptions)}
/>
</div>
</div>
Expand Down Expand Up @@ -475,7 +432,7 @@ export const DecisionForm: FC<props> = ({
onChange={(evt) => {
updateModel("nonCompliance", evt?.value);
}}
value={getValue("nonCompliance")}
value={getDropdownOption(nonCompliance, nonComplianceOptions)}
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FC } from "react";
import { formatDate } from "@common/methods";
import { formatDate, getDropdownOption } from "@common/methods";
import { useAppSelector } from "@hooks/hooks";
import { selectLeadAgencyDropdown } from "@store/reducers/code-table";
import {
Expand Down Expand Up @@ -46,74 +46,29 @@ export const DecisionItem: FC<props> = ({
const decisionTypeOptions = useAppSelector(selectDecisionTypeDropdown);
const agencyOptions = useAppSelector(selectLeadAgencyDropdown);

const getValue = (property: string): Option | undefined | null => {
let result: Option | undefined;

switch (property) {
case "schedule": {
result = schedulesOptions.find((item) => item.value === schedule);
break;
}

case "sector": {
result = sectorsOptions.find((item) => item.value === sector);
break;
}
case "schedule-sector": {
result = scheduleSectorsOptions.find((item) => item.value === sector);
break;
}
case "schedule-sector-type": {
result = scheduleSectorsOptions.find((item) => item.value === sector);
break;
}
case "discharge": {
result = dischargesOptions.find((item) => item.value === discharge);
break;
}

case "nonCompliance": {
result = nonComplianceOptions.find((item) => item.value === nonCompliance);
break;
}

case "leadAgency": {
result = agencyOptions.find((item) => item.value === leadAgency);
break;
}

case "actionTaken": {
result = decisionTypeOptions.find((item) => item.value === actionTaken);
break;
}
}

return !result ? null : result;
};

return (
<dl>
<div>
<dt>WDR schedule/IPM sector type</dt>
<dd>{getValue("schedule")?.label}</dd>
<dd>{getDropdownOption(schedule, schedulesOptions)?.label}</dd>
</div>
<div>
<dt>Sector/Category</dt>
<dd>{getValue("sector")?.label}</dd>
<dd>{getDropdownOption(sector, sectorsOptions)?.label}</dd>
</div>
<div>
<dt>Discharge type</dt>
<dd>{getValue("discharge")?.label}</dd>
<dd>{getDropdownOption(discharge, dischargesOptions)?.label}</dd>
</div>
<hr className="my-0"></hr>
<div>
<dt>Action taken</dt>
<dd>{getValue("actionTaken")?.label}</dd>
<dd>{getDropdownOption(actionTaken, decisionTypeOptions)?.label}</dd>
</div>
{actionTaken === CASE_ACTION_CODE.FWDLEADAGN && (
<div>
<dt>Lead agency</dt>
<dd>{getValue("leadAgency")?.label}</dd>
<dd>{getDropdownOption(leadAgency, agencyOptions)?.label}</dd>
</div>
)}
{actionTaken === CASE_ACTION_CODE.RESPREC && (
Expand All @@ -124,7 +79,7 @@ export const DecisionItem: FC<props> = ({
)}
<div>
<dt>Non-compliance decsion matrix</dt>
<dd>{getValue("nonCompliance")?.label}</dd>
<dd>{getDropdownOption(nonCompliance, nonComplianceOptions)?.label}</dd>
</div>
<div>
<dt>Rationale</dt>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { ToggleError } from "@common/toast";
import { getNextOrderNumber } from "@components/containers/complaints/outcomes/hwcr-outcome-by-animal-v2";
import { ValidationTextArea } from "@common/validation-textarea";
import { selectComplaintLargeCarnivoreInd } from "@store/reducers/complaints";
import { getValue } from "./outcome-common";
import { getDropdownOption } from "@/app/common/methods";

type props = {
index: number;
Expand Down Expand Up @@ -72,8 +72,6 @@ export const CreateAnimalOutcome: FC<props> = ({ index, assignedOfficer: officer
const isInEdit = useAppSelector((state) => state.cases.isInEdit);
const showSectionErrors = isInEdit.showSectionErrors;

const optionDictionaries = { speciesList, sexes, ages, threatLevels, outcomes, officers };

//-- error handling
const [speciesError, setSpeciesError] = useState("");
const [officerError, setOfficerError] = useState("");
Expand Down Expand Up @@ -388,7 +386,7 @@ export const CreateAnimalOutcome: FC<props> = ({ index, assignedOfficer: officer
enableValidation={true}
placeholder="Select"
onChange={handleSpeciesChange}
defaultOption={getValue("species", data, optionDictionaries)}
defaultOption={getDropdownOption(data.species, speciesList)}
errorMessage={speciesError}
/>
</div>
Expand Down Expand Up @@ -536,7 +534,7 @@ export const CreateAnimalOutcome: FC<props> = ({ index, assignedOfficer: officer
onChange={(evt) => {
handleOfficerChange(evt);
}}
value={getValue("officer", data, optionDictionaries)}
value={getDropdownOption(data.officer, officers)}
errorMessage={officerError}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { selectOfficerListByAgency } from "@store/reducers/officer";
import { ValidationDatePicker } from "@common/validation-date-picker";
import Option from "@apptypes/app/option";
import { REQUIRED } from "@constants/general";
import { getDropdownOption } from "@/app/common/methods";

type props = {
agency: string;
Expand Down Expand Up @@ -55,12 +56,6 @@ export const DrugAuthorizedBy = forwardRef<refProps, props>((props, ref) => {
return result;
};

const getValue = (property: string): Option | undefined => {
if (property === "officer") {
return officers.find((item) => item.value === officer);
}
};

const updateModel = (property: string, value: string | Date | null | undefined) => {
const source = { officer, date };
const authorization = { ...source, [property]: value };
Expand Down Expand Up @@ -110,7 +105,7 @@ export const DrugAuthorizedBy = forwardRef<refProps, props>((props, ref) => {
options={officers}
placeholder="Select"
enableValidation={true}
value={getValue("officer")}
value={getDropdownOption(officer, officers)}
errorMessage={officerError}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CompInput } from "@components/common/comp-input";
import { CompSelect } from "@components/common/comp-select";
import { selectDrugs, selectDrugUseMethods, selectRemainingDrugUse } from "@store/reducers/code-table";
import Option from "@apptypes/app/option";
import { isPositiveNum } from "@common/methods";
import { getDropdownOption, isPositiveNum } from "@common/methods";
import { REQUIRED } from "@constants/general";

type refProps = {
Expand Down Expand Up @@ -85,23 +85,7 @@ export const DrugUsed = forwardRef<refProps, props>((props, ref) => {
return result;
};

const getValue = (property: string): Option | undefined => {
switch (property) {
case "drug": {
return drugs.find((item) => item.value === drug);
}

case "injection-method": {
return drugUseMethods.find((item) => item.value === injectionMethod);
}

case "remaining": {
return remainingDrugUse.find((item) => item.value === remainingUse);
}
}
};

const updateModel = (property: string, value: string | Date | number | null | undefined) => {
const updateModel = (property: string, value: string | Date | number | null | undefined) => {
const source = {
id,
vial,
Expand Down Expand Up @@ -212,7 +196,7 @@ export const DrugUsed = forwardRef<refProps, props>((props, ref) => {
onChange={(evt) => {
handleDrugNameChange(evt);
}}
value={getValue("drug")}
value={getDropdownOption(drug, drugs)}
/>
</div>
</div>
Expand All @@ -230,7 +214,7 @@ export const DrugUsed = forwardRef<refProps, props>((props, ref) => {
onChange={(evt) => {
handleInjectionMethodChange(evt);
}}
value={getValue("injection-method")}
value={getDropdownOption(injectionMethod, drugUseMethods)}
/>
</div>
</div>
Expand Down Expand Up @@ -267,7 +251,7 @@ export const DrugUsed = forwardRef<refProps, props>((props, ref) => {
onChange={(evt) => {
handleRemainingUsed(evt?.value ?? "");
}}
value={getValue("remaining")}
value={getDropdownOption(remainingUse, remainingDrugUse)}
/>
</div>
</div>
Expand Down
Loading

0 comments on commit 69573d9

Please sign in to comment.