Skip to content

Commit

Permalink
Let capacities be unlimited
Browse files Browse the repository at this point in the history
  • Loading branch information
htysc committed Mar 2, 2024
1 parent 69de1f4 commit 92e6d45
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -51,13 +51,12 @@ export default function ProgramOccurrenceForm() {
} else if (implementation.optionsFromClass?.endsWith('#NeedSatisfier')) {
return "";
} else if (implementation.label === 'Capacity') {
return <FieldGroup component={implementation.fieldType.type} key={`${type}_${id}`}
label={implementation.label} required={required} inputProps={{min: 0}}
value={fields[`${type}_${id}`]} onChange={handleChange(`${type}_${id}`)}/>;
return <CapacityField fields={fields} handleChange={handleChange}
capacityFieldId={characteristics['Capacity']._id} />;
} 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 <Box minWidth={"350px"}><Loading message=""/></Box>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -52,13 +52,12 @@ export default function ServiceOccurrenceForm() {
} else if (implementation.optionsFromClass?.endsWith('#NeedSatisfier')) {
return "";
} else if (implementation.label === 'Capacity') {
return <FieldGroup component={implementation.fieldType.type} key={`${type}_${id}`}
label={implementation.label} required={required} inputProps={{min: 0}}
value={fields[`${type}_${id}`]} onChange={handleChange(`${type}_${id}`)}/>;
return <CapacityField fields={fields} handleChange={handleChange}
capacityFieldId={characteristics['Capacity']._id} />;
} 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 <Box minWidth={"350px"}><Loading message=""/></Box>;
}
Expand Down
36 changes: 36 additions & 0 deletions frontend/src/components/shared/CapacityField.js
Original file line number Diff line number Diff line change
@@ -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 <>
<SelectField key={capacityFieldKey} label="Is Capacity Limited?" required value={isCapacityLimited}
options={['Yes', 'No']} onChange={e => handleChangeCapacity(e)}/>
{isCapacityLimited === "Yes"
? <Fade in={isCapacityLimited === "Yes"}>
<div>
<FieldGroup component={'NumberField'} key={capacityFieldKey}
label={'Capacity'} required inputProps={{min: 0}}
value={fields[capacityFieldKey]} onChange={handleChange(capacityFieldKey)}/>
</div>
</Fade>
: null
}
</>
}
2 changes: 1 addition & 1 deletion frontend/src/components/shared/fields/GeneralField.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function GeneralField({type, onChange, value: defaultValue, ...pr
else
return null;
}
return defaultValue || '';
return defaultValue ?? '';
});

const handleChange = useCallback(e => {
Expand Down

0 comments on commit 92e6d45

Please sign in to comment.