aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/ducks/options.js
blob: 48e3708b95aa13822da476477c7f36c4ccce484b (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
import { fetchApi } from '../utils'
import  * as optionActions from './ui/option'

export const RECEIVE        = 'OPTIONS_RECEIVE'
export const UPDATE         = 'OPTIONS_UPDATE'
export const REQUEST_UPDATE = 'REQUEST_UPDATE'
export const UNKNOWN_CMD    = 'OPTIONS_UNKNOWN_CMD'

const defaultState = {

}

export default function reducer(state = defaultState, action) {
    switch (action.type) {

        case RECEIVE:
            return action.data

        case UPDATE:
            return {
                ...state,
                ...action.data,
            }

        default:
            return state
    }
}

export function update(options) {
    return dispatch => {
        let option = Object.keys(options)[0]
        dispatch({ type: optionActions.OPTION_UPDATE_START, option, value: options[option] })
        fetchApi.put('/options', options).then(response => {
            if (response.status === 200) {
                dispatch({ type: optionActions.OPTION_UPDATE_SUCCESS, option})
            } else {
                response.text().then( text => {
                    dispatch({type: optionActions.OPTION_UPDATE_ERROR, error: text, option})
                })
            }
        })
    }
}