Merge pull request #16418 from GitStartHQ/fix/condition-fields-break-checkbox

This commit is contained in:
Josh 2023-04-26 11:23:17 +01:00 committed by GitHub
commit e3af28c527
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 4 deletions

View File

@ -1,7 +1,7 @@
import updateValues from '../updateValues'; import updateValues from '../updateValues';
describe('ADMIN | COMPONENTS | Permissions | utils | updateValues', () => { describe('ADMIN | COMPONENTS | Permissions | utils | updateValues', () => {
it('should not the conditions values of given object', () => { it('should not update the conditions values of given object', () => {
const simpleObject = { const simpleObject = {
properties: { properties: {
enabled: true, enabled: true,
@ -18,6 +18,31 @@ describe('ADMIN | COMPONENTS | Permissions | utils | updateValues', () => {
expect(updateValues(simpleObject, false)).toEqual(expected); expect(updateValues(simpleObject, false)).toEqual(expected);
}); });
it('should update the conditions values if they are fields names', () => {
const simpleObject = {
conditions: 'test',
properties: {
fields: {
description: false,
restaurant: false,
conditions: false,
},
},
};
const expected = {
conditions: 'test',
properties: {
fields: {
description: true,
restaurant: true,
conditions: true,
},
},
};
expect(updateValues(simpleObject, true)).toEqual(expected);
});
it('set the leafs of an object with the second argument passed to the function', () => { it('set the leafs of an object with the second argument passed to the function', () => {
const complexeObject = { const complexeObject = {
conditions: 'test', conditions: 'test',

View File

@ -8,18 +8,18 @@ import isObject from 'lodash/isObject';
* of an object. * of an object.
* This utility is very helpful when dealing with parent<>children checkboxes * This utility is very helpful when dealing with parent<>children checkboxes
*/ */
const updateValues = (obj, valueToSet) => { const updateValues = (obj, valueToSet, isFieldUpdate = false) => {
return Object.keys(obj).reduce((acc, current) => { return Object.keys(obj).reduce((acc, current) => {
const currentValue = obj[current]; const currentValue = obj[current];
if (current === 'conditions') { if (current === 'conditions' && !isFieldUpdate) {
acc[current] = currentValue; acc[current] = currentValue;
return acc; return acc;
} }
if (isObject(currentValue)) { if (isObject(currentValue)) {
return { ...acc, [current]: updateValues(currentValue, valueToSet) }; return { ...acc, [current]: updateValues(currentValue, valueToSet, current === 'fields') };
} }
acc[current] = valueToSet; acc[current] = valueToSet;