summaryrefslogtreecommitdiff
path: root/src/api/sagas/objects.js
diff options
context:
space:
mode:
authorGeorgios Andreadis <g.andreadis@student.tudelft.nl>2017-08-16 22:55:16 +0300
committerGeorgios Andreadis <g.andreadis@student.tudelft.nl>2017-09-23 10:05:42 +0200
commit07195f3762b6a8a7dfb44c2231db58c5be13c43f (patch)
tree2b4640457f70b55a97aff22fbe617e4b8538464a /src/api/sagas/objects.js
parent91c8088e1d7def9242f60c708cd34f25dcb77d76 (diff)
Rename project to sim and enable sim-adding
Diffstat (limited to 'src/api/sagas/objects.js')
-rw-r--r--src/api/sagas/objects.js24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/api/sagas/objects.js b/src/api/sagas/objects.js
new file mode 100644
index 00000000..9bc98e6f
--- /dev/null
+++ b/src/api/sagas/objects.js
@@ -0,0 +1,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.simulations,
+ user: state => state.objects.users,
+ authorization: state => state.objects.authorizations,
+};
+
+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);