1
0

Better settings handling with localSettings (new!)

This commit is contained in:
kibigo! 2017-06-24 19:12:34 -07:00
parent 4c37f629bc
commit da05cde721
5 changed files with 52 additions and 6 deletions

View File

@ -0,0 +1,20 @@
export const LOCAL_SETTING_CHANGE = 'LOCAL_SETTING_CHANGE';
export function changeLocalSetting(key, value) {
return dispatch => {
dispatch({
type: LOCAL_SETTING_CHANGE,
key,
value,
});
dispatch(saveLocalSettings());
};
};
export function saveLocalSettings() {
return (_, getState) => {
const localSettings = getState().get('localSettings').toJS();
localStorage.setItem('mastodon-settings', JSON.stringify(localSettings));
};
};

View File

@ -24,7 +24,11 @@ addLocaleData(localeData);
const store = configureStore(); const store = configureStore();
const initialState = JSON.parse(document.getElementById('initial-state').textContent); const initialState = JSON.parse(document.getElementById('initial-state').textContent);
if (localStorage) initialState.layout = localStorage.getItem('mastodon-layout'); try {
initialState.localSettings = JSON.parse(localStorage.getItem('mastodon-settings'));
} catch (e) {
initialState.localSettings = {};
}
store.dispatch(hydrateStore(initialState)); store.dispatch(hydrateStore(initialState));
export default class Mastodon extends React.PureComponent { export default class Mastodon extends React.PureComponent {

View File

@ -75,7 +75,7 @@ class WrappedRoute extends React.Component {
} }
const mapStateToProps = state => ({ const mapStateToProps = state => ({
layout: state.getIn(['settings', 'layout']), layout: state.getIn(['localSettings', 'layout']),
}); });
@connect(mapStateToProps) @connect(mapStateToProps)
@ -180,19 +180,19 @@ export default class UI extends React.PureComponent {
} }
render () { render () {
const { width, draggingOver, layout } = this.state; const { width, draggingOver } = this.state;
const { children } = this.props; const { children, layout } = this.props;
const columnsClass = layout => { const columnsClass = layout => {
switch (layout) { switch (layout) {
case 'single': case 'single':
return 'single-column'; return 'single-column';
case 'multiple': case 'multiple':
return 'multiple-columns'; return 'multi-columns';
default: default:
return 'auto-columns'; return 'auto-columns';
} }
} };
return ( return (
<div className={'ui ' + columnsClass(layout)} ref={this.setRef}> <div className={'ui ' + columnsClass(layout)} ref={this.setRef}>

View File

@ -14,6 +14,7 @@ import relationships from './relationships';
import search from './search'; import search from './search';
import notifications from './notifications'; import notifications from './notifications';
import settings from './settings'; import settings from './settings';
import localSettings from './local_settings';
import status_lists from './status_lists'; import status_lists from './status_lists';
import cards from './cards'; import cards from './cards';
import reports from './reports'; import reports from './reports';
@ -36,6 +37,7 @@ export default combineReducers({
search, search,
notifications, notifications,
settings, settings,
localSettings,
cards, cards,
reports, reports,
contexts, contexts,

View File

@ -0,0 +1,20 @@
import { LOCAL_SETTING_CHANGE } from '../actions/local_settings';
import { STORE_HYDRATE } from '../actions/store';
import Immutable from 'immutable';
const initialState = Immutable.Map({
layout: 'auto',
});
const hydrate = (state, localSettings) => state.mergeDeep(localSettings);
export default function localSettings(state = initialState, action) {
switch(action.type) {
case STORE_HYDRATE:
return hydrate(state, action.state.get('localSettings'));
case LOCAL_SETTING_CHANGE:
return state.setIn(action.key, action.value);
default:
return state;
}
};