summaryrefslogtreecommitdiff
path: root/frontend/src/reducers/states.js
blob: c9bb4158f5d7c0ecdc4c05c201e938e0d6fa1b51 (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
import { combineReducers } from 'redux'
import { ADD_BATCH_TO_STATES } from '../actions/states'

export const states = combineReducers({
    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
    }
}