From 92e6d45a2b584b2dff4ffcaff2fd3bc76840cc9e Mon Sep 17 00:00:00 2001 From: Subhasis Chakraborti Date: Fri, 1 Mar 2024 21:04:54 -0500 Subject: [PATCH] Let capacities be unlimited --- .../programOccurrence/ProgramOccurrence.js | 9 +++-- .../serviceOccurrence/ServiceOccurrence.js | 9 +++-- .../src/components/shared/CapacityField.js | 36 +++++++++++++++++++ .../components/shared/fields/GeneralField.js | 2 +- 4 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 frontend/src/components/shared/CapacityField.js diff --git a/frontend/src/components/programOccurrence/ProgramOccurrence.js b/frontend/src/components/programOccurrence/ProgramOccurrence.js index 15542660..8269f63b 100644 --- a/frontend/src/components/programOccurrence/ProgramOccurrence.js +++ b/frontend/src/components/programOccurrence/ProgramOccurrence.js @@ -2,12 +2,12 @@ import React, {useEffect, useState} from 'react'; import GenericForm from "../shared/GenericForm"; import {fetchInternalTypeByFormType} from "../../api/internalTypeApi"; import {ProgramAndNeedSatisfierField} from "./ProgramAndNeedSatisfierField"; -import FieldGroup from '../shared/FieldGroup'; import {fetchCharacteristics} from '../../api/characteristicApi'; import {getInstancesInClass} from '../../api/dynamicFormApi'; import {Box} from '@mui/material'; import {Loading} from '../shared'; import SelectField from '../shared/fields/SelectField'; +import {CapacityField} from '../shared/CapacityField'; export default function ProgramOccurrenceForm() { const formType = 'programOccurrence'; @@ -51,13 +51,12 @@ export default function ProgramOccurrenceForm() { } else if (implementation.optionsFromClass?.endsWith('#NeedSatisfier')) { return ""; } else if (implementation.label === 'Capacity') { - return ; + return ; } else if (implementation.label === 'Occupancy') { return ''; // Not editable by the user } else if (implementation.label === "Occurrence Status") { - const statusFieldKey = `characteristic_${characteristics['Occurrence Status']?._id}`; + const statusFieldKey = `characteristic_${characteristics['Occurrence Status']._id}`; if (!statusFieldKey || !statusOptions) { return ; } diff --git a/frontend/src/components/serviceOccurrence/ServiceOccurrence.js b/frontend/src/components/serviceOccurrence/ServiceOccurrence.js index 9b165c97..e29576d2 100644 --- a/frontend/src/components/serviceOccurrence/ServiceOccurrence.js +++ b/frontend/src/components/serviceOccurrence/ServiceOccurrence.js @@ -2,12 +2,12 @@ import React, {useEffect, useState} from 'react'; import GenericForm from "../shared/GenericForm"; import {fetchInternalTypeByFormType} from "../../api/internalTypeApi"; import {ServiceAndNeedSatisfierField} from "./ServiceAndNeedSatisfierField"; -import FieldGroup from '../shared/FieldGroup'; import {fetchCharacteristics} from '../../api/characteristicApi'; import {getInstancesInClass} from '../../api/dynamicFormApi'; import {Box} from '@mui/material'; import {Loading} from '../shared'; import SelectField from '../shared/fields/SelectField'; +import {CapacityField} from '../shared/CapacityField'; export default function ServiceOccurrenceForm() { const formType = 'serviceOccurrence'; @@ -52,13 +52,12 @@ export default function ServiceOccurrenceForm() { } else if (implementation.optionsFromClass?.endsWith('#NeedSatisfier')) { return ""; } else if (implementation.label === 'Capacity') { - return ; + return ; } else if (implementation.label === 'Occupancy') { return ''; // Not editable by the user } else if (implementation.label === "Occurrence Status") { - const statusFieldKey = `characteristic_${characteristics['Occurrence Status']?._id}`; + const statusFieldKey = `characteristic_${characteristics['Occurrence Status']._id}`; if (!statusFieldKey || !statusOptions) { return ; } diff --git a/frontend/src/components/shared/CapacityField.js b/frontend/src/components/shared/CapacityField.js new file mode 100644 index 00000000..d9b7966e --- /dev/null +++ b/frontend/src/components/shared/CapacityField.js @@ -0,0 +1,36 @@ +import React from "react"; +import {useState} from "react"; +import SelectField from "../shared/fields/SelectField"; +import {Fade} from "@mui/material"; +import FieldGroup from "./FieldGroup"; + +export function CapacityField({fields, handleChange, capacityFieldId}) { + const capacityFieldKey = `characteristic_${capacityFieldId}`; + const [isCapacityLimited, setIsCapacityLimited] = useState(typeof fields[capacityFieldKey] !== 'undefined' + ? 'Yes' : 'No'); + + const handleChangeCapacity = e => { + const value = e.target.value; + setIsCapacityLimited(value); + + // if the new isCapacityLimited value is No, unset the inner field + if (value === 'No') { + handleChange(capacityFieldKey)(null); + } + } + + return <> + handleChangeCapacity(e)}/> + {isCapacityLimited === "Yes" + ? +
+ +
+
+ : null + } + +} diff --git a/frontend/src/components/shared/fields/GeneralField.js b/frontend/src/components/shared/fields/GeneralField.js index 7a18d83f..2477fe81 100644 --- a/frontend/src/components/shared/fields/GeneralField.js +++ b/frontend/src/components/shared/fields/GeneralField.js @@ -35,7 +35,7 @@ export default function GeneralField({type, onChange, value: defaultValue, ...pr else return null; } - return defaultValue || ''; + return defaultValue ?? ''; }); const handleChange = useCallback(e => {