mirror of
https://github.com/Unleash/unleash.git
synced 2025-04-10 01:16:39 +02:00
34 lines
904 B
JavaScript
34 lines
904 B
JavaScript
import { fromJS, Map as $Map } from 'immutable';
|
|
import { UPDATE_SETTING } from './actions';
|
|
|
|
// TODO: provde a mock if localstorage does not exists?
|
|
const localStorage = window.localStorage || {};
|
|
const SETTINGS = 'settings';
|
|
|
|
function getInitState() {
|
|
try {
|
|
const state = JSON.parse(localStorage.getItem(SETTINGS));
|
|
return state ? fromJS(state) : new $Map();
|
|
} catch (e) {
|
|
return new $Map();
|
|
}
|
|
}
|
|
|
|
function updateSetting(state, action) {
|
|
const newState = state.updateIn([action.group, action.field], () => action.value);
|
|
|
|
localStorage.setItem(SETTINGS, JSON.stringify(newState.toJSON()));
|
|
return newState;
|
|
}
|
|
|
|
const settingStore = (state = getInitState(), action) => {
|
|
switch (action.type) {
|
|
case UPDATE_SETTING:
|
|
return updateSetting(state, action);
|
|
default:
|
|
return state;
|
|
}
|
|
};
|
|
|
|
export default settingStore;
|