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