summaryrefslogtreecommitdiff
path: root/src/api/sagas/objects.js
blob: fd7830b3414283f15928ca79a39c8a8d4471732d (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
import {call, put, select} from "redux-saga/effects";
import {addToSimulationStore, addToUserStore} from "../../actions/objects";
import {getSimulation} from "../routes/simulations";
import {getUser} from "../routes/users";

const selectors = {
    simulation: state => state.objects.simulation,
    user: state => state.objects.user,
    authorization: state => state.objects.authorization,
};

function* fetchAndStoreObject(objectType, id, apiCall, addToStore) {
    const objectStore = yield select(selectors[objectType]);
    if (!objectStore[id]) {
        const object = yield apiCall;
        yield put(addToStore(object));
    }
}

export const fetchAndStoreSimulation = (id) =>
    fetchAndStoreObject("simulation", id, call(getSimulation, id), addToSimulationStore);

export const fetchAndStoreUser = (id) =>
    fetchAndStoreObject("user", id, call(getUser, id), addToUserStore);