From 474871d375b8045e87abc54a5b4c5491a31eed8b Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Thu, 1 Nov 2018 18:47:44 +0100 Subject: [PATCH] wallet type from passphrase modal - added new action "CONNECT.UPDATE_WALLET_TYPE" to update DeviceReducer - pass device straight from action getState() instead of param of this action - replace switch with if --- src/actions/ModalActions.js | 22 ++++++++++++---- src/actions/TrezorConnectActions.js | 3 +-- src/actions/constants/TrezorConnect.js | 3 ++- .../modals/device/WalletType/index.js | 12 +++------ src/reducers/DevicesReducer.js | 1 + src/services/WalletService.js | 26 +++++++++---------- 6 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/actions/ModalActions.js b/src/actions/ModalActions.js index c0856481..cbb48e9d 100644 --- a/src/actions/ModalActions.js +++ b/src/actions/ModalActions.js @@ -25,7 +25,19 @@ export const onPinSubmit = (value: string): Action => { }; }; -export const onPassphraseSubmit = (passphrase: string): AsyncAction => async (dispatch: Dispatch): Promise => { +export const onPassphraseSubmit = (passphrase: string): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise => { + const { modal } = getState(); + if (modal.context !== MODAL.CONTEXT_DEVICE) return; + + if (passphrase === '') { + // set standard wallet type if passphrase is blank + dispatch({ + type: CONNECT.UPDATE_WALLET_TYPE, + device: modal.device, + hidden: false, + }); + } + await TrezorConnect.uiResponse({ type: UI.RECEIVE_PASSPHRASE, payload: { @@ -106,15 +118,16 @@ export const onDeviceConnect = (device: Device): ThunkAction => (dispatch: Dispa } }; -export const onWalletTypeRequest = (device: TrezorDevice, hidden: boolean, state: ?string): ThunkAction => (dispatch: Dispatch): void => { +export const onWalletTypeRequest = (hidden: boolean): ThunkAction => (dispatch: Dispatch, getState: GetState): void => { + const { modal } = getState(); + if (modal.context !== MODAL.CONTEXT_DEVICE) return; dispatch({ type: MODAL.CLOSE, }); dispatch({ type: CONNECT.RECEIVE_WALLET_TYPE, - device, + device: modal.device, hidden, - state, }); }; @@ -129,7 +142,6 @@ export const gotoExternalWallet = (id: string, url: string): ThunkAction => (dis export default { onPinSubmit, onPassphraseSubmit, - // askForRemember, onRememberDevice, onForgetDevice, onForgetSingleDevice, diff --git a/src/actions/TrezorConnectActions.js b/src/actions/TrezorConnectActions.js index cc51ffab..d90530fb 100644 --- a/src/actions/TrezorConnectActions.js +++ b/src/actions/TrezorConnectActions.js @@ -78,10 +78,9 @@ export type TrezorConnectAction = { type: typeof CONNECT.REQUEST_WALLET_TYPE, device: TrezorDevice } | { - type: typeof CONNECT.RECEIVE_WALLET_TYPE, + type: typeof CONNECT.RECEIVE_WALLET_TYPE | typeof CONNECT.UPDATE_WALLET_TYPE, device: TrezorDevice, hidden: boolean, - state: ?string, }; declare var LOCAL: ?string; diff --git a/src/actions/constants/TrezorConnect.js b/src/actions/constants/TrezorConnect.js index a502472f..53b27c4f 100644 --- a/src/actions/constants/TrezorConnect.js +++ b/src/actions/constants/TrezorConnect.js @@ -29,4 +29,5 @@ export const START_ACQUIRING: 'connect__start_acquiring' = 'connect__start_acqui export const STOP_ACQUIRING: 'connect__stop_acquiring' = 'connect__stop_acquiring'; export const REQUEST_WALLET_TYPE: 'connect__request_wallet_type' = 'connect__request_wallet_type'; -export const RECEIVE_WALLET_TYPE: 'connect__receive_wallet_type' = 'connect__receive_wallet_type'; \ No newline at end of file +export const RECEIVE_WALLET_TYPE: 'connect__receive_wallet_type' = 'connect__receive_wallet_type'; +export const UPDATE_WALLET_TYPE: 'connect__update_wallet_type' = 'connect__update_wallet_type'; \ No newline at end of file diff --git a/src/components/modals/device/WalletType/index.js b/src/components/modals/device/WalletType/index.js index eb11b8a9..89d7d600 100644 --- a/src/components/modals/device/WalletType/index.js +++ b/src/components/modals/device/WalletType/index.js @@ -91,18 +91,14 @@ class WalletType extends PureComponent { keyboardHandler(event: KeyboardEvent): void { if (event.keyCode === 13) { event.preventDefault(); - this.changeType(false); + this.props.onWalletTypeRequest(false); } } keyboardHandler: (event: KeyboardEvent) => void; - changeType(hidden: boolean, state: ?string) { - this.props.onWalletTypeRequest(this.props.device, hidden, state); - } - render() { - const { device, onCancel } = this.props; + const { device, onCancel, onWalletTypeRequest } = this.props; return ( @@ -122,7 +118,7 @@ class WalletType extends PureComponent { Standard Wallet

Continue to access your standard wallet.

- this.changeType(false, device.state)}>Go to your standard wallet + onWalletTypeRequest(false)}>Go to your standard wallet { Hidden Wallet

You will be asked to enter your passphrase to unlock your hidden wallet.

- this.changeType(true, device.state)}>Go to your hidden wallet + onWalletTypeRequest(true)}>Go to your hidden wallet
); diff --git a/src/reducers/DevicesReducer.js b/src/reducers/DevicesReducer.js index d7ca930a..691ed375 100644 --- a/src/reducers/DevicesReducer.js +++ b/src/reducers/DevicesReducer.js @@ -317,6 +317,7 @@ export default function devices(state: State = initialState, action: Action): St return onSelectedDevice(state, action.device); case CONNECT.RECEIVE_WALLET_TYPE: + case CONNECT.UPDATE_WALLET_TYPE: return onChangeWalletType(state, action.device, action.hidden); default: diff --git a/src/services/WalletService.js b/src/services/WalletService.js index 2ad40df0..b892a0c9 100644 --- a/src/services/WalletService.js +++ b/src/services/WalletService.js @@ -105,22 +105,22 @@ const WalletService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispa api.dispatch(SendFormActionActions.observe(prevState, action)); } } else { - switch (action.type) { - case CONNECT.AUTH_DEVICE: - // selected device did changed - // try to restore discovery after device authentication - api.dispatch(DiscoveryActions.restore()); - break; - case CONNECT.RECEIVE_WALLET_TYPE: - if (action.state) { - api.dispatch(RouterActions.selectFirstAvailableDevice(true)); - } - api.dispatch(TrezorConnectActions.authorizeDevice()); - break; - default: break; + // no changes in common values + if (action.type === CONNECT.RECEIVE_WALLET_TYPE) { + if (action.device.state) { + // redirect to root view (Dashboard) if device was authenticated before + api.dispatch(RouterActions.selectFirstAvailableDevice(true)); + } + api.dispatch(TrezorConnectActions.authorizeDevice()); + } + if (action.type === CONNECT.AUTH_DEVICE) { + // selected device did changed + // try to restore discovery after device authentication + api.dispatch(DiscoveryActions.restore()); } } + // even if "selectedDevice" didn't change because it was updated on DEVICE.CHANGED before DEVICE.CONNECT action // try to restore discovery if (action.type === DEVICE.CONNECT) {