Stian Aune
5 years ago
5 changed files with 121 additions and 21 deletions
-
41webui/package-lock.json
-
10webui/src/Actions/authActions.js
-
13webui/src/Actions/lightActions.js
-
57webui/src/Helpers/fetcher.js
-
21webui/src/Reducers/lightReducer.js
@ -1,10 +0,0 @@ |
|||||
import store from "../Reducers" |
|
||||
import {verificationStartedEvent} from "../Reducers/authReducer"; |
|
||||
|
|
||||
const dispatch = store.dispatch; |
|
||||
|
|
||||
export const verify = () => { |
|
||||
dispatch(verificationStartedEvent()); |
|
||||
|
|
||||
|
|
||||
}; |
|
@ -0,0 +1,13 @@ |
|||||
|
import store from "../Reducers"; |
||||
|
import {fetchGet} from "../Helpers/fetcher"; |
||||
|
import {lightsReceivedEvent} from "../Reducers/lightReducer"; |
||||
|
|
||||
|
const dispatch = store.dispatch; |
||||
|
|
||||
|
export function getLights() { |
||||
|
fetchGet("/light/").then(res => { |
||||
|
if (res !== null) { |
||||
|
dispatch(lightsReceivedEvent(res)); |
||||
|
} |
||||
|
}); |
||||
|
} |
@ -0,0 +1,57 @@ |
|||||
|
import store from "../Reducers"; |
||||
|
import {verificationFailedEvent} from "../Reducers/authReducer"; |
||||
|
|
||||
|
const PREFIX = "/api"; |
||||
|
|
||||
|
const dispatch = store.dispatch; |
||||
|
|
||||
|
function formatUrl(url, params = {}) { |
||||
|
const urlWithPrefix = PREFIX + url; |
||||
|
|
||||
|
const queryString = |
||||
|
Object |
||||
|
.keys(params) |
||||
|
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k])); |
||||
|
|
||||
|
if (queryString.length > 0) { |
||||
|
return urlWithPrefix + "?" + queryString; |
||||
|
} |
||||
|
|
||||
|
return urlWithPrefix; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param {Response} res |
||||
|
*/ |
||||
|
function authCheck(res) { |
||||
|
if (res.ok) { |
||||
|
return Promise.resolve(res.json()); |
||||
|
} else { |
||||
|
if (res.status === 403) { |
||||
|
dispatch(verificationFailedEvent()); |
||||
|
|
||||
|
// Allow the promise to sort of fizzle out
|
||||
|
return Promise.resolve(null); |
||||
|
} |
||||
|
|
||||
|
return Promise.reject({ status: res.status, text: res.statusText }); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export function fetchGet(url, params = {}) { |
||||
|
return fetch(formatUrl(url, params), { |
||||
|
method: "GET", |
||||
|
credentials: "include", |
||||
|
}).then(authCheck); |
||||
|
} |
||||
|
|
||||
|
export function fetchPost(url, data) { |
||||
|
return fetch(formatUrl(url), { |
||||
|
method: "POST", |
||||
|
credentials: "include", |
||||
|
headers: { |
||||
|
"Content-Type": "application/json; charset=utf-8", |
||||
|
}, |
||||
|
body: JSON.stringify(data), |
||||
|
}).then(authCheck); |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
const initialState = { |
||||
|
lights: [], |
||||
|
}; |
||||
|
|
||||
|
const LIGHTS_RECEIVED = "LightReducer/Received"; |
||||
|
|
||||
|
const lightReducer = (state = initialState, {type, payload} = {}) => { |
||||
|
switch (type) { |
||||
|
case LIGHTS_RECEIVED: |
||||
|
return { |
||||
|
...state, |
||||
|
lights: payload, |
||||
|
}; |
||||
|
default: |
||||
|
return state; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
export const lightsReceivedEvent = (lights) => ({type: LIGHTS_RECEIVED, payload: lights}); |
||||
|
|
||||
|
export default lightReducer; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue