summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-ui/src/redux/sagas/scenarios.js
blob: a5463fa6768386ec01119ea20dd2403f648fe321 (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
57
58
59
60
61
62
63
64
65
import { call, put, select } from 'redux-saga/effects'
import { addPropToStoreObject, addToStore } from '../actions/objects'
import { getProject } from '../../api/projects'
import { fetchAndStoreAllSchedulers, fetchAndStoreAllTraces } from './objects'
import { fetchAndStoreAllTopologiesOfProject } from './topology'
import { addScenario, deleteScenario, updateScenario } from '../../api/scenarios'
import { fetchPortfolioWithScenarios, watchForPortfolioResults } from './portfolios'

export function* onOpenScenarioSucceeded(action) {
    try {
        const project = yield call(getProject, action.projectId)
        yield put(addToStore('project', project))
        yield fetchAndStoreAllTopologiesOfProject(project._id)
        yield fetchAndStoreAllSchedulers()
        yield fetchAndStoreAllTraces()
        yield fetchPortfolioWithScenarios(action.portfolioId)

        // TODO Fetch scenario-specific metrics
    } catch (error) {
        console.error(error)
    }
}

export function* onAddScenario(action) {
    try {
        const scenario = yield call(addScenario, action.scenario.portfolioId, action.scenario)
        yield put(addToStore('scenario', scenario))

        const scenarioIds = yield select((state) => state.objects.portfolio[action.scenario.portfolioId].scenarioIds)
        yield put(
            addPropToStoreObject('portfolio', action.scenario.portfolioId, {
                scenarioIds: scenarioIds.concat([scenario._id]),
            })
        )
        yield watchForPortfolioResults()
    } catch (error) {
        console.error(error)
    }
}

export function* onUpdateScenario(action) {
    try {
        const scenario = yield call(updateScenario, action.scenario._id, action.scenario)
        yield put(addToStore('scenario', scenario))
    } catch (error) {
        console.error(error)
    }
}

export function* onDeleteScenario(action) {
    try {
        yield call(deleteScenario, action.id)

        const currentPortfolioId = yield select((state) => state.currentPortfolioId)
        const scenarioIds = yield select((state) => state.objects.portfolio[currentPortfolioId].scenarioIds)

        yield put(
            addPropToStoreObject('scenario', currentPortfolioId, {
                scenarioIds: scenarioIds.filter((id) => id !== action.id),
            })
        )
    } catch (error) {
        console.error(error)
    }
}