diff --git a/src/actions/SelectedAccountActions.js b/src/actions/SelectedAccountActions.js index 0bacf1b3..a89362d0 100644 --- a/src/actions/SelectedAccountActions.js +++ b/src/actions/SelectedAccountActions.js @@ -48,10 +48,6 @@ export const updateSelectedValues = (prevState: State, action: Action): AsyncAct || prevState.discovery !== state.discovery || prevState.tokens !== state.tokens || prevState.pending !== state.pending) { - if (locationChange) { - // dispose current account view - dispatch(dispose()); - } const account = stateUtils.getSelectedAccount(state); const network = stateUtils.getSelectedNetwork(state); diff --git a/src/services/WalletService.js b/src/services/WalletService.js index 97448262..86dd4089 100644 --- a/src/services/WalletService.js +++ b/src/services/WalletService.js @@ -4,9 +4,11 @@ import { DEVICE } from 'trezor-connect'; import { LOCATION_CHANGE } from 'react-router-redux'; import * as WALLET from 'actions/constants/wallet'; +import * as CONNECT from 'actions/constants/TrezorConnect'; import * as WalletActions from 'actions/WalletActions'; import * as RouterActions from 'actions/RouterActions'; +import * as NotificationActions from 'actions/NotificationActions'; import * as LocalStorageActions from 'actions/LocalStorageActions'; import * as TrezorConnectActions from 'actions/TrezorConnectActions'; import * as SelectedAccountActions from 'actions/SelectedAccountActions'; @@ -30,41 +32,69 @@ const WalletService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispa const { location } = api.getState().router; if (!location) { api.dispatch(WalletActions.init()); + return next(action); } } - if (action.type === WALLET.SET_INITIAL_URL) { - // load data from config.json and local storage - api.dispatch(LocalStorageActions.loadData()); - } - // pass action next(action); - if (action.type === DEVICE.CONNECT) { - api.dispatch(WalletActions.clearUnavailableDevicesData(prevState, action.device)); + switch (action.type) { + case WALLET.SET_INITIAL_URL: + api.dispatch(LocalStorageActions.loadData()); + break; + case WALLET.SET_SELECTED_DEVICE: { + if (action.device) { + // try to authorize device + api.dispatch(TrezorConnectActions.getSelectedDeviceState()); + } else { + // try select different device + api.dispatch(RouterActions.selectFirstAvailableDevice()); + } + } + break; + case DEVICE.CONNECT: + api.dispatch(WalletActions.clearUnavailableDevicesData(prevState, action.device)); + break; + default: { + break; + } } // update common values ONLY if application is ready - if (api.getState().wallet.ready) { - // update common values in WallerReducer - api.dispatch(WalletActions.updateSelectedValues(prevState, action)); + if (!api.getState().wallet.ready) return action; - // update common values in SelectedAccountReducer - api.dispatch(SelectedAccountActions.updateSelectedValues(prevState, action)); - } - - // handle selected device change - if (action.type === WALLET.SET_SELECTED_DEVICE) { - if (action.device) { - // try to authorize device - api.dispatch(TrezorConnectActions.getSelectedDeviceState()); - } else { - // try select different device - api.dispatch(RouterActions.selectFirstAvailableDevice()); + // double verification needed + // Corner case: LOCATION_CHANGE was called but pathname didn't changed (redirection in RouterService) + const prevLocation = prevState.router.location; + const currentLocation = api.getState().router.location; + if (locationChange && prevLocation.pathname !== currentLocation.pathname) { + // watch for coin change + if (prevLocation.state.network !== currentLocation.state.network) { + api.dispatch({ + type: CONNECT.COIN_CHANGED, + payload: { + network: currentLocation.state.network, + }, + }); } + + // watch for account change + if (prevLocation.state.account !== currentLocation.state.account) { + api.dispatch(SelectedAccountActions.dispose()); + } + + // clear notifications + api.dispatch(NotificationActions.clear(prevLocation.state, currentLocation.state)); } + + // update common values in WallerReducer + api.dispatch(WalletActions.updateSelectedValues(prevState, action)); + + // update common values in SelectedAccountReducer + api.dispatch(SelectedAccountActions.updateSelectedValues(prevState, action)); + return action; };