2017-07-05 22:09:58 +00:00
|
|
|
export const REDUX_STATE = 'REDUX_STATE';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { State as CustomTokenState } from 'reducers/customTokens';
|
|
|
|
import { State as SwapState } from 'reducers/swap';
|
2017-07-05 22:09:58 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export function loadState<T>(): T | undefined {
|
2017-07-05 22:09:58 +00:00
|
|
|
try {
|
|
|
|
const serializedState = localStorage.getItem(REDUX_STATE);
|
|
|
|
if (serializedState === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2017-07-13 21:02:39 +00:00
|
|
|
return JSON.parse(serializedState || '');
|
2017-07-05 22:09:58 +00:00
|
|
|
} 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-09-25 02:06:28 +00:00
|
|
|
}
|
2017-07-05 22:09:58 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export const saveState = (state: any) => {
|
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:56:46 +00:00
|
|
|
console.warn(' Warning: failed to set to local storage', state);
|
2017-07-05 22:09:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export type SwapLocalStorage = SwapState;
|
|
|
|
|
|
|
|
export function loadStatePropertyOrEmptyObject<T>(key: string): T | undefined {
|
2017-07-05 22:09:58 +00:00
|
|
|
const localStorageState = loadState();
|
|
|
|
if (localStorageState) {
|
|
|
|
if (localStorageState.hasOwnProperty(key)) {
|
2017-09-25 02:06:28 +00:00
|
|
|
return localStorageState[key] as T;
|
2017-07-05 22:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-25 02:06:28 +00:00
|
|
|
return undefined;
|
|
|
|
}
|