From da467f0304673adb045dbcad710cd3ae4c0f9408 Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Wed, 16 May 2018 15:38:12 +0200 Subject: [PATCH] FIX: unacquired to acuquired --- src/js/actions/TrezorConnectActions.js | 5 +- src/js/reducers/AccountsReducer.js | 3 +- src/js/reducers/TrezorConnectReducer.js | 61 ++++++++----------------- src/js/services/TrezorConnectService.js | 14 +++--- 4 files changed, 31 insertions(+), 52 deletions(-) diff --git a/src/js/actions/TrezorConnectActions.js b/src/js/actions/TrezorConnectActions.js index b8dbb414..49a5be71 100644 --- a/src/js/actions/TrezorConnectActions.js +++ b/src/js/actions/TrezorConnectActions.js @@ -202,11 +202,10 @@ const sortDevices = (devices: Array): Array => { export const initConnectedDevice = (device: TrezorDevice | Device): ThunkAction => { return (dispatch: Dispatch, getState: GetState): void => { - const selected = findSelectedDevice(getState().connect); - if (!selected || (selected && selected.state)) { + // if (!selected || (selected && selected.state)) { dispatch( onSelectDevice(device) ); - } + // } // if (device.unacquired && selected && selected.path !== device.path && !selected.connected) { // dispatch( onSelectDevice(device) ); // } else if (!selected) { diff --git a/src/js/reducers/AccountsReducer.js b/src/js/reducers/AccountsReducer.js index 7ac1721f..b9c3e4c2 100644 --- a/src/js/reducers/AccountsReducer.js +++ b/src/js/reducers/AccountsReducer.js @@ -43,7 +43,8 @@ const createAccount = (state: State, action: AccountCreateAction): State => { // TODO check with device_id // check if account was created before - const exist: ?Account = state.find(account => account.address === action.address && account.network === action.network && action.device.features && account.deviceID === action.device.features.device_id); + // const exist: ?Account = state.find(account => account.address === action.address && account.network === action.network && action.device.features && account.deviceID === action.device.features.device_id); + const exist: ?Account = state.find(account => account.address === action.address && account.network === action.network && account.deviceState === action.device.state); if (exist) { return state; } diff --git a/src/js/reducers/TrezorConnectReducer.js b/src/js/reducers/TrezorConnectReducer.js index 9bd85f20..0e737b44 100644 --- a/src/js/reducers/TrezorConnectReducer.js +++ b/src/js/reducers/TrezorConnectReducer.js @@ -148,18 +148,18 @@ const addDevice = (state: State, device: Device): State => { let affectedDevices: Array = []; let otherDevices: Array = []; - if (device.unacquired) { - // check if connected device is unacquired, but it was already merged with saved device(s) after DEVICE.CHANGE action + if (!device.features) { + // check if connected device is unacquired, and it's already exists affectedDevices = newState.devices.filter(d => d.path === device.path); - const diff: Array = newState.devices.filter(d => affectedDevices.indexOf(d) === -1); - // if so, ignore this action if (affectedDevices.length > 0) { return state; } + otherDevices = newState.devices.filter(d => affectedDevices.indexOf(d) === -1); } else { affectedDevices = newState.devices.filter(d => d.features && d.features.device_id === device.features.device_id); - otherDevices = newState.devices.filter(d => d.features && d.features.device_id !== device.features.device_id); + const unacquiredDevices = newState.devices.filter(d => d.path.length > 0 && d.path === device.path); + otherDevices = newState.devices.filter(d => affectedDevices.indexOf(d) < 0 && unacquiredDevices.indexOf(d) < 0); } if (affectedDevices.length > 0 ) { @@ -222,7 +222,8 @@ const addDevice = (state: State, device: Device): State => { instanceName: null, ts: new Date().getTime(), } - newState.devices.push(newDevice); + // newState.devices.push(newDevice); + newState.devices = otherDevices.concat([newDevice]); } return newState; @@ -248,49 +249,25 @@ const setDeviceState = (state: State, action: any): State => { const changeDevice = (state: State, device: Object): State => { - const newState: State = { ...state }; + // change only acquired devices + if (!device.features) return state; - let affectedDevices: Array = []; - let otherDevices: Array = []; - if (device.features) { - affectedDevices = state.devices.filter(d => - (d.features && d.features.device_id === device.features.device_id && d.features.passphrase_protection === device.features.passphrase_protection) || - (d.path.length > 0 && d.path === device.path) - ); - otherDevices = state.devices.filter(d => affectedDevices.indexOf(d) === -1); - } else { - affectedDevices = state.devices.filter(d => !d.features && d.path === device.path); - otherDevices = state.devices.filter(d => affectedDevices.indexOf(d) === -1); - // otherDevices = state.devices.filter(d => d.path !== device.path); - } + const affectedDevices: Array = state.devices.filter(d => + (d.features && d.features.device_id === device.features.device_id && d.features.passphrase_protection === device.features.passphrase_protection) || + (d.features && d.path.length > 0 && d.path === device.path) + ); + const otherDevices: Array = state.devices.filter(d => affectedDevices.indexOf(d) === -1); if (affectedDevices.length > 0) { - - const isAffectedUnacquired: number = affectedDevices.findIndex(d => d.unacquired); - // if (isAffectedUnacquired >= 0 && affectedDevices.length > 1){ - if (isAffectedUnacquired >= 0){ - // TODO: should unacquired device be removed? or merged? - //affectedDevices.splice(isAffectedUnacquired, 1); - } else { - // replace existing values - const changedDevices: Array = affectedDevices.map(d => mergeDevices(d, device)); - newState.devices = otherDevices.concat(changedDevices); - } - - // else if (isAffectedUnacquired >= 0 && !device.unacquired && affectedDevices.length > 1) { - // affectedDevices.splice(isAffectedUnacquired, 1); - // } - // acquiring selected device. remove unnecessary (not acquired) device from list - // after this action selectedDevice needs to be updated (in TrezorConnectService) - if (state.selectedDevice && device.path === state.selectedDevice.id && affectedDevices.length > 1) { - // affectedDevices = affectedDevices.filter(d => d.path !== state.selectedDevice.id && d.features); - } + const newState: State = { ...state }; + const changedDevices = affectedDevices.map(d => mergeDevices(d, device)); + newState.devices = otherDevices.concat(changedDevices); + return newState; } - return newState; + return state; } - const disconnectDevice = (state: State, device: Device): State => { const newState: State = { ...state }; diff --git a/src/js/services/TrezorConnectService.js b/src/js/services/TrezorConnectService.js index 3fa08649..a87a0a0f 100644 --- a/src/js/services/TrezorConnectService.js +++ b/src/js/services/TrezorConnectService.js @@ -78,11 +78,12 @@ const TrezorConnectService: Middleware = (api: MiddlewareAPI) => (next: Middlewa } else if (action.type === DEVICE.CHANGED) { // selected device was previously unacquired, but now it's acquired // we need to change route - if (prevState.selectedDevice) { - if (!action.device.unacquired && action.device.path === prevState.selectedDevice.id) { - api.dispatch( TrezorConnectActions.onSelectDevice(action.device) ); - } - } + // if (prevState.selectedDevice) { + // if (action.device.features && action.device.path === prevState.selectedDevice.id) { + // //console.log("REDIR HERE!", action.device) + // //api.dispatch( TrezorConnectActions.onSelectDevice(action.device) ); + // } + // } } else if (action.type === DEVICE.CONNECT || action.type === DEVICE.CONNECT_UNACQUIRED) { api.dispatch( DiscoveryActions.restore() ); @@ -108,7 +109,8 @@ const TrezorConnectService: Middleware = (api: MiddlewareAPI) => (next: Middlewa api.dispatch( DiscoveryActions.check() ); } else if (action.type === CONNECT.DUPLICATE) { api.dispatch( TrezorConnectActions.selectDuplicatedDevice() ); - } else if (action.type === CONNECT.ACQUIRED || action.type === CONNECT.SELECT_DEVICE) { + // } else if (action.type === CONNECT.ACQUIRED || action.type === CONNECT.SELECT_DEVICE) { + } else if (action.type === CONNECT.SELECT_DEVICE) { api.dispatch( TrezorConnectActions.getSelectedDeviceState() ); } else if (action.type === CONNECT.COIN_CHANGED) { api.dispatch( TrezorConnectActions.coinChanged( action.payload.network ) );