aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/ducks/options.js
diff options
context:
space:
mode:
Diffstat (limited to 'web/src/js/ducks/options.js')
-rw-r--r--web/src/js/ducks/options.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/web/src/js/ducks/options.js b/web/src/js/ducks/options.js
new file mode 100644
index 00000000..0da0fb8c
--- /dev/null
+++ b/web/src/js/ducks/options.js
@@ -0,0 +1,50 @@
+import { fetchApi } from "../utils"
+import * as optionsEditorActions from "./ui/optionsEditor"
+import _ from "lodash"
+
+export const RECEIVE = 'OPTIONS_RECEIVE'
+export const UPDATE = 'OPTIONS_UPDATE'
+export const REQUEST_UPDATE = 'REQUEST_UPDATE'
+
+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 pureSendUpdate (option, value, dispatch) {
+ fetchApi.put('/options', { [option]: value }).then(response => {
+ if (response.status === 200) {
+ dispatch(optionsEditorActions.updateSuccess(option))
+ } else {
+ response.text().then(error => {
+ dispatch(optionsEditorActions.updateError(option, error))
+ })
+ }
+ })
+}
+let sendUpdate = _.throttle(pureSendUpdate, 700, { leading: true, trailing: true })
+
+export function update(option, value) {
+ return dispatch => {
+ dispatch(optionsEditorActions.startUpdate(option, value))
+ sendUpdate(option, value, dispatch);
+ }
+}
+
+export function save() {
+ return dispatch => fetchApi('/options/save', { method: 'POST' })
+}