1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-24 09:18:09 +00:00

setDeviceState method properly propagate new state

This commit is contained in:
Szymon Lesisz 2018-05-22 19:42:55 +02:00
parent e5bdadfcb2
commit 1e66618186

View File

@ -3,6 +3,7 @@
import { TRANSPORT, DEVICE } from 'trezor-connect'; import { TRANSPORT, DEVICE } from 'trezor-connect';
import * as CONNECT from '../actions/constants/TrezorConnect'; import * as CONNECT from '../actions/constants/TrezorConnect';
import * as WALLET from '../actions/constants/wallet';
import type { Action, TrezorDevice } from '~/flowtype'; import type { Action, TrezorDevice } from '~/flowtype';
import type { Device } from 'trezor-connect'; import type { Device } from 'trezor-connect';
@ -230,25 +231,19 @@ const addDevice = (state: State, device: Device): State => {
return newState; return newState;
} }
const setDeviceState = (state: State, action: any): State => { const setDeviceState = (state: State, device: TrezorDevice, deviceState: string): State => {
const newState: State = { ...state }; const newState: State = { ...state };
const affectedDevice: ?TrezorDevice = state.devices.find(d => d.path === device.path && d.instance === device.instance);
//const affectedDevice: ?TrezorDevice = state.devices.find(d => d.path === action.device.path && d.instance === action.device.instance); // device could already have own state from firmware, do not override it
const index: number = state.devices.findIndex(d => d.path === action.device.path && d.instance === action.device.instance); if (affectedDevice && !affectedDevice.state) {
if (index > -1) { const otherDevices: Array<TrezorDevice> = state.devices.filter(d => d !== affectedDevice);
// device could already have own state from firmware, do not override it affectedDevice.state = deviceState;
if (!newState.devices[index].state) { newState.devices = otherDevices.concat([affectedDevice]);
const changedDevice: TrezorDevice = {
...newState.devices[index],
state: action.state
};
newState.devices[index] = changedDevice;
}
} }
return newState; return newState;
} }
const changeDevice = (state: State, device: Object): State => { const changeDevice = (state: State, device: Device): State => {
// change only acquired devices // change only acquired devices
if (!device.features) return state; if (!device.features) return state;
@ -312,7 +307,7 @@ const forgetDevice = (state: State, action: any): State => {
return newState; return newState;
} }
const devicesFromLocalStorage = (devices: Array<any>): Array<TrezorDevice> => { const devicesFromLocalStorage = (devices: Array<TrezorDevice>): Array<TrezorDevice> => {
return devices.map(d => { return devices.map(d => {
return { return {
...d, ...d,
@ -430,7 +425,7 @@ export default function connect(state: State = initialState, action: Action): St
} }
case CONNECT.AUTH_DEVICE : case CONNECT.AUTH_DEVICE :
return setDeviceState(state, action); return setDeviceState(state, action.device, action.state);
case CONNECT.REMEMBER : case CONNECT.REMEMBER :
return changeDevice(state, { ...action.device, path: '', remember: true } ); return changeDevice(state, { ...action.device, path: '', remember: true } );