diff --git a/webui/package-lock.json b/webui/package-lock.json index dfd30a3..9d72b37 100644 --- a/webui/package-lock.json +++ b/webui/package-lock.json @@ -5470,7 +5470,8 @@ }, "ansi-regex": { "version": "2.1.1", - "bundled": true + "bundled": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -5488,11 +5489,13 @@ }, "balanced-match": { "version": "1.0.0", - "bundled": true + "bundled": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -5505,15 +5508,18 @@ }, "code-point-at": { "version": "1.1.0", - "bundled": true + "bundled": true, + "optional": true }, "concat-map": { "version": "0.0.1", - "bundled": true + "bundled": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", - "bundled": true + "bundled": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -5616,7 +5622,8 @@ }, "inherits": { "version": "2.0.3", - "bundled": true + "bundled": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -5626,6 +5633,7 @@ "is-fullwidth-code-point": { "version": "1.0.0", "bundled": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -5638,17 +5646,20 @@ "minimatch": { "version": "3.0.4", "bundled": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } }, "minimist": { "version": "0.0.8", - "bundled": true + "bundled": true, + "optional": true }, "minipass": { "version": "2.2.4", "bundled": true, + "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -5665,6 +5676,7 @@ "mkdirp": { "version": "0.5.1", "bundled": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -5737,7 +5749,8 @@ }, "number-is-nan": { "version": "1.0.1", - "bundled": true + "bundled": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -5747,6 +5760,7 @@ "once": { "version": "1.4.0", "bundled": true, + "optional": true, "requires": { "wrappy": "1" } @@ -5822,7 +5836,8 @@ }, "safe-buffer": { "version": "5.1.1", - "bundled": true + "bundled": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -5852,6 +5867,7 @@ "string-width": { "version": "1.0.2", "bundled": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -5869,6 +5885,7 @@ "strip-ansi": { "version": "3.0.1", "bundled": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -5907,11 +5924,13 @@ }, "wrappy": { "version": "1.0.2", - "bundled": true + "bundled": true, + "optional": true }, "yallist": { "version": "3.0.2", - "bundled": true + "bundled": true, + "optional": true } } }, diff --git a/webui/src/Actions/authActions.js b/webui/src/Actions/authActions.js deleted file mode 100644 index d1239e3..0000000 --- a/webui/src/Actions/authActions.js +++ /dev/null @@ -1,10 +0,0 @@ -import store from "../Reducers" -import {verificationStartedEvent} from "../Reducers/authReducer"; - -const dispatch = store.dispatch; - -export const verify = () => { - dispatch(verificationStartedEvent()); - - -}; diff --git a/webui/src/Actions/lightActions.js b/webui/src/Actions/lightActions.js new file mode 100644 index 0000000..3095c55 --- /dev/null +++ b/webui/src/Actions/lightActions.js @@ -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)); + } + }); +} diff --git a/webui/src/Helpers/fetcher.js b/webui/src/Helpers/fetcher.js new file mode 100644 index 0000000..8e9e30b --- /dev/null +++ b/webui/src/Helpers/fetcher.js @@ -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); +} diff --git a/webui/src/Reducers/lightReducer.js b/webui/src/Reducers/lightReducer.js new file mode 100644 index 0000000..693dc9a --- /dev/null +++ b/webui/src/Reducers/lightReducer.js @@ -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; \ No newline at end of file