Skip to content

Commit

Permalink
Merge pull request #4 from Jake-Bodin/Jake-Bodin/issue1019
Browse files Browse the repository at this point in the history
Jake bodin/issue1019
  • Loading branch information
Jake-Bodin committed Oct 13, 2023
2 parents 6d21ed3 + 2f70d9e commit cafb090
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
31 changes: 23 additions & 8 deletions src/client/app/components/unit/CreateUnitModalComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ export default function CreateUnitModalComponent() {
/* Create Unit Validation:
Name cannot be blank
Sec in Rate must be greater than zero
If type of unit is suffix their must be a suffix
*/
const [validUnit, setValidUnit] = useState(false);
useEffect(() => {
setValidUnit(state.name !== '' && state.secInRate > 0);
}, [state.name, state.secInRate]);
setValidUnit(state.name !== '' && state.secInRate > 0 &&
(state.typeOfUnit !== UnitType.suffix || (state.typeOfUnit === UnitType.suffix && state.suffix !== '')));
}, [state.name, state.secInRate, state.typeOfUnit, state.suffix]);
/* End State */

// Reset the state to default values
Expand All @@ -91,9 +93,13 @@ export default function CreateUnitModalComponent() {
// Set default identifier as name if left blank
state.identifier = (!state.identifier || state.identifier.length === 0) ? state.name : state.identifier;
// set displayable to none if unit is meter
if(state.typeOfUnit == UnitType.meter && state.displayable != DisplayableType.none) {
if (state.typeOfUnit == UnitType.meter && state.displayable != DisplayableType.none) {
state.displayable = DisplayableType.none;
}
// set unit to suffix if suffix is not empty
if (state.typeOfUnit != UnitType.suffix && state.suffix != '') {
state.typeOfUnit = UnitType.suffix;
}
// Add the new unit and update the store
dispatch(addUnit(state));
resetState();
Expand Down Expand Up @@ -142,7 +148,7 @@ export default function CreateUnitModalComponent() {
autoComplete='on'
onChange={e => handleStringChange(e)}
value={state.name}
invalid={state.name === ''}/>
invalid={state.name === ''} />
<FormFeedback>
<FormattedMessage id="error.required" />
</FormFeedback>
Expand All @@ -157,11 +163,16 @@ export default function CreateUnitModalComponent() {
name='typeOfUnit'
type='select'
onChange={e => handleStringChange(e)}
value={state.typeOfUnit}>
value={state.typeOfUnit}
invalid={state.typeOfUnit != UnitType.suffix && state.suffix != ''}>
{Object.keys(UnitType).map(key => {
return (<option value={key} key={key}>{translate(`UnitType.${key}`)}</option>)
return (<option value={key} key={key} disabled={state.suffix != '' && key != UnitType.suffix}>
{translate(`UnitType.${key}`)}</option>)
})}
</Input>
<FormFeedback>
<FormattedMessage id="unit.type.of.unit.suffix" />
</FormFeedback>
</FormGroup></Col>
{/* Unit represent input */}
<Col><FormGroup>
Expand Down Expand Up @@ -226,7 +237,7 @@ export default function CreateUnitModalComponent() {
min='1'
invalid={state.secInRate <= 0} />
<FormFeedback>
<FormattedMessage id="error.greater" values={{ min: '0'}} />
<FormattedMessage id="error.greater" values={{ min: '0' }} />
</FormFeedback>
</FormGroup></Col>
{/* Suffix input */}
Expand All @@ -238,7 +249,11 @@ export default function CreateUnitModalComponent() {
type='text'
autoComplete='off'
onChange={e => handleStringChange(e)}
value={state.suffix} />
value={state.suffix}
invalid={state.typeOfUnit === UnitType.suffix && state.suffix === ''} />
<FormFeedback>
<FormattedMessage id="error.required" />
</FormFeedback>
</FormGroup></Col>
</Row>
{/* Note input */}
Expand Down
31 changes: 23 additions & 8 deletions src/client/app/components/unit/EditUnitModalComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ export default function EditUnitModalComponent(props: EditUnitModalComponentProp
Name cannot be blank
Sec in Rate must be greater than zero
Unit type mismatches checked on submit
If type of unit is suffix their must be a suffix
*/
const [validUnit, setValidUnit] = useState(false);
useEffect(() => {
setValidUnit(state.name !== '' && state.secInRate > 0);
}, [state.name, state.secInRate]);
setValidUnit(state.name !== '' && state.secInRate > 0 &&
(state.typeOfUnit !== UnitType.suffix || (state.typeOfUnit === UnitType.suffix && state.suffix !== '')));
}, [state.name, state.secInRate, state.typeOfUnit, state.suffix]);
/* End State */

// Reset the state to default values
Expand Down Expand Up @@ -151,9 +153,13 @@ export default function EditUnitModalComponent(props: EditUnitModalComponentProp
|| (props.unit.secInRate !== state.secInRate
&& (props.unit.unitRepresent === UnitRepresentType.flow || props.unit.unitRepresent === UnitRepresentType.raw));
// set displayable to none if unit is meter
if(state.typeOfUnit == UnitType.meter && state.displayable != DisplayableType.none) {
if (state.typeOfUnit == UnitType.meter && state.displayable != DisplayableType.none) {
state.displayable = DisplayableType.none;
}
// set unit to suffix if suffix is not empty
if (state.typeOfUnit != UnitType.suffix && state.suffix != '') {
state.typeOfUnit = UnitType.suffix;
}
// Save our changes by dispatching the submitEditedUnit action
dispatch(submitEditedUnit(state, shouldRedoCik, shouldRefreshReadingViews));
// The updated unit is not fetched to save time. However, the identifier might have been
Expand Down Expand Up @@ -217,7 +223,7 @@ export default function EditUnitModalComponent(props: EditUnitModalComponentProp
autoComplete='on'
onChange={e => handleStringChange(e)}
value={state.name}
invalid={state.name === ''}/>
invalid={state.name === ''} />
<FormFeedback>
<FormattedMessage id="error.required" />
</FormFeedback>
Expand All @@ -232,11 +238,16 @@ export default function EditUnitModalComponent(props: EditUnitModalComponentProp
name='typeOfUnit'
type='select'
onChange={e => handleStringChange(e)}
value={state.typeOfUnit}>
value={state.typeOfUnit}
invalid={state.typeOfUnit != UnitType.suffix && state.suffix != ''}>
{Object.keys(UnitType).map(key => {
return (<option value={key} key={key}>{translate(`UnitType.${key}`)}</option>)
return (<option value={key} key={key} disabled={state.suffix != '' && key != UnitType.suffix}>
{translate(`UnitType.${key}`)}</option>)
})}
</Input>
<FormFeedback>
<FormattedMessage id="unit.type.of.unit.suffix" />
</FormFeedback>
</FormGroup></Col>
{/* Unit represent input */}
<Col><FormGroup>
Expand Down Expand Up @@ -304,7 +315,7 @@ export default function EditUnitModalComponent(props: EditUnitModalComponentProp
min='1'
invalid={state.secInRate <= 0} />
<FormFeedback>
<FormattedMessage id="error.greater" values={{ min: '0'}} />
<FormattedMessage id="error.greater" values={{ min: '0' }} />
</FormFeedback>
</FormGroup></Col>
{/* Suffix input */}
Expand All @@ -316,7 +327,11 @@ export default function EditUnitModalComponent(props: EditUnitModalComponentProp
type='text'
value={state.suffix}
placeholder='Suffix'
onChange={e => handleStringChange(e)} />
onChange={e => handleStringChange(e)}
invalid={state.typeOfUnit === UnitType.suffix && state.suffix === ''} />
<FormFeedback>
<FormattedMessage id="error.required" />
</FormFeedback>
</FormGroup></Col>
</Row>
{/* Note input */}
Expand Down
3 changes: 3 additions & 0 deletions src/client/app/translations/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ const localeData = {
"unit.successfully.edited.unit": "Successfully edited unit.",
"unit.suffix": "Suffix:",
"unit.type.of.unit": "Type of Unit:",
"unit.type.of.unit.suffix" : "Added suffix will set type of unit to suffix",
"units": "Units",
"unsaved.warning": "You have unsaved change(s). Are you sure you want to leave?",
"update": "update",
Expand Down Expand Up @@ -882,6 +883,7 @@ const localeData = {
"unit.successfully.edited.unit": "(Need French) Successfully edited unit.",
"unit.suffix": "(Need French) Suffix:",
"unit.type.of.unit": "(Need French) Type of Unit:",
"unit.type.of.unit.suffix" : "(Need French) Added suffix will set type of unit to suffix",
"units": "(Need French) Units",
"unsaved.warning": "(Need French) You have unsaved change(s). Are you sure you want to leave?",
"update": "update",
Expand Down Expand Up @@ -1333,6 +1335,7 @@ const localeData = {
"unit.successfully.edited.unit": "(Need Spanish) Successfully edited unit.",
"unit.suffix": "(Need Spanish) Suffix:",
"unit.type.of.unit": "(Need Spanish) Type of Unit:",
"unit.type.of.unit.suffix" : "(Need Spanish) Added suffix will set type of unit to suffix",
"units": "(Need Spanish) Units",
"unsaved.warning": "Tienes cambios sin guardar. ¿Estás seguro que quieres irte?",
"update": "Actualizado",
Expand Down

0 comments on commit cafb090

Please sign in to comment.