separate setting actions and reducers
This commit is contained in:
parent
d23c190cad
commit
6083e70ea0
9 changed files with 96 additions and 16 deletions
4
src/background/actions/index.js
Normal file
4
src/background/actions/index.js
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
export default {
|
||||||
|
// Settings
|
||||||
|
SETTING_SET_SETTINGS: 'setting.set.settings',
|
||||||
|
};
|
13
src/background/actions/setting.js
Normal file
13
src/background/actions/setting.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import actions from '../actions';
|
||||||
|
import * as settingsStorage from 'shared/settings/storage';
|
||||||
|
|
||||||
|
const load = () => {
|
||||||
|
return settingsStorage.loadValue().then((value) => {
|
||||||
|
return {
|
||||||
|
type: actions.SETTING_SET_SETTINGS,
|
||||||
|
value,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export { load };
|
|
@ -1,6 +1,6 @@
|
||||||
import messages from 'shared/messages';
|
import messages from 'shared/messages';
|
||||||
import * as operationActions from 'background/actions/operation';
|
import * as operationActions from 'background/actions/operation';
|
||||||
import * as settingsActions from 'settings/actions/setting';
|
import * as settingActions from 'background/actions/setting';
|
||||||
import * as tabActions from 'background/actions/tab';
|
import * as tabActions from 'background/actions/tab';
|
||||||
import * as commands from 'shared/commands';
|
import * as commands from 'shared/commands';
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ export default class BackgroundComponent {
|
||||||
case messages.CONSOLE_QUERY_COMPLETIONS:
|
case messages.CONSOLE_QUERY_COMPLETIONS:
|
||||||
return commands.complete(message.text, settings.value);
|
return commands.complete(message.text, settings.value);
|
||||||
case messages.SETTINGS_RELOAD:
|
case messages.SETTINGS_RELOAD:
|
||||||
this.store.dispatch(settingsActions.load());
|
this.store.dispatch(settingActions.load());
|
||||||
return this.broadcastSettingsChanged();
|
return this.broadcastSettingsChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import * as settingsActions from 'settings/actions/setting';
|
import * as settingActions from 'background/actions/setting';
|
||||||
import messages from 'shared/messages';
|
import messages from 'shared/messages';
|
||||||
import BackgroundComponent from 'background/components/background';
|
import BackgroundComponent from 'background/components/background';
|
||||||
import reducers from 'background/reducers';
|
import reducers from 'background/reducers';
|
||||||
|
@ -16,4 +16,4 @@ const store = createStore(reducers, (e, sender) => {
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
const backgroundComponent = new BackgroundComponent(store);
|
const backgroundComponent = new BackgroundComponent(store);
|
||||||
|
|
||||||
store.dispatch(settingsActions.load());
|
store.dispatch(settingActions.load());
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import settingReducer from 'settings/reducers/setting';
|
import settingReducer from './setting';
|
||||||
|
|
||||||
// Make setting reducer instead of re-use
|
// Make setting reducer instead of re-use
|
||||||
const defaultState = {
|
const defaultState = {
|
||||||
|
|
17
src/background/reducers/setting.js
Normal file
17
src/background/reducers/setting.js
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import actions from 'settings/actions';
|
||||||
|
|
||||||
|
const defaultState = {
|
||||||
|
value: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function reducer(state = defaultState, action = {}) {
|
||||||
|
switch (action.type) {
|
||||||
|
case actions.SETTING_SET_SETTINGS:
|
||||||
|
return {
|
||||||
|
value: action.value,
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,27 +1,23 @@
|
||||||
import actions from 'settings/actions';
|
import actions from 'settings/actions';
|
||||||
import messages from 'shared/messages';
|
import messages from 'shared/messages';
|
||||||
import DefaultSettings from 'shared/settings/default';
|
import DefaultSettings from 'shared/settings/default';
|
||||||
|
import * as settingsStorage from 'shared/settings/storage';
|
||||||
import * as settingsValues from 'shared/settings/values';
|
import * as settingsValues from 'shared/settings/values';
|
||||||
|
|
||||||
const load = () => {
|
const load = () => {
|
||||||
return browser.storage.local.get('settings').then(({ settings }) => {
|
return settingsStorage.loadRaw().then((settings) => {
|
||||||
if (!settings) {
|
return set(settings);
|
||||||
return set(DefaultSettings);
|
});
|
||||||
}
|
|
||||||
return set(Object.assign({}, DefaultSettings, settings));
|
|
||||||
}, console.error);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const save = (settings) => {
|
const save = (settings) => {
|
||||||
return browser.storage.local.set({
|
return settingsStorage.save(settings).then(() => {
|
||||||
settings,
|
|
||||||
}).then(() => {
|
|
||||||
return browser.runtime.sendMessage({
|
return browser.runtime.sendMessage({
|
||||||
type: messages.SETTINGS_RELOAD
|
type: messages.SETTINGS_RELOAD
|
||||||
|
});
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
return set(settings);
|
return set(settings);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const set = (settings) => {
|
const set = (settings) => {
|
||||||
|
|
31
src/shared/settings/storage.js
Normal file
31
src/shared/settings/storage.js
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
import DefaultSettings from './default';
|
||||||
|
import * as settingsValues from './values';
|
||||||
|
|
||||||
|
const loadRaw = () => {
|
||||||
|
return browser.storage.local.get('settings').then(({ settings }) => {
|
||||||
|
if (!settings) {
|
||||||
|
return DefaultSettings;
|
||||||
|
}
|
||||||
|
return Object.assign({}, DefaultSettings, settings);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const loadValue = () => {
|
||||||
|
return loadRaw().then((settings) => {
|
||||||
|
let value = JSON.parse(DefaultSettings.json);
|
||||||
|
if (settings.source === 'json') {
|
||||||
|
value = settingsValues.valueFromJson(settings.json);
|
||||||
|
} else if (settings.source === 'form') {
|
||||||
|
value = settingsValues.valueFromForm(settings.form);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const save = (settings) => {
|
||||||
|
return browser.storage.local.set({
|
||||||
|
settings,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export { loadRaw, loadValue, save };
|
19
test/background/reducers/setting.test.js
Normal file
19
test/background/reducers/setting.test.js
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import { expect } from "chai";
|
||||||
|
import actions from 'background/actions';
|
||||||
|
import settingReducer from 'background/reducers/setting';
|
||||||
|
|
||||||
|
describe("setting reducer", () => {
|
||||||
|
it('return the initial state', () => {
|
||||||
|
let state = settingReducer(undefined, {});
|
||||||
|
expect(state).to.have.deep.property('value', {});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('return next state for SETTING_SET_SETTINGS', () => {
|
||||||
|
let action = {
|
||||||
|
type: actions.SETTING_SET_SETTINGS,
|
||||||
|
value: { key: 123 },
|
||||||
|
};
|
||||||
|
let state = settingReducer(undefined, action);
|
||||||
|
expect(state).to.have.deep.property('value', { key: 123 });
|
||||||
|
});
|
||||||
|
});
|
Reference in a new issue