summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-ui/src/redux/sagas/objects.js
blob: 9b4f80949839a38b7460ed435be1292519be1b5f (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
import { call, put, select, getContext } from 'redux-saga/effects'
import { fetchTopology, updateTopology } from '../../api/topologies'
import { Topology } from '../../util/topology-schema'
import { denormalize, normalize } from 'normalizr'
import { storeTopology } from '../actions/topologies'

/**
 * Fetches and normalizes the topology with the specified identifier.
 */
export const fetchAndStoreTopology = function* (id) {
    const auth = yield getContext('auth')

    let topology = yield select((state) => state.objects.topology[id])
    if (!topology) {
        const newTopology = yield call(fetchTopology, auth, id)
        const { entities } = normalize(newTopology, Topology)
        yield put(storeTopology(entities))
    }

    return topology
}

export const updateTopologyOnServer = function* (id) {
    const topology = yield denormalizeTopology(id)
    const auth = yield getContext('auth')
    yield call(updateTopology, auth, topology)
}

/**
 * Denormalizes the topology representation in order to be stored on the server.
 */
export const denormalizeTopology = function* (id) {
    const objects = yield select((state) => state.objects)
    const topology = objects.topology[id]
    return denormalize(topology, Topology, objects)
}