blob: 7ad97b87699de134cfdd3a33d0d803c2cb614aaf (
plain)
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
78
79
|
import { fetchApi } from '../utils'
import * as websocketActions from './websocket'
import * as msgQueueActions from './msgQueue'
export const MSG_TYPE = 'UPDATE_SETTINGS'
export const DATA_URL = '/settings'
export const RECEIVE = 'RECEIVE'
export const UPDATE = 'UPDATE'
export const REQUEST_UPDATE = 'REQUEST_UPDATE'
export const UNKNOWN_CMD = 'SETTINGS_UNKNOWN_CMD'
const defaultState = {
settings: {},
}
export default function reducer(state = defaultState, action) {
switch (action.type) {
case RECEIVE:
return {
...state,
settings: action.settings,
}
case UPDATE:
return {
...state,
settings: { ...state.settings, ...action.settings },
}
default:
return state
}
}
/**
* @public msgQueue
*/
export function handleWsMsg(msg) {
switch (msg.cmd) {
case websocketActions.CMD_UPDATE:
return updateSettings(msg.data)
default:
console.error('unknown settings update', msg)
return { type: UNKNOWN_CMD, msg }
}
}
/**
* @public
*/
export function update(settings) {
fetchApi.put('/settings', settings)
return { type: REQUEST_UPDATE }
}
/**
* @public websocket
*/
export function fetchData() {
return msgQueueActions.fetchData(MSG_TYPE)
}
/**
* @public msgQueue
*/
export function receiveData(settings) {
return { type: RECEIVE, settings }
}
/**
* @private
*/
export function updateSettings(settings) {
return { type: UPDATE, settings }
}
|