diff options
| author | Georgios Andreadis <g.andreadis@student.tudelft.nl> | 2017-08-25 17:48:12 +0200 |
|---|---|---|
| committer | Georgios Andreadis <g.andreadis@student.tudelft.nl> | 2017-09-23 10:05:44 +0200 |
| commit | c47a27b826f7d76410308a4151611a366f9eaf46 (patch) | |
| tree | c1ca374204714cedabcacb8620848b903a0bf8d6 /src/sagas/users.js | |
| parent | 1ddbbd3563af77a218020021ea50a8832900b4db (diff) | |
Fetch and display datacenter topology
Diffstat (limited to 'src/sagas/users.js')
| -rw-r--r-- | src/sagas/users.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/sagas/users.js b/src/sagas/users.js new file mode 100644 index 00000000..5f9bffa1 --- /dev/null +++ b/src/sagas/users.js @@ -0,0 +1,46 @@ +import {call, put} from "redux-saga/effects"; +import {logInSucceeded} from "../actions/auth"; +import {addToStore} from "../actions/objects"; +import {fetchAuthorizationsOfCurrentUserSucceeded} from "../actions/users"; +import {performTokenSignIn} from "../api/routes/token-signin"; +import {addUser, getAuthorizationsByUser} from "../api/routes/users"; +import {saveAuthLocalStorage} from "../auth/index"; +import {fetchAndStoreSimulation, fetchAndStoreUser} from "./objects"; + +export function* onFetchLoggedInUser(action) { + try { + const tokenResponse = yield call(performTokenSignIn, action.payload.authToken); + let userId = tokenResponse.userId; + + if (tokenResponse.isNewUser) { + saveAuthLocalStorage({authToken: action.payload.authToken}); + const newUser = yield call(addUser, action.payload); + userId = newUser.id; + } + + yield put(logInSucceeded(Object.assign({userId}, action.payload))); + } catch (error) { + console.log(error); + } +} + +export function* onFetchAuthorizationsOfCurrentUser(action) { + try { + const authorizations = yield call(getAuthorizationsByUser, action.userId); + + for (const authorization of authorizations) { + yield put(addToStore("authorization", authorization)); + + yield fetchAndStoreSimulation(authorization.simulationId); + yield fetchAndStoreUser(authorization.userId); + } + + const authorizationIds = authorizations.map(authorization => ( + [authorization.userId, authorization.simulationId] + )); + + yield put(fetchAuthorizationsOfCurrentUserSucceeded(authorizationIds)); + } catch (error) { + console.log(error); + } +} |
