diff --git a/src/js/actions/LocalStorageActions.js b/src/js/actions/LocalStorageActions.js index fc37e817..c36d0dcd 100644 --- a/src/js/actions/LocalStorageActions.js +++ b/src/js/actions/LocalStorageActions.js @@ -81,10 +81,17 @@ export const loadData = (): ThunkAction => { export function loadTokensFromJSON(): AsyncAction { return async (dispatch: Dispatch, getState: GetState): Promise => { + + if (typeof window.localStorage === 'undefined') return; + try { const config: Config = await httpRequest('data/appConfig.json', 'json'); const ERC20Abi = await httpRequest('data/ERC20Abi.json', 'json'); + window.addEventListener('storage', event => { + dispatch( update(event) ); + }) + // load tokens const tokens = await config.coins.reduce(async (promise: Promise, coin: Coin): Promise => { const collection: TokensCollection = await promise; @@ -150,6 +157,17 @@ export function loadTokensFromJSON(): AsyncAction { } } +export function update(event: StorageEvent): AsyncAction { + return async (dispatch: Dispatch, getState: GetState): Promise => { + if (event.key === 'tokens' && event.newValue) { + dispatch({ + type: TOKEN.FROM_STORAGE, + payload: JSON.parse(event.newValue) + }); + } + } +} + export const save = (key: string, value: string): ThunkAction => { return (dispatch: Dispatch, getState: GetState): void => { @@ -165,12 +183,13 @@ export const save = (key: string, value: string): ThunkAction => { } export const get = (key: string): ?string => { - if (typeof window.localStorage !== 'undefined') { - try { - return window.localStorage.getItem(key); - } catch (error) { - // available = false; - return null; - } + + + try { + return window.localStorage.getItem(key); + } catch (error) { + // available = false; + return null; } + } \ No newline at end of file