2017-01-09 05:00:55 -08:00
|
|
|
import axios from 'axios';
|
2017-10-08 12:47:15 -07:00
|
|
|
import { debounce } from 'lodash';
|
2017-01-09 05:00:55 -08:00
|
|
|
|
|
|
|
export const SETTING_CHANGE = 'SETTING_CHANGE';
|
2017-10-08 12:47:15 -07:00
|
|
|
export const SETTING_SAVE = 'SETTING_SAVE';
|
2017-01-09 05:00:55 -08:00
|
|
|
|
|
|
|
export function changeSetting(key, value) {
|
2017-06-14 09:59:02 -07:00
|
|
|
return dispatch => {
|
|
|
|
dispatch({
|
|
|
|
type: SETTING_CHANGE,
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch(saveSettings());
|
2017-01-10 08:25:10 -08:00
|
|
|
};
|
|
|
|
};
|
2017-01-09 05:00:55 -08:00
|
|
|
|
2017-10-08 12:47:15 -07:00
|
|
|
const debouncedSave = debounce((dispatch, getState) => {
|
|
|
|
if (getState().getIn(['settings', 'saved'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = getState().get('settings').filter((_, key) => key !== 'saved').toJS();
|
|
|
|
|
|
|
|
axios.put('/api/web/settings', { data }).then(() => dispatch({ type: SETTING_SAVE }));
|
|
|
|
}, 5000, { trailing: true });
|
|
|
|
|
2017-01-10 08:25:10 -08:00
|
|
|
export function saveSettings() {
|
2017-10-08 12:47:15 -07:00
|
|
|
return (dispatch, getState) => debouncedSave(dispatch, getState);
|
2017-01-09 05:00:55 -08:00
|
|
|
};
|