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