summaryrefslogtreecommitdiff
path: root/src/reducers/objects.js
blob: 4fbffea6d75ee8cd58fa18e99192956b85321af0 (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
import {combineReducers} from "redux";
import {ADD_PROP_TO_STORE_OBJECT, ADD_TO_STORE} from "../actions/objects";

export const objects = combineReducers({
    simulation: object("simulation"),
    user: object("user"),
    authorization: objectWithId("authorization", object => [object.userId, object.simulationId]),
    failureModel: object("failureModel"),
    cpu: object("cpu"),
    gpu: object("gpu"),
    memory: object("memory"),
    storage: object("storage"),
    machine: object("machine"),
    rack: object("rack"),
    coolingItem: object("coolingItem"),
    psu: object("psu"),
    tile: object("tile"),
    room: object("room"),
    datacenter: object("datacenter"),
    section: object("section"),
    path: object("path"),
});

function object(type) {
    return objectWithId(type, object => object.id);
}

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

        if (action.type === ADD_TO_STORE) {
            return Object.assign(
                state,
                {[getId(action.object)]: action.object}
            );
        } else if (action.type === ADD_PROP_TO_STORE_OBJECT) {
            return Object.assign(
                state,
                {[action.objectId]: Object.assign(state[action.objectId], action.propObject)}
            );
        }

        return state;
    };
}