summaryrefslogtreecommitdiff
path: root/src/reducers/states.js
blob: 793f7b7d513254a0cc661893526a53f0659e2c1c (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
31
32
33
import { combineReducers } from "redux";
import { ADD_BATCH_TO_STATES } from "../actions/states";

export const states = combineReducers({
  task: objectStates("task"),
  room: objectStates("room"),
  rack: objectStates("rack"),
  machine: objectStates("machine")
});

function objectStates(type) {
  return (state = {}, action) => {
    if (action.objectType !== type) {
      return state;
    }

    if (action.type === ADD_BATCH_TO_STATES) {
      const batch = {};
      for (let i in action.objects) {
        batch[action.objects[i].tick] = Object.assign(
          {},
          state[action.objects[i].tick],
          batch[action.objects[i].tick],
          { [action.objects[i][action.objectType + "Id"]]: action.objects[i] }
        );
      }

      return Object.assign({}, state, batch);
    }

    return state;
  };
}