1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
import {fetchApi} from '../utils';
export const REQUEST_SETTINGS = 'REQUEST_SETTINGS'
export const RECEIVE_SETTINGS = 'RECEIVE_SETTINGS'
export const UPDATE_SETTINGS = 'UPDATE_SETTINGS'
const defaultState = {
settings: {},
isFetching: false,
actionsDuringFetch: [],
}
export default function reducer(state = defaultState, action) {
switch (action.type) {
case REQUEST_SETTINGS:
return {
...state,
isFetching: true
}
case RECEIVE_SETTINGS:
let s = {
settings: action.settings,
isFetching: false,
actionsDuringFetch: [],
}
for (action of state.actionsDuringFetch) {
s = reducer(s, action)
}
return s
case UPDATE_SETTINGS:
if (state.isFetching) {
return {
...state,
actionsDuringFetch: [...state.actionsDuringFetch, action]
}
}
return {
...state,
settings: {...state.settings, ...action.settings}
}
default:
return state
}
}
export function handleWsMsg(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 fetchSettings() {
return dispatch => {
dispatch({type: REQUEST_SETTINGS})
return fetchApi('/settings')
.then(response => response.json())
.then(json =>
dispatch({type: RECEIVE_SETTINGS, settings: json.data})
)
// TODO: Error handling
}
}
export function updateSettings(settings) {
fetchApi.put('/settings', settings)
return { type: SET_INTERCEPT }
}
|