diff --git a/src/js/actions/ModalActions.js b/src/js/actions/ModalActions.js index c51974fa..137c1465 100644 --- a/src/js/actions/ModalActions.js +++ b/src/js/actions/ModalActions.js @@ -7,6 +7,7 @@ import * as CONNECT from './constants/TrezorConnect'; import type { ThunkAction, AsyncAction, Action, GetState, Dispatch, TrezorDevice } from '~/flowtype'; import type { State } from '../reducers/ModalReducer'; +import type { Device } from 'trezor-connect'; export type ModalAction = { type: typeof MODAL.CLOSE @@ -107,6 +108,27 @@ export const onRememberRequest = (prevState: State): ThunkAction => { } } +export const onDeviceConnect = (device: Device): ThunkAction => { + return (dispatch: Dispatch, getState: GetState): void => { + // interrupt process of remembering device (force forget) + // TODO: the same for disconnect more than 1 device at once + const { modal } = getState(); + if (modal.opened && modal.windowType === CONNECT.REMEMBER_REQUEST) { + if (device.features && modal.device && modal.device.features && modal.device.features.device_id === device.features.device_id) { + dispatch({ + type: MODAL.CLOSE, + }); + } else { + dispatch({ + type: CONNECT.FORGET, + device: modal.device + }); + } + } + + } +} + export default { onPinSubmit, onPassphraseSubmit, diff --git a/src/js/services/TrezorConnectService.js b/src/js/services/TrezorConnectService.js index d11c9c67..94e003ea 100644 --- a/src/js/services/TrezorConnectService.js +++ b/src/js/services/TrezorConnectService.js @@ -55,9 +55,9 @@ const TrezorConnectService: Middleware = (api: MiddlewareAPI) => (next: Middlewa } }); } else if (action.type === DEVICE.DISCONNECT) { - api.dispatch( TrezorConnectActions.deviceDisconnect(action.device) ); + api.dispatch( TrezorConnectActions.deviceDisconnect( action.device ) ); } else if (action.type === CONNECT.REMEMBER_REQUEST) { - api.dispatch(ModalActions.onRememberRequest(prevModalState)); + api.dispatch( ModalActions.onRememberRequest( prevModalState ) ); } else if (action.type === CONNECT.FORGET) { //api.dispatch( TrezorConnectActions.forgetDevice(action.device) ); api.dispatch( TrezorConnectActions.switchToFirstAvailableDevice() ); @@ -75,23 +75,7 @@ const TrezorConnectService: Middleware = (api: MiddlewareAPI) => (next: Middlewa } else if (action.type === DEVICE.CONNECT || action.type === DEVICE.CONNECT_UNACQUIRED) { api.dispatch( DiscoveryActions.restore() ); - - // interrupt process of remembering device (force forget) - // TODO: the same for disconnect more than 1 device at once - // TODO: move it to modal actions - const { modal } = api.getState(); - if (modal.opened && modal.windowType === CONNECT.REMEMBER_REQUEST) { - if (action.device.features && modal.device && modal.device.features && modal.device.features.device_id === action.device.features.device_id) { - api.dispatch({ - type: MODAL.CLOSE, - }); - } else { - api.dispatch({ - type: CONNECT.FORGET, - device: modal.device - }); - } - } + api.dispatch( ModalActions.onDeviceConnect( action.device ) ); } else if (action.type === CONNECT.AUTH_DEVICE) { api.dispatch( DiscoveryActions.check() );