aboutsummaryrefslogtreecommitdiff
path: root/modern/src/reducers/index.js
blob: 4593d1f36b4377a7e893d5822252911450a86a26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const initialState = {
  devices: new Map(),
  positions: new Map()
};

function updateMap(map, array, key) {
  for (let value of array) {
    map.set(value[key], value);
  }
  return map;
}

function rootReducer(state = initialState, action) {
  switch (action.type) {
    case 'UPDATE_DEVICES':
      return Object.assign({}, {
        ...state,
        devices: updateMap(state.devices, action.devices, 'id')
      });
    case 'UPDATE_POSITIONS':
      return Object.assign({}, {
        ...state,
        positions: updateMap(state.positions, action.positions, 'deviceId')
      });
    default:
      return state;
  }
}

export default rootReducer