2018-01-09 04:01:33 +09:00
|
|
|
import api from '../api';
|
2017-10-09 04:47:15 +09:00
|
|
|
import { debounce } from 'lodash';
|
2018-04-02 21:51:02 +09:00
|
|
|
import { showAlertForError } from './alerts';
|
2017-01-09 22:00:55 +09:00
|
|
|
|
|
|
|
export const SETTING_CHANGE = 'SETTING_CHANGE';
|
2017-10-09 04:47:15 +09:00
|
|
|
export const SETTING_SAVE = 'SETTING_SAVE';
|
2017-01-09 22:00:55 +09:00
|
|
|
|
2018-01-02 21:50:54 +09:00
|
|
|
export function changeSetting(path, value) {
|
2017-06-15 01:59:02 +09:00
|
|
|
return dispatch => {
|
|
|
|
dispatch({
|
|
|
|
type: SETTING_CHANGE,
|
2018-01-02 21:50:54 +09:00
|
|
|
path,
|
2017-06-15 01:59:02 +09:00
|
|
|
value,
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch(saveSettings());
|
2017-01-11 01:25:10 +09:00
|
|
|
};
|
|
|
|
};
|
2017-01-09 22:00:55 +09:00
|
|
|
|
2017-10-09 04:47:15 +09:00
|
|
|
const debouncedSave = debounce((dispatch, getState) => {
|
|
|
|
if (getState().getIn(['settings', 'saved'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-02 21:50:54 +09:00
|
|
|
const data = getState().get('settings').filter((_, path) => path !== 'saved').toJS();
|
2017-10-09 04:47:15 +09:00
|
|
|
|
2018-04-02 21:51:02 +09:00
|
|
|
api(getState).put('/api/web/settings', { data })
|
|
|
|
.then(() => dispatch({ type: SETTING_SAVE }))
|
|
|
|
.catch(error => dispatch(showAlertForError(error)));
|
2017-10-09 04:47:15 +09:00
|
|
|
}, 5000, { trailing: true });
|
|
|
|
|
2017-01-11 01:25:10 +09:00
|
|
|
export function saveSettings() {
|
2017-10-09 04:47:15 +09:00
|
|
|
return (dispatch, getState) => debouncedSave(dispatch, getState);
|
2017-01-09 22:00:55 +09:00
|
|
|
};
|