Skip to content

Commit 12ce210

Browse files
committed
Creating new objects before mutating anything in uiStateReducer
1 parent 21361ec commit 12ce210

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

packages/react-form-renderer/src/files/ui-state-reducer.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ const uiStateReducer = (state, action) => {
2121
switch (action.type) {
2222
case 'addUIState': {
2323
const {source, uiState} = action;
24+
const newState = {...state};
2425

2526
Object.entries(uiState).forEach(([key, item]) => {
2627
//Create the item object for this item if it doesn't exist
27-
if (!state.fields[key]) state.fields[key] = {};
28+
if (!newState.fields[key]) newState.fields[key] = {};
2829

2930
validAttributes.forEach(type => {
3031
//Don't add uiTypes if they don't exist in the dispatched message
@@ -35,49 +36,62 @@ const uiStateReducer = (state, action) => {
3536
const value = inversedType ? !item[type] : item[type];
3637
type = inversedType || type;
3738

38-
if (!state.fields[key][type]) {
39+
if (!newState.fields[key][type]) {
3940
//If this type doesn't exists for this item, we create a new array with only this source. No need to search fot the source
40-
state.fields[key][type] = [{source, value}];
41+
// newState.fields[key][type] = [{source, value}];
42+
newState.fields[key] = {...newState.fields[key], [type]: [{source, value}]};
4143
} else {
42-
const index = state.fields[key][type].findIndex(item => item.source === source);
44+
newState.fields[key] = {...newState.fields[key]};
45+
const index = newState.fields[key][type].findIndex(item => item.source === source);
4346
if (index !== -1) {
4447
//If this type for this item from this source existed, update the state (value could change if condition went from "then" to "else")
45-
state.fields[key][type][index].value = value;
48+
newState.fields[key][type] = [...newState.fields[key][type]];
49+
newState.fields[key][type][index].value = value;
4650
} else {
4751
//Otherwise, add the state from this source at the begining of the array (i.e. this will supress result from other sources)
48-
state.fields[key][type].unshift({source, value});
52+
newState.fields[key][type] = [{source, value}, ...newState.fields[key][type]];
4953
}
5054
}
5155
});
5256

5357
// Set-instructions are ephemeral and goes into a separate list which is emptied when processed
5458
if (item.set) {
55-
state.setFieldValues = {...state.setFieldValues, [key]: item.set};
59+
newState.setFieldValues = {...newState.setFieldValues, [key]: item.set};
5660
}
5761
});
58-
return {...state};
62+
return {...newState};
5963
}
6064

6165
case 'removeUIState': {
6266
const {source, uiState} = action;
67+
const newState = {...state};
6368

6469
Object.entries(uiState).forEach(([key, item]) => {
6570
//If the field/section doesn't exist, we don't need to do anymore
66-
if (!state.fields[key]) return;
71+
if (!newState.fields[key]) return;
6772

6873
Object.entries(item).forEach(([type, value]) => {
69-
if (!state.fields[key][type]) return;
74+
if (!newState.fields[key][type]) return;
7075

71-
const index = state.fields[key][type].findIndex(item => item.source === source);
72-
if (index !== -1) state.fields[key][type].splice(index, 1);
73-
if (state.fields[key][type].length === 0) delete state.fields[key][type];
76+
const index = newState.fields[key][type].findIndex(item => item.source === source);
77+
if (index !== -1) {
78+
newState.fields[key][type] = [...newState.fields[key][type]];
79+
newState.fields[key][type].splice(index, 1);
80+
}
81+
if (newState.fields[key][type].length === 0) {
82+
newState.fields[key] = {...newState.fields[key]};
83+
delete newState.fields[key][type];
84+
}
7485
});
7586

7687
//If no more uiStateType keys exists for this field, remove the field
77-
if (Object.keys(state.fields[key]).length === 0) delete state.fields[key];
88+
if (Object.keys(newState.fields[key]).length === 0) {
89+
newState.fields = {...newState.fields[key]};
90+
delete newState.fields[key];
91+
}
7892
});
7993

80-
return {...state};
94+
return newState;
8195
}
8296

8397
case 'fieldValuesUpdated': {

0 commit comments

Comments
 (0)