Skip to content

Commit

Permalink
#1292 | Handle IsWithinCatchment switch during InlineConcept creation
Browse files Browse the repository at this point in the history
  • Loading branch information
himeshr committed Aug 23, 2024
1 parent 98616bf commit 0ad539c
Showing 1 changed file with 13 additions and 38 deletions.
51 changes: 13 additions & 38 deletions src/formDesigner/components/LocationConcept.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useMemo } from "react";
import Grid from "@material-ui/core/Grid";
import { AvniSwitch } from "../../common/components/AvniSwitch";
import { AvniSelect } from "../../common/components/AvniSelect";
Expand Down Expand Up @@ -31,6 +31,8 @@ export const LocationConcept = props => {
});
}, []);

const keyValuesArr = useMemo(() => [...props.keyValues], [...props.keyValues]);

React.useEffect(() => {
if (props.isCreatePage || props.keyValues.length === 0) {
updateIsWithinCatchment();
Expand All @@ -42,20 +44,18 @@ export const LocationConcept = props => {
setWithinCatchment(withinCatchment === true || withinCatchment === "true");

const lowest =
props.keyValues.find(keyValue => keyValue.key === "lowestAddressLevelTypeUUIDs") !==
undefined
props.keyValues.find(keyValue => keyValue.key === "lowestAddressLevelTypeUUIDs") !== undefined
? props.keyValues.find(keyValue => keyValue.key === "lowestAddressLevelTypeUUIDs").value
: undefined;
setLowestAddressLevelTypes(lowest !== undefined ? lowest : []);

const highest =
props.keyValues.find(keyValue => keyValue.key === "highestAddressLevelTypeUUID") !==
undefined
props.keyValues.find(keyValue => keyValue.key === "highestAddressLevelTypeUUID") !== undefined
? props.keyValues.find(keyValue => keyValue.key === "highestAddressLevelTypeUUID").value
: undefined;
setHighestAddressLevelType(highest !== undefined ? highest : "");
}
}, [props.keyValues]);
}, [keyValuesArr]);

React.useEffect(() => {
setAddressLevelTypeHierarchy(generateAddressLevelTypeHierarchy());
Expand Down Expand Up @@ -114,12 +114,7 @@ export const LocationConcept = props => {
const updateValue = event === undefined ? true : event.target.checked;
setWithinCatchment(updateValue);
props.inlineConcept
? props.updateConceptKeyValues(
props.groupIndex,
"isWithinCatchment",
updateValue,
props.index
)
? props.updateConceptKeyValues(props.groupIndex, "isWithinCatchment", updateValue, props.index)
: props.updateConceptKeyValues(
{
key: "isWithinCatchment",
Expand All @@ -133,12 +128,7 @@ export const LocationConcept = props => {
const lowestAddressLevelTypeUUIDs = event !== undefined ? event.target.value : uuids;
setLowestAddressLevelTypes(lowestAddressLevelTypeUUIDs);
props.inlineConcept
? props.updateConceptKeyValues(
props.groupIndex,
"lowestAddressLevelTypeUUIDs",
lowestAddressLevelTypeUUIDs,
props.index
)
? props.updateConceptKeyValues(props.groupIndex, "lowestAddressLevelTypeUUIDs", lowestAddressLevelTypeUUIDs, props.index)
: props.updateConceptKeyValues(
{
key: "lowestAddressLevelTypeUUIDs",
Expand All @@ -155,14 +145,10 @@ export const LocationConcept = props => {
intersection = _.intersection(intersection, addressLevelTypeHierarchy.get(levelType));
});

const highestOptions = addressLevelTypes.filter(addressLevelType =>
intersection.includes(addressLevelType.value)
);
const highestOptions = addressLevelTypes.filter(addressLevelType => intersection.includes(addressLevelType.value));
setHighestAddressLevelTypeOptions(highestOptions);
props.error["noCommonAncestor"] =
highestOptions.length === 0 &&
lowestAddressLevelTypes.length !== 1 &&
lowestAddressLevelTypes[0].parent !== null;
highestOptions.length === 0 && lowestAddressLevelTypes.length !== 1 && lowestAddressLevelTypes[0].parent !== null;
if (highestAddressLevelType !== "" && !intersection.includes(highestAddressLevelType)) {
updateHighestAddressLevelType();
}
Expand All @@ -173,12 +159,7 @@ export const LocationConcept = props => {

setHighestAddressLevelType(updateValue);
props.inlineConcept
? props.updateConceptKeyValues(
props.groupIndex,
"highestAddressLevelTypeUUID",
updateValue,
props.index
)
? props.updateConceptKeyValues(props.groupIndex, "highestAddressLevelTypeUUID", updateValue, props.index)
: props.updateConceptKeyValues(
{
key: "highestAddressLevelTypeUUID",
Expand Down Expand Up @@ -214,14 +195,8 @@ export const LocationConcept = props => {
multiple
toolTipKey="APP_DESIGNER_CONCEPT_LOWEST_LOCATION_LEVEL"
/>
{props.error.lowestAddressLevelRequired && (
<FormHelperText error>*Required</FormHelperText>
)}
{props.error.noCommonAncestor && (
<FormHelperText error>
No common higher location between selected lower levels.
</FormHelperText>
)}
{props.error.lowestAddressLevelRequired && <FormHelperText error>*Required</FormHelperText>}
{props.error.noCommonAncestor && <FormHelperText error>No common higher location between selected lower levels.</FormHelperText>}
</Grid>
{lowestAddressLevelTypes.length > 0 && (
<Grid item={true} xs={12} sm={12}>
Expand Down

0 comments on commit 0ad539c

Please sign in to comment.