Skip to content

Commit

Permalink
fix: load dependent fields on form create
Browse files Browse the repository at this point in the history
  • Loading branch information
harshpatel-crest committed Jun 11, 2021
1 parent d1e37de commit b85516d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
38 changes: 37 additions & 1 deletion ui/src/main/webapp/components/BaseFormView.jsx
Expand Up @@ -135,7 +135,7 @@ class BaseFormView extends PureComponent {
this.isOAuth = false;
this.isAuthVal = false;
this.authMap = {};
const temState = {};
let temState = {};
const temEntities = [];

this.entities.forEach((e) => {
Expand Down Expand Up @@ -332,6 +332,42 @@ class BaseFormView extends PureComponent {

this.entities = temEntities;

// flatten the dependencyMap to remove redundant iterations for resolving them
// one-by-one in following loop
let flattenedMap = {};
this.dependencyMap.forEach((value) => {
flattenedMap = { ...flattenedMap, ...value };
});

const changes = {};
Object.keys(flattenedMap).forEach((field) => {
const values = flattenedMap[field];
const data = {};
let load = true;

values.forEach((dependency) => {
const required = !!this.entities.find((e) => {
return e.field === dependency;
}).required;

const currentValue = temState[dependency].value;
if (required && !currentValue) {
load = false;
data[dependency] = null;
} else {
data[dependency] = currentValue;
}
});

if (load) {
changes[field] = {
dependencyValues: { $set: data },
};
}
});

// apply dependency field changes in state
temState = update(temState, changes);
this.state = {
data: temState,
errorMsg: '',
Expand Down
2 changes: 1 addition & 1 deletion ui/src/main/webapp/util/util.js
Expand Up @@ -72,7 +72,7 @@ export function filterByDenyList(fields, denyList) {

export function filterResponse(items, labelField, allowList, denyList) {
let newItems = items.map((item) => {
return { label: labelField ? item[labelField] : item.name, value: item.name };
return { label: labelField ? item.content?.[labelField] : item.name, value: item.name };
});

if (allowList) {
Expand Down

0 comments on commit b85516d

Please sign in to comment.