Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/nested conditions #1047

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions designer/client/conditions/SelectConditions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,22 @@ class SelectConditions extends React.Component<Props, State> {
const { data } = this.context;
const fields: any = Object.values(this.fieldsForPath(path));
const { conditions = [] } = data;
var conditionsForPath: any[] = [];

let conditionsForPath: any[] = [];
const stringConditions = conditions.filter(
(condition) => typeof condition.value === "string"
);
// nested conditions have their own data structure, so need to be considered separately
const nestedConditions = conditions.filter((condition) =>
condition.value.conditions
.map((innerCondition) => innerCondition.hasOwnProperty("conditionName"))
.includes(true)
);
const objectConditions = conditions.filter(
(condition) => typeof condition.value !== "string"
(condition) =>
typeof condition.value !== "string" &&
!nestedConditions.find(
(nestedCondition) => nestedCondition.name === condition.name
)
);

fields.forEach((field) => {
Expand All @@ -97,6 +106,11 @@ class SelectConditions extends React.Component<Props, State> {
conditionsForPath
);
this.handleConditions(objectConditions, field.name, conditionsForPath);
this.handleNestedConditions(
nestedConditions,
field.name,
conditionsForPath
);
});

return conditionsForPath;
Expand Down Expand Up @@ -152,6 +166,33 @@ class SelectConditions extends React.Component<Props, State> {
);
var a = "";
}
// loops through nested conditions, checking the referenced condition against the current field
handleNestedConditions(
nestedConditions: ConditionData[],
fieldName: string,
conditionsForPath: any[]
) {
nestedConditions.forEach((condition) => {
condition.value.conditions.forEach((innerCondition) => {
if (innerCondition.hasOwnProperty("conditionName")) {
let nestedCondition = conditionsForPath.find(
(conditionForPath) =>
conditionForPath.name === innerCondition.conditionName
);
if (nestedCondition) {
conditionsForPath.push(condition);
}
} else {
this.checkAndAddCondition(
condition,
fieldName,
innerCondition.field.name,
conditionsForPath
);
}
});
});
}

checkAndAddCondition(
conditionToAdd,
Expand Down