Skip to content

Commit

Permalink
machines: cleanup UPDATE_STORAGE_POOLS and UPDATE_STORAGE_VOLUMES red…
Browse files Browse the repository at this point in the history
…ucers

UPDATE_STORAGE_POOLS reducer was overwriting storage volumes of each pool with
empty array each time it was called.
Then it expected that the UPDATE_STORAGE_VOLUMES re-filled the arrays.
This approach is not correct, since it causes component rerendering without actual
changes in the state and also in some cases we might even read wrong data,
if we try to read the state, after UPDATE_STORAGE_POOLS ran but not
UPDATE_STORAGE_VOLUMEs.
  • Loading branch information
KKoukiou committed Sep 21, 2018
1 parent 1d03fa9 commit 41e9211
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions pkg/machines/reducers.es6
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,25 @@ function storagePools(state, action) {
const { connectionName, pools } = action.payload;

const newState = Object.assign({}, state);
newState[connectionName] = {};
if (!(connectionName in newState))
newState[connectionName] = {};
else
newState[connectionName] = Object.assign({}, state[connectionName]);

// Array of strings (pool names)
pools.forEach(poolName => {
newState[connectionName][poolName] = []; // will be filled by UPDATE_STORAGE_VOLUMES
});
// Delete pools from state that are not in the payload
for (var poolCurrent in newState[connectionName]) {
if (!pools.includes(poolCurrent)) {
delete newState[connectionName][poolCurrent];
}
}

// Add new pools to state
for (var i in pools) {
let poolName = pools[i];
if (!(poolName in newState[connectionName])) {
newState[connectionName][poolName] = [];
}
}

return newState;
}
Expand Down

0 comments on commit 41e9211

Please sign in to comment.