Skip to content

Commit 32eadd4

Browse files
committed
issue #2189 - item 2 - fixed mutating state for UPDATE_PHASE_SUCCESS reducer
1 parent 64b58ad commit 32eadd4

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/projects/reducers/project.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,6 @@ function getProductInPhases(phases, phaseId, productId) {
9595
return product
9696
}
9797

98-
/**
99-
* Update phase from server result
100-
*
101-
* @param {Array} phases phases list in store
102-
* @param {Object} phase phase
103-
* @param {Number} phaseIndex index of phase to update
104-
*
105-
* @return {Array} phases
106-
*/
107-
function updateSomeBasicInfoIntoPhase(phases, phase, phaseIndex) {
108-
if (phase && phaseIndex >= 0) {
109-
_.assign(phases[phaseIndex], phase)
110-
}
111-
return _.cloneDeep(phases || [])
112-
}
113-
11498
export const projectState = function (state=initialState, action) {
11599

116100
switch (action.type) {
@@ -129,11 +113,25 @@ export const projectState = function (state=initialState, action) {
129113
lastUpdated: new Date()
130114
})
131115

132-
case UPDATE_PHASE_SUCCESS:
116+
case UPDATE_PHASE_SUCCESS: {
117+
// as we additionally loaded products to the phase object we have to keep them
118+
// note that we keep them as they are without creation a new copy
119+
const phase = {
120+
...action.payload,
121+
products: state.phases[action.payload.phaseIndex].products
122+
}
123+
const phaseNonDirty = {
124+
// for non-dirty version we make sure that dont' have the same objects with phase
125+
..._.cloneDeep(action.payload),
126+
products: state.phasesNonDirty[action.payload.phaseIndex].products
127+
}
128+
133129
return update(state, {
134-
processing: { $set: false},
135-
phases: { $set: updateSomeBasicInfoIntoPhase(state.phases, action.payload, action.payload.phaseIndex || 0)}
130+
processing: { $set: false },
131+
phases: { $splice: [[action.payload.phaseIndex, 1, phase]] },
132+
phasesNonDirty: { $splice: [[action.payload.phaseIndex, 1, phaseNonDirty]] },
136133
})
134+
}
137135

138136
case LOAD_PROJECT_TEMPLATE_SUCCESS:
139137
return {...state,

0 commit comments

Comments
 (0)