parent
d23c190cad
commit
6083e70ea0
9 changed files with 96 additions and 16 deletions
@ -0,0 +1,4 @@ |
||||
export default { |
||||
// Settings
|
||||
SETTING_SET_SETTINGS: 'setting.set.settings', |
||||
}; |
@ -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 }; |
@ -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; |
||||
} |
||||
} |
||||
|
@ -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 }; |
@ -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 new issue