preferences --> settings

I give up trying to organize them. Just going back to using Mastodon's preferences map.
stable/1.0.x
Alex Gleason 2020-04-18 13:58:51 -05:00
rodzic fc04e6a718
commit bc5b286737
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
17 zmienionych plików z 42 dodań i 172 usunięć

Wyświetl plik

@ -65,9 +65,9 @@ export function importFetchedStatuses(statuses) {
function processStatus(status) {
const normalOldStatus = getState().getIn(['statuses', status.id]);
const readingPrefs = getState().getIn(['preferences', 'reading']);
const expandSpoilers = getState().getIn(['settings', 'expandSpoilers']);
pushUnique(normalStatuses, normalizeStatus(status, normalOldStatus, readingPrefs));
pushUnique(normalStatuses, normalizeStatus(status, normalOldStatus, expandSpoilers));
pushUnique(accounts, status.account);
if (status.reblog && status.reblog.id) {

Wyświetl plik

@ -34,9 +34,8 @@ export function normalizeAccount(account) {
return account;
}
export function normalizeStatus(status, normalOldStatus, readingPrefs) {
export function normalizeStatus(status, normalOldStatus, expandSpoilers) {
const normalStatus = { ...status };
const expandSpoilers = readingPrefs.getIn(['expand', 'spoilers']);
normalStatus.account = status.account.id;

Wyświetl plik

@ -1,62 +0,0 @@
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';
export function fetchMastoPreferences() {
return (dispatch, getState) => {
api(getState).get('/api/v1/preferences').then(response => {
dispatch(mastoFetchPrefsSuccess(response.data));
}).catch(e => {
console.error(e);
console.error('Could not fetch Mastodon preferences.');
});
};
}
export function mastoFetchPrefsSuccess(prefs) {
return {
type: MASTO_PREFS_FETCH_SUCCESS,
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);
};

Wyświetl plik

@ -1,10 +1,13 @@
import api from '../api';
import { debounce } from 'lodash';
import { showAlertForError } from './alerts';
import { fetchMeSuccess } from 'gabsocial/actions/me';
export const SETTING_CHANGE = 'SETTING_CHANGE';
export const SETTING_SAVE = 'SETTING_SAVE';
export const FE_NAME = 'soapbox_fe';
export function changeSetting(path, value) {
return dispatch => {
dispatch({
@ -19,16 +22,20 @@ export function changeSetting(path, value) {
const debouncedSave = debounce((dispatch, getState) => {
if (!getState().get('me')) return;
if (getState().getIn(['settings', 'saved'])) return;
if (getState().getIn(['settings', 'saved'])) {
return;
}
const data = getState().get('settings').delete('saved').toJS();
const data = getState().get('settings').filter((_, path) => path !== 'saved').toJS();
api().put('/api/web/settings', { data })
.then(() => dispatch({ type: SETTING_SAVE }))
.catch(error => dispatch(showAlertForError(error)));
api(getState).patch('/api/v1/accounts/update_credentials', {
pleroma_settings_store: {
[FE_NAME]: data,
},
}).then(response => {
dispatch({ type: SETTING_SAVE });
dispatch(fetchMeSuccess(response.data));
}).catch(error => {
dispatch(showAlertForError(error));
});
}, 5000, { trailing: true });
export function saveSettings() {

Wyświetl plik

@ -4,7 +4,7 @@ import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
const mapStateToProps = state => ({
animate: state.getIn(['preferences', 'auto_play_gif']),
animate: state.getIn(['settings', 'autoPlayGif']),
});
export default @connect(mapStateToProps)

Wyświetl plik

@ -4,7 +4,7 @@ import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
const mapStateToProps = state => ({
animate: state.getIn(['preferences', 'auto_play_gif']),
animate: state.getIn(['settings', 'autoPlayGif']),
});
export default @connect(mapStateToProps)

Wyświetl plik

@ -4,7 +4,7 @@ import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
const mapStateToProps = state => ({
animate: state.getIn(['preferences', 'auto_play_gif']),
animate: state.getIn(['settings', 'autoPlayGif']),
});
export default @connect(mapStateToProps)

Wyświetl plik

@ -16,7 +16,7 @@ const messages = defineMessages({
});
const mapStateToProps = state => ({
autoPlayGif: state.getIn(['preferences', 'auto_play_gif']),
autoPlayGif: state.getIn(['settings', 'autoPlayGif']),
});
@connect(mapStateToProps)

Wyświetl plik

@ -41,7 +41,7 @@ const mapStateToProps = (state) => {
return {
showIntroduction,
me,
theme: state.getIn(['preferences', 'theme']),
theme: state.getIn(['settings', 'theme']),
};
};

Wyświetl plik

@ -50,7 +50,7 @@ const messages = defineMessages({
const mapStateToProps = state => {
return {
me: state.get('me'),
autoPlayGif: state.getIn(['preferences', 'auto_play_gif']),
autoPlayGif: state.getIn(['settings', 'autoPlayGif']),
};
};

Wyświetl plik

@ -10,7 +10,7 @@ import { decode } from 'blurhash';
import { isIOS } from 'gabsocial/is_mobile';
const mapStateToProps = state => ({
autoPlayGif: state.getIn(['preferences', 'auto_play_gif']),
autoPlayGif: state.getIn(['settings', 'autoPlayGif']),
});
export default @connect(mapStateToProps)

Wyświetl plik

@ -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 { changePreference } from 'gabsocial/actions/preferences';
import { changeSetting } from 'gabsocial/actions/settings';
import Column from '../ui/components/column';
const messages = defineMessages({
@ -50,7 +50,7 @@ class Preferences extends ImmutablePureComponent {
onThemeChange = e => {
const { dispatch } = this.props;
dispatch(changePreference(['theme'], e.target.value));
dispatch(changeSetting(['theme'], e.target.value));
}
render() {

Wyświetl plik

@ -90,7 +90,7 @@ const mapStateToProps = state => {
return {
account: getAccount(state, me),
autoPlayGif: state.getIn(['preferences', 'auto_play_gif']),
autoPlayGif: state.getIn(['settings', 'autoPlayGif']),
};
};

Wyświetl plik

@ -1,29 +0,0 @@
import { mastoPrefsToMap } from '../preferences';
import { Map as ImmutableMap } from 'immutable';
describe('mastoPrefToMap', () => {
const prefs = {
'posting:default:visibility': 'public',
'posting:default:sensitive': false,
'posting:default:language': null,
'reading:expand:media': 'default',
'reading:expand:spoilers': false,
};
it('returns a map', () => {
expect(mastoPrefsToMap(prefs)).toEqual(ImmutableMap({
posting: ImmutableMap({
default: ImmutableMap({
visibility: 'public',
sensitive: false,
language: null,
}),
}),
reading: ImmutableMap({
expand: ImmutableMap({
media: 'default',
spoilers: false,
}),
}),
}));
});
});

Wyświetl plik

@ -42,7 +42,6 @@ import soapbox from './soapbox';
import instance from './instance';
import me from './me';
import auth from './auth';
import preferences from './preferences';
const reducers = {
dropdown_menu,
@ -88,7 +87,6 @@ const reducers = {
instance,
me,
auth,
preferences,
};
export default combineReducers(reducers);

Wyświetl plik

@ -1,55 +0,0 @@
import {
MASTO_PREFS_FETCH_SUCCESS,
PREFERENCE_CHANGE,
PREFERENCE_SAVE,
FE_NAME,
} from 'gabsocial/actions/preferences';
import { ME_FETCH_SUCCESS } from 'gabsocial/actions/me';
import { Map as ImmutableMap, fromJS } from 'immutable';
const initialState = ImmutableMap({
saved: true,
posting: ImmutableMap({
default: ImmutableMap({
visibility: 'public',
sensitive: false,
language: null,
}),
}),
reading: ImmutableMap({
expand: ImmutableMap({
media: 'default',
spoilers: false,
}),
}),
auto_play_gif: false,
theme: 'lime',
});
export function mastoPrefsToMap(prefs) {
let map = ImmutableMap();
for (const [key, value] of Object.entries(prefs)) {
map = map.setIn(key.split(':'), value);
}
return map;
}
export default function preferences(state = initialState, action) {
switch(action.type) {
case MASTO_PREFS_FETCH_SUCCESS:
return state.merge(mastoPrefsToMap(action.prefs));
case ME_FETCH_SUCCESS:
const me = fromJS(action.me);
const fePrefs = me.getIn(['pleroma', 'settings_store', FE_NAME]);
return state.merge(fePrefs);
case PREFERENCE_CHANGE:
return state
.setIn(action.path, action.value)
.set('saved', false);
case PREFERENCE_SAVE:
return state.set('saved', true);
default:
return state;
}
}

Wyświetl plik

@ -1,17 +1,25 @@
import { SETTING_CHANGE, SETTING_SAVE } from '../actions/settings';
import { SETTING_CHANGE, SETTING_SAVE, FE_NAME } from '../actions/settings';
import { NOTIFICATIONS_FILTER_SET } from '../actions/notifications';
import { STORE_HYDRATE } from '../actions/store';
import { EMOJI_USE } from '../actions/emojis';
import { LIST_DELETE_SUCCESS, LIST_FETCH_FAIL } from '../actions/lists';
import { ME_FETCH_SUCCESS } from 'gabsocial/actions/me';
import { Map as ImmutableMap, fromJS } from 'immutable';
import uuid from '../uuid';
const initialState = ImmutableMap({
saved: true,
onboarded: false,
skinTone: 1,
reduceMotion: false,
autoPlayGif: false,
displayMedia: true,
expandSpoilers: false,
unfollowModal: false,
boostModal: false,
deleteModal: true,
theme: 'lime',
home: ImmutableMap({
shows: ImmutableMap({
@ -101,6 +109,10 @@ export default function settings(state = initialState, action) {
switch(action.type) {
case STORE_HYDRATE:
return hydrate(state, action.state.get('settings'));
case ME_FETCH_SUCCESS:
const me = fromJS(action.me);
const fePrefs = me.getIn(['pleroma', 'settings_store', FE_NAME]);
return state.merge(fePrefs);
case NOTIFICATIONS_FILTER_SET:
case SETTING_CHANGE:
return state