summaryrefslogtreecommitdiff
path: root/frontend/src/sagas/simulations.js
blob: 9e914b8547ac7edd4a1918af8d85e393cbb361c3 (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
import { call, put } from 'redux-saga/effects'
import { addToStore } from '../actions/objects'
import { addSimulationSucceeded, deleteSimulationSucceeded } from '../actions/simulations'
import { addSimulation, deleteSimulation, getSimulation } from '../api/routes/simulations'
import { fetchLatestDatacenter } from './topology'

export function* onOpenSimulationSucceeded(action) {
    try {
        const simulation = yield call(getSimulation, action.id)
        yield put(addToStore('simulation', simulation))

        yield fetchLatestDatacenter(action.id)
    } catch (error) {
        console.error(error)
    }
}

export function* onSimulationAdd(action) {
    try {
        const simulation = yield call(addSimulation, { name: action.name })
        yield put(addToStore('simulation', simulation))

        const authorization = {
            simulationId: simulation.id,
            userId: action.userId,
            authorizationLevel: 'OWN',
        }
        yield put(addToStore('authorization', authorization))
        yield put(
            addSimulationSucceeded([authorization.userId, authorization.simulationId]),
        )
    } catch (error) {
        console.error(error)
    }
}

export function* onSimulationDelete(action) {
    try {
        yield call(deleteSimulation, action.id)
        yield put(deleteSimulationSucceeded(action.id))
    } catch (error) {
        console.error(error)
    }
}