gab-social/app/javascript/gabsocial/actions/settings.js

35 lines
847 B
JavaScript
Raw Normal View History

2020-04-09 20:18:14 +01:00
import api from '../api'
import debounce from 'lodash.debounce'
import { me } from '../initial_state'
2019-07-02 08:10:25 +01:00
2020-04-09 20:18:14 +01:00
export const SETTING_CHANGE = 'SETTING_CHANGE'
export const SETTING_SAVE = 'SETTING_SAVE'
2019-07-02 08:10:25 +01:00
export function changeSetting(path, value) {
return dispatch => {
dispatch({
type: SETTING_CHANGE,
path,
value,
2020-04-09 20:18:14 +01:00
})
2019-07-02 08:10:25 +01:00
2020-04-09 20:18:14 +01:00
dispatch(saveSettings())
}
}
2019-07-02 08:10:25 +01:00
const debouncedSave = debounce((dispatch, getState) => {
2020-04-09 20:18:14 +01:00
if (!me) return
2019-07-02 08:10:25 +01:00
2020-04-09 20:18:14 +01:00
if (getState().getIn(['settings', 'saved'])) return
2019-07-02 08:10:25 +01:00
2020-04-09 20:18:14 +01:00
const data = getState().get('settings').filter((_, path) => path !== 'saved').toJS()
2019-07-02 08:10:25 +01:00
api().put('/api/web/settings', { data })
.then(() => dispatch({ type: SETTING_SAVE }))
2020-04-09 20:18:14 +01:00
.catch(() => { /* */ })
2020-05-15 00:56:27 +01:00
}, 500, { trailing: true })
2019-07-02 08:10:25 +01:00
export function saveSettings() {
2020-04-09 20:18:14 +01:00
return (dispatch, getState) => debouncedSave(dispatch, getState)
}