2017-10-08 19:47:15 +00:00
|
|
|
import { debounce } from 'lodash';
|
2023-05-28 14:38:10 +00:00
|
|
|
|
|
|
|
import api from '../api';
|
|
|
|
|
2019-02-10 20:28:29 +00:00
|
|
|
import { showAlertForError } from './alerts';
|
2017-01-09 13:00:55 +00:00
|
|
|
|
|
|
|
export const SETTING_CHANGE = 'SETTING_CHANGE';
|
2017-10-08 19:47:15 +00:00
|
|
|
export const SETTING_SAVE = 'SETTING_SAVE';
|
2017-01-09 13:00:55 +00:00
|
|
|
|
2018-01-09 15:48:14 +00:00
|
|
|
export function changeSetting(path, value) {
|
2017-06-14 16:59:02 +00:00
|
|
|
return dispatch => {
|
|
|
|
dispatch({
|
|
|
|
type: SETTING_CHANGE,
|
2018-01-09 15:48:14 +00:00
|
|
|
path,
|
2017-06-14 16:59:02 +00:00
|
|
|
value,
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch(saveSettings());
|
2017-01-10 16:25:10 +00:00
|
|
|
};
|
2023-02-03 19:52:07 +00:00
|
|
|
}
|
2017-01-09 13:00:55 +00:00
|
|
|
|
2017-10-08 19:47:15 +00:00
|
|
|
const debouncedSave = debounce((dispatch, getState) => {
|
2024-05-16 13:01:01 +00:00
|
|
|
if (getState().getIn(['settings', 'saved']) || !getState().getIn(['meta', 'me'])) {
|
2017-10-08 19:47:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-09 15:48:14 +00:00
|
|
|
const data = getState().get('settings').filter((_, path) => path !== 'saved').toJS();
|
2017-10-08 19:47:15 +00:00
|
|
|
|
2023-11-15 11:24:59 +00:00
|
|
|
api().put('/api/web/settings', { data })
|
2019-02-10 20:28:29 +00:00
|
|
|
.then(() => dispatch({ type: SETTING_SAVE }))
|
|
|
|
.catch(error => dispatch(showAlertForError(error)));
|
2017-10-08 19:47:15 +00:00
|
|
|
}, 5000, { trailing: true });
|
|
|
|
|
2017-01-10 16:25:10 +00:00
|
|
|
export function saveSettings() {
|
2017-10-08 19:47:15 +00:00
|
|
|
return (dispatch, getState) => debouncedSave(dispatch, getState);
|
2023-02-03 19:52:07 +00:00
|
|
|
}
|