diff options
| author | Georgios Andreadis <g.andreadis@student.tudelft.nl> | 2017-08-15 23:24:28 +0300 |
|---|---|---|
| committer | Georgios Andreadis <g.andreadis@student.tudelft.nl> | 2017-09-23 10:05:42 +0200 |
| commit | 91c8088e1d7def9242f60c708cd34f25dcb77d76 (patch) | |
| tree | b68065019692cea5cf6c3d14b811104aff2f0879 /src/api | |
| parent | d7512ace72448242b392299cf459c9c72c8dbee5 (diff) | |
Connect to backend and fetch initial project data
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/index.js | 13 | ||||
| -rw-r--r-- | src/api/routes/auth.js | 9 | ||||
| -rw-r--r-- | src/api/routes/simulations.js | 73 | ||||
| -rw-r--r-- | src/api/routes/users.js | 90 | ||||
| -rw-r--r-- | src/api/sagas/index.js | 9 | ||||
| -rw-r--r-- | src/api/sagas/users.js | 47 | ||||
| -rw-r--r-- | src/api/socket.js | 40 |
7 files changed, 281 insertions, 0 deletions
diff --git a/src/api/index.js b/src/api/index.js index e69de29b..a26422c0 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -0,0 +1,13 @@ +import {sendSocketRequest} from "./socket"; + +export function sendRequest(request) { + return new Promise((resolve, reject) => { + sendSocketRequest(request, response => { + if (response.status.code === 200) { + resolve(response.content); + } else { + reject(response); + } + }) + }); +} diff --git a/src/api/routes/auth.js b/src/api/routes/auth.js new file mode 100644 index 00000000..76a39572 --- /dev/null +++ b/src/api/routes/auth.js @@ -0,0 +1,9 @@ +export function performTokenSignIn(token) { + return new Promise((resolve, reject) => { + window["jQuery"].post( + "/tokensignin", + {idtoken: token}, + data => resolve(data) + ) + }); +} diff --git a/src/api/routes/simulations.js b/src/api/routes/simulations.js new file mode 100644 index 00000000..3c7c748e --- /dev/null +++ b/src/api/routes/simulations.js @@ -0,0 +1,73 @@ +import {sendRequest} from "../index"; + +export function addSimulation(simulation) { + return sendRequest({ + path: "/simulations", + method: "POST", + parameters: { + body: { + simulation + }, + path: {}, + query: {} + } + }); +} + +export function getSimulation(simulationId) { + return sendRequest({ + path: "/simulations/{simulationId}", + method: "GET", + parameters: { + body: {}, + path: { + simulationId + }, + query: {} + } + }); +} + +export function updateSimulation(simulation) { + return sendRequest({ + path: "/simulations/{simulationId}", + method: "PUT", + parameters: { + body: { + simulation + }, + path: { + simulationId: simulation.id + }, + query: {} + } + }); +} + +export function deleteSimulation(simulationId) { + return sendRequest({ + path: "/simulations/{simulationId}", + method: "DELETE", + parameters: { + body: {}, + path: { + simulationId + }, + query: {} + } + }); +} + +export function getAuthorizationsBySimulation(simulationId) { + return sendRequest({ + path: "/simulations/{simulationId}/authorizations", + method: "GET", + parameters: { + body: {}, + path: { + simulationId + }, + query: {} + } + }) +} diff --git a/src/api/routes/users.js b/src/api/routes/users.js new file mode 100644 index 00000000..c91e07b2 --- /dev/null +++ b/src/api/routes/users.js @@ -0,0 +1,90 @@ +import {sendRequest} from "../index"; + +export function getUserByEmail(email) { + return sendRequest({ + path: "/users", + method: "GET", + parameters: { + body: {}, + path: {}, + query: { + email + } + } + }); +} + +export function addUser(user) { + return sendRequest({ + path: "/users", + method: "POST", + parameters: { + body: { + user: user + }, + path: {}, + query: {} + } + }); +} + +export function getUser(userId) { + return sendRequest({ + path: "/users/{userId}", + method: "GET", + parameters: { + body: {}, + path: { + userId + }, + query: {} + } + }); +} + +export function updateUser(userId, user) { + return sendRequest({ + path: "/users/{userId}", + method: "PUT", + parameters: { + body: { + user: { + givenName: user.givenName, + familyName: user.familyName + } + }, + path: { + userId + }, + query: {} + } + }); +} + +export function deleteUser(userId) { + return sendRequest({ + path: "/users/{userId}", + method: "DELETE", + parameters: { + body: {}, + path: { + userId + }, + query: {} + } + }); +} + +export function getAuthorizationsByUser(userId) { + return sendRequest({ + path: "/users/{userId}/authorizations", + method: "GET", + parameters: { + body: {}, + path: { + userId + }, + query: {} + } + }); +} diff --git a/src/api/sagas/index.js b/src/api/sagas/index.js new file mode 100644 index 00000000..ea92533a --- /dev/null +++ b/src/api/sagas/index.js @@ -0,0 +1,9 @@ +import {takeEvery} from "redux-saga/effects"; +import {LOG_IN} from "../../actions/auth"; +import {FETCH_AUTHORIZATIONS_OF_CURRENT_USER} from "../../actions/users"; +import {fetchAuthorizationsOfCurrentUser, fetchLoggedInUser} from "./users"; + +export default function* rootSaga() { + yield takeEvery(LOG_IN, fetchLoggedInUser); + yield takeEvery(FETCH_AUTHORIZATIONS_OF_CURRENT_USER, fetchAuthorizationsOfCurrentUser); +} diff --git a/src/api/sagas/users.js b/src/api/sagas/users.js new file mode 100644 index 00000000..b999b693 --- /dev/null +++ b/src/api/sagas/users.js @@ -0,0 +1,47 @@ +import {call, put} from "redux-saga/effects"; +import {logInSucceeded} from "../../actions/auth"; +import {addToAuthorizationStore, addToSimulationStore, addToUserStore} from "../../actions/object-stores"; +import {fetchAuthorizationsOfCurrentUserSucceeded} from "../../actions/users"; +import {performTokenSignIn} from "../routes/auth"; +import {getSimulation} from "../routes/simulations"; +import {addUser, getAuthorizationsByUser, getUser} from "../routes/users"; + +export function* fetchLoggedInUser(action) { + try { + const tokenResponse = yield call(performTokenSignIn, action.payload.authToken); + let userId = tokenResponse.userId; + + if (tokenResponse.isNewUser) { + 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* fetchAuthorizationsOfCurrentUser(action) { + try { + const authorizations = yield call(getAuthorizationsByUser, action.userId); + + for (const authorization of authorizations) { + yield put(addToAuthorizationStore(authorization)); + + const simulation = yield call(getSimulation, authorization.simulationId); + yield put(addToSimulationStore(simulation)); + + const user = yield call(getUser, authorization.userId); + yield put(addToUserStore(user)); + } + + const authorizationIds = authorizations.map(authorization => ( + [authorization.userId, authorization.simulationId] + )); + + yield put(fetchAuthorizationsOfCurrentUserSucceeded(authorizationIds)); + } catch (error) { + console.log(error); + } +} diff --git a/src/api/socket.js b/src/api/socket.js new file mode 100644 index 00000000..86422808 --- /dev/null +++ b/src/api/socket.js @@ -0,0 +1,40 @@ +import io from "socket.io-client"; +import {getAuthToken} from "../auth/index"; + +let socket; +let requestIdCounter = 0; +const callbacks = {}; + +export function setupSocketConnection(onConnect) { + socket = io.connect("http://localhost:8081"); + socket.on("connect", onConnect); + socket.on("response", onSocketResponse); +} + +export function sendSocketRequest(request, callback) { + if (!socket.connected) { + console.error("Attempted to send request over unconnected socket"); + return; + } + + const newId = requestIdCounter++; + callbacks[newId] = callback; + + request.id = newId; + request.token = getAuthToken(); + + if (!request.isRootRoute) { + request.path = "/v1" + request.path; + } + + socket.emit("request", request); + + console.log("Sent socket request:", request); +} + +function onSocketResponse(json) { + const response = JSON.parse(json); + console.log("Received socket response:", response); + callbacks[response.id](response); + delete callbacks[response.id]; +} |
