2020-11-23 06:28:15 +00:00
|
|
|
import { isBoolean, get, map, set } from "lodash";
|
2020-11-19 03:32:58 +00:00
|
|
|
import { HiddenType } from "./BaseControl";
|
|
|
|
|
|
2021-04-05 10:11:04 +00:00
|
|
|
export const evaluateCondtionWithType = (
|
|
|
|
|
conditions: Array<boolean> | undefined,
|
|
|
|
|
type: string | undefined,
|
|
|
|
|
) => {
|
|
|
|
|
if (conditions) {
|
|
|
|
|
let flag;
|
|
|
|
|
//this is where each conditions gets evaluated
|
|
|
|
|
if (conditions.length > 1) {
|
|
|
|
|
if (type === "AND") {
|
|
|
|
|
flag = conditions.reduce((acc: any, item: boolean) => {
|
|
|
|
|
return acc && item;
|
|
|
|
|
}, conditions[0]);
|
|
|
|
|
} else if (type === "OR") {
|
|
|
|
|
flag = conditions.reduce((acc: any, item: boolean) => {
|
|
|
|
|
return acc || item;
|
|
|
|
|
}, undefined);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
flag = conditions[0];
|
|
|
|
|
}
|
|
|
|
|
return flag;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const isHiddenConditionsEvaluation = (
|
|
|
|
|
values: any,
|
|
|
|
|
hidden?: HiddenType,
|
|
|
|
|
): any => {
|
|
|
|
|
if (!!hidden && !isBoolean(hidden)) {
|
|
|
|
|
//if nested condtions are there recursively from bottom to top call this function on each condtion
|
|
|
|
|
let conditionType, conditions;
|
|
|
|
|
if ("conditionType" in hidden) {
|
|
|
|
|
conditionType = hidden.conditionType;
|
|
|
|
|
}
|
|
|
|
|
if ("conditions" in hidden) {
|
|
|
|
|
conditions = hidden.conditions;
|
|
|
|
|
}
|
|
|
|
|
if (Array.isArray(conditions)) {
|
|
|
|
|
conditions = conditions.map((rule: any) => {
|
|
|
|
|
return isHiddenConditionsEvaluation(values, rule);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return caculateIsHidden(values, hidden);
|
|
|
|
|
}
|
|
|
|
|
return evaluateCondtionWithType(conditions, conditionType);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const caculateIsHidden = (values: any, hiddenConfig?: HiddenType) => {
|
2020-11-19 03:32:58 +00:00
|
|
|
if (!!hiddenConfig && !isBoolean(hiddenConfig)) {
|
2021-04-05 10:11:04 +00:00
|
|
|
let valueAtPath;
|
|
|
|
|
let value, comparison;
|
|
|
|
|
if ("path" in hiddenConfig) {
|
|
|
|
|
valueAtPath = get(values, hiddenConfig.path);
|
|
|
|
|
}
|
|
|
|
|
if ("value" in hiddenConfig) {
|
|
|
|
|
value = hiddenConfig.value;
|
|
|
|
|
}
|
|
|
|
|
if ("comparison" in hiddenConfig) {
|
|
|
|
|
comparison = hiddenConfig.comparison;
|
|
|
|
|
}
|
2020-11-19 03:32:58 +00:00
|
|
|
|
2021-04-05 10:11:04 +00:00
|
|
|
switch (comparison) {
|
2020-11-19 03:32:58 +00:00
|
|
|
case "EQUALS":
|
|
|
|
|
return valueAtPath === value;
|
|
|
|
|
case "NOT_EQUALS":
|
|
|
|
|
return valueAtPath !== value;
|
|
|
|
|
case "GREATER":
|
|
|
|
|
return valueAtPath > value;
|
|
|
|
|
case "LESSER":
|
|
|
|
|
return valueAtPath < value;
|
2021-02-10 05:57:21 +00:00
|
|
|
case "IN":
|
|
|
|
|
return Array.isArray(value) && value.includes(valueAtPath);
|
|
|
|
|
case "NOT_IN":
|
|
|
|
|
return Array.isArray(value) && !value.includes(valueAtPath);
|
2020-11-19 03:32:58 +00:00
|
|
|
default:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-05 10:11:04 +00:00
|
|
|
};
|
2020-11-19 03:32:58 +00:00
|
|
|
|
2021-04-05 10:11:04 +00:00
|
|
|
export const isHidden = (values: any, hiddenConfig?: HiddenType) => {
|
|
|
|
|
if (!!hiddenConfig && !isBoolean(hiddenConfig)) {
|
|
|
|
|
if ("conditionType" in hiddenConfig) {
|
|
|
|
|
//check if nested conditions exist
|
|
|
|
|
return isHiddenConditionsEvaluation(values, hiddenConfig);
|
|
|
|
|
} else {
|
|
|
|
|
return caculateIsHidden(values, hiddenConfig);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-19 03:32:58 +00:00
|
|
|
return !!hiddenConfig;
|
|
|
|
|
};
|
2020-11-23 06:28:15 +00:00
|
|
|
|
|
|
|
|
export const getConfigInitialValues = (config: Record<string, any>[]) => {
|
|
|
|
|
const configInitialValues = {};
|
2021-02-15 16:13:20 +00:00
|
|
|
if (!Array.isArray(config)) return configInitialValues;
|
|
|
|
|
|
2020-11-23 06:28:15 +00:00
|
|
|
const parseConfig = (section: any): any => {
|
|
|
|
|
return map(section.children, (subSection: any) => {
|
|
|
|
|
if ("children" in subSection) {
|
|
|
|
|
return parseConfig(subSection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (subSection.initialValue) {
|
|
|
|
|
if (subSection.controlType === "KEYVALUE_ARRAY") {
|
|
|
|
|
subSection.initialValue.forEach(
|
|
|
|
|
(initialValue: string | number, index: number) => {
|
|
|
|
|
const configProperty = subSection.configProperty.replace(
|
|
|
|
|
"*",
|
|
|
|
|
index,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
set(configInitialValues, configProperty, initialValue);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
set(
|
|
|
|
|
configInitialValues,
|
|
|
|
|
subSection.configProperty,
|
|
|
|
|
subSection.initialValue,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config.forEach((section: any) => {
|
|
|
|
|
parseConfig(section);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return configInitialValues;
|
|
|
|
|
};
|