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

41 lines
973 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
2020-11-15 18:48:32 +00:00
export const saveShownOnboarding = () => (dispatch) => {
dispatch(changeSetting(['shownOnboarding'], true))
dispatch(saveSettings())
}
export const changeSetting = (path, value) => (dispatch) => {
dispatch({
type: SETTING_CHANGE,
path,
value,
})
dispatch(saveSettings())
}
/**
*
*/
export const saveSettings = () => (dispatch, getState) => {
debouncedSave(dispatch, getState)
2020-04-09 20:18:14 +01:00
}
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(() => { /* */ })
}, 350, { trailing: true })