summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-ui/src/redux/reducers/objects.js
blob: 11f6d35367c981dc62662cc8ca7d1a3ae62cc263 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { combineReducers } from 'redux'
import {
    ADD_ID_TO_STORE_OBJECT_LIST_PROP,
    ADD_PROP_TO_STORE_OBJECT,
    ADD_TO_STORE,
    REMOVE_ID_FROM_STORE_OBJECT_LIST_PROP,
} from '../actions/objects'
import { CPU_UNITS, GPU_UNITS, MEMORY_UNITS, STORAGE_UNITS } from '../../util/unit-specifications'
import { STORE_TOPOLOGY } from '../actions/topologies'

export const objects = combineReducers({
    cpu: object('cpu', CPU_UNITS),
    gpu: object('gpu', GPU_UNITS),
    memory: object('memory', MEMORY_UNITS),
    storage: object('storage', STORAGE_UNITS),
    machine: object('machine'),
    rack: object('rack'),
    tile: object('tile'),
    room: object('room'),
    topology: object('topology'),
    prefab: object('prefab'),
})

function object(type, defaultState = {}) {
    return objectWithId(type, (object) => object._id, defaultState)
}

function objectWithId(type, getId, defaultState = {}) {
    return (state = defaultState, action) => {
        if (action.type === STORE_TOPOLOGY) {
            return { ...state, ...action.entities[type] }
        } else if (action.objectType !== type) {
            return state
        }

        if (action.type === ADD_TO_STORE) {
            return { ...state, [getId(action.object)]: action.object }
        } else if (action.type === ADD_PROP_TO_STORE_OBJECT) {
            return { ...state, [action.objectId]: { ...state[action.objectId], ...action.propObject } }
        } else if (action.type === ADD_ID_TO_STORE_OBJECT_LIST_PROP) {
            return Object.assign({}, state, {
                [action.objectId]: Object.assign({}, state[action.objectId], {
                    [action.propName]: [...state[action.objectId][action.propName], action.id],
                }),
            })
        } else if (action.type === REMOVE_ID_FROM_STORE_OBJECT_LIST_PROP) {
            return Object.assign({}, state, {
                [action.objectId]: Object.assign({}, state[action.objectId], {
                    [action.propName]: state[action.objectId][action.propName].filter((id) => id !== action.id),
                }),
            })
        }

        return state
    }
}