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

54 lines
1.3 KiB
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'
import { getWindowDimension } from '../utils/is_mobile'
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 const WINDOW_DIMENSION_CHANGE = 'WINDOW_DIMENSION_CHANGE'
export const saveWindowDimensions = () => (dispatch) => {
const { width, height } = getWindowDimension()
dispatch({
type: WINDOW_DIMENSION_CHANGE,
width,
height,
})
}
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 })