diff --git a/app/gabsocial/actions/preferences.js b/app/gabsocial/actions/preferences.js index 1395ce1da..23b9d9081 100644 --- a/app/gabsocial/actions/preferences.js +++ b/app/gabsocial/actions/preferences.js @@ -1,6 +1,11 @@ import api from '../api'; +import { fetchMeSuccess } from 'gabsocial/actions/me'; +import { debounce } from 'lodash'; +import { showAlertForError } from './alerts'; export const MASTO_PREFS_FETCH_SUCCESS = 'MASTO_PREFS_FETCH_SUCCESS'; +export const PREFERENCE_CHANGE = 'PREFERENCE_CHANGE'; +export const PREFERENCE_SAVE = 'PREFERENCE_SAVE'; export const FE_NAME = 'soapbox_fe'; @@ -21,3 +26,37 @@ export function mastoFetchPrefsSuccess(prefs) { prefs, }; } + +export function changePreference(path, value) { + return dispatch => { + dispatch({ + type: PREFERENCE_CHANGE, + path, + value, + }); + + dispatch(savePreferences()); + }; +}; + +const debouncedSave = debounce((dispatch, getState) => { + if (!getState().get('me')) return; + if (getState().getIn(['preferences', 'saved'])) return; + + const data = getState().get('preferences').delete('saved').toJS(); + + api(getState).patch('/api/v1/accounts/update_credentials', { + pleroma_settings_store: { + [FE_NAME]: data, + }, + }).then(response => { + dispatch({ type: PREFERENCE_SAVE }); + dispatch(fetchMeSuccess(response.data)); + }).catch(error => { + dispatch(showAlertForError(error)); + }); +}, 5000, { trailing: true }); + +export function savePreferences() { + return (dispatch, getState) => debouncedSave(dispatch, getState); +}; diff --git a/app/gabsocial/actions/theme.js b/app/gabsocial/actions/theme.js index 427a0314e..e69de29bb 100644 --- a/app/gabsocial/actions/theme.js +++ b/app/gabsocial/actions/theme.js @@ -1,7 +0,0 @@ -export const SET_THEME = 'SET_THEME'; - -export function setTheme(theme) { - return (dispatch, getState) => { - dispatch({ type: SET_THEME, theme }); - }; -} diff --git a/app/gabsocial/containers/gabsocial.js b/app/gabsocial/containers/gabsocial.js index 77f4509fe..a6c1fb5a0 100644 --- a/app/gabsocial/containers/gabsocial.js +++ b/app/gabsocial/containers/gabsocial.js @@ -41,7 +41,7 @@ const mapStateToProps = (state) => { return { showIntroduction, me, - theme: state.getIn(['settings', 'theme']), + theme: state.getIn(['preferences', 'theme']), }; }; diff --git a/app/gabsocial/features/preferences/index.js b/app/gabsocial/features/preferences/index.js index bb2c5650a..c7c9c3539 100644 --- a/app/gabsocial/features/preferences/index.js +++ b/app/gabsocial/features/preferences/index.js @@ -4,7 +4,7 @@ import { defineMessages, injectIntl } from 'react-intl'; import ImmutablePureComponent from 'react-immutable-pure-component'; import PropTypes from 'prop-types'; import ImmutablePropTypes from 'react-immutable-proptypes'; -import { changeSetting } from 'gabsocial/actions/settings'; +import { changePreference } from 'gabsocial/actions/preferences'; import Column from '../ui/components/column'; const messages = defineMessages({ @@ -42,10 +42,6 @@ class Preferences extends ImmutablePureComponent { this.state = { isLoading: false }; } - componentWillMount() { - // this.props.dispatch(fetchPreferences()); - } - getFormData = (form) => { return Object.fromEntries( Array.from(form).map(i => [i.name, i.value]) @@ -54,7 +50,7 @@ class Preferences extends ImmutablePureComponent { onThemeChange = e => { const { dispatch } = this.props; - dispatch(changeSetting(['theme'], e.target.value)); + dispatch(changePreference(['theme'], e.target.value)); } render() { @@ -74,7 +70,7 @@ class Preferences extends ImmutablePureComponent { name='user[setting_theme]' id='user_setting_theme' onChange={this.onThemeChange} - value={settings.get('theme')} + defaultValue={settings.get('theme')} > {Object.keys(themes).map(theme => (