闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
884 B

  1. import api from '../api';
  2. import { debounce } from 'lodash';
  3. import { showAlertForError } from './alerts';
  4. export const SETTING_CHANGE = 'SETTING_CHANGE';
  5. export const SETTING_SAVE = 'SETTING_SAVE';
  6. export function changeSetting(path, value) {
  7. return dispatch => {
  8. dispatch({
  9. type: SETTING_CHANGE,
  10. path,
  11. value,
  12. });
  13. dispatch(saveSettings());
  14. };
  15. };
  16. const debouncedSave = debounce((dispatch, getState) => {
  17. if (getState().getIn(['settings', 'saved'])) {
  18. return;
  19. }
  20. const data = getState().get('settings').filter((_, path) => path !== 'saved').toJS();
  21. api().put('/api/web/settings', { data })
  22. .then(() => dispatch({ type: SETTING_SAVE }))
  23. .catch(error => dispatch(showAlertForError(error)));
  24. }, 5000, { trailing: true });
  25. export function saveSettings() {
  26. return (dispatch, getState) => debouncedSave(dispatch, getState);
  27. };