2017-07-05 22:16:32 +00:00
|
|
|
// @flow
|
2017-07-05 22:09:58 +00:00
|
|
|
export const REDUX_STATE = 'REDUX_STATE';
|
|
|
|
|
|
|
|
export const loadState = () => {
|
|
|
|
try {
|
|
|
|
const serializedState = localStorage.getItem(REDUX_STATE);
|
|
|
|
if (serializedState === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return JSON.parse(serializedState);
|
|
|
|
} catch (err) {
|
2017-07-05 22:35:53 +00:00
|
|
|
console.warn(' Warning: corrupted local storage');
|
2017-07-05 22:09:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-05 22:16:32 +00:00
|
|
|
export const saveState = (state: Object) => {
|
2017-07-05 22:09:58 +00:00
|
|
|
try {
|
|
|
|
const serializedState = JSON.stringify(state);
|
|
|
|
localStorage.setItem(REDUX_STATE, serializedState);
|
|
|
|
} catch (err) {
|
2017-07-05 22:35:53 +00:00
|
|
|
console.warn(' Warning: corrupted local storage');
|
2017-07-05 22:09:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const loadStatePropertyOrEmptyObject = (key: string): Object => {
|
|
|
|
const localStorageState = loadState();
|
|
|
|
if (localStorageState) {
|
|
|
|
if (localStorageState.hasOwnProperty(key)) {
|
|
|
|
return localStorageState[key];
|
|
|
|
}
|
|
|
|
}
|
2017-07-05 22:16:32 +00:00
|
|
|
return {};
|
2017-07-05 22:09:58 +00:00
|
|
|
};
|