aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/ducks
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2016-06-17 21:03:19 -0700
committerMaximilian Hils <git@maximilianhils.com>2016-06-17 21:03:19 -0700
commitce53799c623a8ac74631f6fc4ab8fb134bff1cc8 (patch)
tree32651f7068556a7c8c4b67aa77097b9d7da4d83f /web/src/js/ducks
parent965b27b52942cc6bf082355a1540bc0f960f1043 (diff)
downloadmitmproxy-ce53799c623a8ac74631f6fc4ab8fb134bff1cc8.tar.gz
mitmproxy-ce53799c623a8ac74631f6fc4ab8fb134bff1cc8.tar.bz2
mitmproxy-ce53799c623a8ac74631f6fc4ab8fb134bff1cc8.zip
simplify settings reducer
Diffstat (limited to 'web/src/js/ducks')
-rw-r--r--web/src/js/ducks/settings.js96
1 files changed, 52 insertions, 44 deletions
diff --git a/web/src/js/ducks/settings.js b/web/src/js/ducks/settings.js
index 6de1c2af..479d1300 100644
--- a/web/src/js/ducks/settings.js
+++ b/web/src/js/ducks/settings.js
@@ -1,64 +1,72 @@
-import { addLogEntry } from './eventLog'
+import {fetchApi} from "../utils";
-export const WS_MSG_TYPE = 'settings'
-export const WS_MSG_CMD_UPDATE = 'update'
+export const REQUEST_SETTINGS = "REQUEST_SETTINGS"
+export const RECEIVE_SETTINGS = "RECEIVE_SETTINGS"
+export const UPDATE_SETTINGS = "UPDATE_SETTINGS"
-export const BEGIN_FETCH = 'SETTINGS_BEGIN_FETCH'
-export const FETCHED = 'SETTINGS_FETCHED'
-export const RECV_WS_MSG = 'SETTINGS_RECV_WS_MSG'
-
-const defaultState = { settings: {}, pendings: null }
+const defaultState = {
+ settings: {},
+ isFetching: false,
+ actionsDuringFetch: [],
+}
-export default function reduce(state = defaultState, action) {
+export default function reducer(state = defaultState, action) {
switch (action.type) {
- case BEGIN_FETCH:
- return { ...state, pendings: [] }
+ case REQUEST_SETTINGS:
+ return {
+ ...state,
+ isFetching: true
+ }
- case FETCHED:
- const pendings = state.pendings || []
- return { ...state, pendings: null, settings: pendings.reduce(reduceData, action.data) }
+ case RECEIVE_SETTINGS:
+ let s = {
+ settings: action.settings,
+ isFetching: false,
+ actionsDuringFetch: [],
+ }
+ for (action of state.actionsDuringFetch) {
+ s = reducer(s, action)
+ }
+ return s
- case RECV_WS_MSG:
- if (state.pendings) {
- return { ...state, pendings: state.pendings.concat(action) }
+ case UPDATE_SETTINGS:
+ if (state.isFetching) {
+ return {
+ ...state,
+ actionsDuringFetch: [...state.actionsDuringFetch, action]
+ }
+ }
+ return {
+ ...state,
+ settings: {...state.settings, ...action.settings}
}
- return { ...state, settings: reduceData(state.settings, action) }
default:
return state
}
}
-function reduceData(data, action) {
- switch (action.cmd) {
-
- case WS_MSG_CMD_UPDATE:
- return { ...data, ...action.data }
-
- default:
- return data
+export function updateSettings(event) {
+ /* This action creator takes all WebSocket events */
+ if (event.cmd === "update") {
+ return {
+ type: UPDATE_SETTINGS,
+ settings: event.data
+ }
}
+ console.error("unknown settings update", event)
}
-export function fetch() {
+export function fetchSettings() {
return dispatch => {
- dispatch({ type: BEGIN_FETCH })
- return $.getJSON('/settings')
- .done(msg => dispatch(handleFetchResponse(msg.data)))
- .fail(error => dispatch(handleFetchError(error)));
- }
-}
-
-export function handleWsMsg(msg) {
- return { type: RECV_WS_MSG, cmd: msg.cmd, data: msg.data }
-}
-
-export function handleFetchResponse(data) {
- return { type: FETCHED, data }
-}
+ dispatch({type: REQUEST_SETTINGS})
-export function handleFetchError(error) {
- // @todo let eventLog subscribe to SettingsActions.FETCH_ERROR
- return addLogEntry(error.stack || error.message || error)
+ return fetchApi("/settings")
+ .then(response => response.json())
+ .then(json =>
+ dispatch({type: RECEIVE_SETTINGS, settings: json.data})
+ )
+ // TODO: Error handling
+ }
}