diff --git a/src/js/actions/TrezorConnectActions.js b/src/js/actions/TrezorConnectActions.js index f48e04a5..5ef5de3a 100644 --- a/src/js/actions/TrezorConnectActions.js +++ b/src/js/actions/TrezorConnectActions.js @@ -271,10 +271,6 @@ export const switchToFirstAvailableDevice = (): AsyncAction => { } } else { dispatch( push('/') ); - dispatch({ - type: CONNECT.SELECT_DEVICE, - payload: null - }) } } } @@ -350,11 +346,6 @@ export const deviceDisconnect = (device: Device): AsyncAction => { }); } } - - if (!selected) { - dispatch( switchToFirstAvailableDevice() ); - } - } } diff --git a/src/js/actions/WalletActions.js b/src/js/actions/WalletActions.js index c34a6d9c..154fbd07 100644 --- a/src/js/actions/WalletActions.js +++ b/src/js/actions/WalletActions.js @@ -20,6 +20,9 @@ export type WalletAction = { } | { type: typeof WALLET.SET_SELECTED_DEVICE, device: ?TrezorDevice +} | { + type: typeof WALLET.UPDATE_SELECTED_DEVICE, + device: TrezorDevice } export const init = (): ThunkAction => { diff --git a/src/js/actions/constants/wallet.js b/src/js/actions/constants/wallet.js index b183d00f..e525151d 100644 --- a/src/js/actions/constants/wallet.js +++ b/src/js/actions/constants/wallet.js @@ -6,4 +6,5 @@ export const TOGGLE_DEVICE_DROPDOWN: 'wallet__toggle_dropdown' = 'wallet__toggle export const SET_INITIAL_URL: 'wallet__set_initial_url' = 'wallet__set_initial_url'; export const ONLINE_STATUS: 'wallet__online_status' = 'wallet__online_status'; -export const SET_SELECTED_DEVICE: 'wallet__set_selected_device' = 'wallet__set_selected_device'; \ No newline at end of file +export const SET_SELECTED_DEVICE: 'wallet__set_selected_device' = 'wallet__set_selected_device'; +export const UPDATE_SELECTED_DEVICE: 'wallet__update_selected_device' = 'wallet__update_selected_device'; \ No newline at end of file diff --git a/src/js/reducers/TrezorConnectReducer.js b/src/js/reducers/TrezorConnectReducer.js index 20057dfb..f2d5316c 100644 --- a/src/js/reducers/TrezorConnectReducer.js +++ b/src/js/reducers/TrezorConnectReducer.js @@ -350,15 +350,12 @@ const duplicate = (state: State, device: TrezorDevice): State => { } -const onSelectDevice = (state: State, action: any): State => { +const onSelectedDevice = (state: State, device: ?TrezorDevice): State => { const newState: State = { ...state }; - newState.selectedDevice = action.payload; - - const selected = findSelectedDevice(newState); - if (selected) { - selected.ts = new Date().getTime(); + if (device) { + const otherDevices: Array = state.devices.filter(d => d !== device); + newState.devices = otherDevices.concat([ { ...device, ts: new Date().getTime() } ]); } - return newState; } @@ -378,12 +375,8 @@ export default function connect(state: State = initialState, action: Action): St return duplicate(state, action.device); - case CONNECT.SELECT_DEVICE : - return onSelectDevice(state, action); - // return { - // ...state, - // selectedDevice: action.payload - // } + case WALLET.SET_SELECTED_DEVICE : + return onSelectedDevice(state, action.device); case CONNECT.INITIALIZATION_ERROR : return { diff --git a/src/js/reducers/WalletReducer.js b/src/js/reducers/WalletReducer.js index cfe3d8d5..9eb5bc0d 100644 --- a/src/js/reducers/WalletReducer.js +++ b/src/js/reducers/WalletReducer.js @@ -83,6 +83,7 @@ export default function wallet(state: State = initialState, action: Action): Sta return state; case WALLET.SET_SELECTED_DEVICE : + case WALLET.UPDATE_SELECTED_DEVICE : return { ...state, selectedDevice: action.device diff --git a/src/js/services/WalletService.js b/src/js/services/WalletService.js index 23203d8c..10592328 100644 --- a/src/js/services/WalletService.js +++ b/src/js/services/WalletService.js @@ -22,7 +22,7 @@ import type { const getSelectedDevice = (state: State): ?TrezorDevice => { const locationState = state.router.location.state; - if (!locationState.device) return null; + if (!locationState.device) return undefined; const instance: ?number = locationState.deviceInstance ? parseInt(locationState.deviceInstance) : undefined; return state.connect.devices.find(d => { @@ -60,21 +60,29 @@ const WalletService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispa const state = api.getState(); - // listening devices state change + // handle devices state change if (locationChange || prevState.connect.devices !== state.connect.devices) { const device = getSelectedDevice(state); const currentDevice = state.wallet.selectedDevice; if (state.wallet.selectedDevice !== device) { - api.dispatch({ - type: WALLET.SET_SELECTED_DEVICE, - device - }) - if (!locationChange && currentDevice && device && (currentDevice.path === device.path || currentDevice.instance === device.instance)) { - // console.warn("but ONLY UPDATE!") + api.dispatch({ + type: WALLET.UPDATE_SELECTED_DEVICE, + device + }) } else { - api.dispatch( TrezorConnectActions.getSelectedDeviceState() ); + api.dispatch({ + type: WALLET.SET_SELECTED_DEVICE, + device + }); + + if (device) { + api.dispatch( TrezorConnectActions.getSelectedDeviceState() ); + } else { + api.dispatch( TrezorConnectActions.switchToFirstAvailableDevice() ); + } + } } }