SET_SELECTED_DEVICE + UPDATE_SELECTED_DEVICE

pull/2/merge
Szymon Lesisz 6 years ago
parent 60c4390925
commit 6de33d5691

@ -271,10 +271,6 @@ export const switchToFirstAvailableDevice = (): AsyncAction => {
} }
} else { } else {
dispatch( push('/') ); dispatch( push('/') );
dispatch({
type: CONNECT.SELECT_DEVICE,
payload: null
})
} }
} }
} }
@ -350,11 +346,6 @@ export const deviceDisconnect = (device: Device): AsyncAction => {
}); });
} }
} }
if (!selected) {
dispatch( switchToFirstAvailableDevice() );
}
} }
} }

@ -20,6 +20,9 @@ export type WalletAction = {
} | { } | {
type: typeof WALLET.SET_SELECTED_DEVICE, type: typeof WALLET.SET_SELECTED_DEVICE,
device: ?TrezorDevice device: ?TrezorDevice
} | {
type: typeof WALLET.UPDATE_SELECTED_DEVICE,
device: TrezorDevice
} }
export const init = (): ThunkAction => { export const init = (): ThunkAction => {

@ -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 SET_INITIAL_URL: 'wallet__set_initial_url' = 'wallet__set_initial_url';
export const ONLINE_STATUS: 'wallet__online_status' = 'wallet__online_status'; export const ONLINE_STATUS: 'wallet__online_status' = 'wallet__online_status';
export const SET_SELECTED_DEVICE: 'wallet__set_selected_device' = 'wallet__set_selected_device'; 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';

@ -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 }; const newState: State = { ...state };
newState.selectedDevice = action.payload; if (device) {
const otherDevices: Array<TrezorDevice> = state.devices.filter(d => d !== device);
const selected = findSelectedDevice(newState); newState.devices = otherDevices.concat([ { ...device, ts: new Date().getTime() } ]);
if (selected) {
selected.ts = new Date().getTime();
} }
return newState; return newState;
} }
@ -378,12 +375,8 @@ export default function connect(state: State = initialState, action: Action): St
return duplicate(state, action.device); return duplicate(state, action.device);
case CONNECT.SELECT_DEVICE : case WALLET.SET_SELECTED_DEVICE :
return onSelectDevice(state, action); return onSelectedDevice(state, action.device);
// return {
// ...state,
// selectedDevice: action.payload
// }
case CONNECT.INITIALIZATION_ERROR : case CONNECT.INITIALIZATION_ERROR :
return { return {

@ -83,6 +83,7 @@ export default function wallet(state: State = initialState, action: Action): Sta
return state; return state;
case WALLET.SET_SELECTED_DEVICE : case WALLET.SET_SELECTED_DEVICE :
case WALLET.UPDATE_SELECTED_DEVICE :
return { return {
...state, ...state,
selectedDevice: action.device selectedDevice: action.device

@ -22,7 +22,7 @@ import type {
const getSelectedDevice = (state: State): ?TrezorDevice => { const getSelectedDevice = (state: State): ?TrezorDevice => {
const locationState = state.router.location.state; 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; const instance: ?number = locationState.deviceInstance ? parseInt(locationState.deviceInstance) : undefined;
return state.connect.devices.find(d => { return state.connect.devices.find(d => {
@ -60,21 +60,29 @@ const WalletService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispa
const state = api.getState(); const state = api.getState();
// listening devices state change // handle devices state change
if (locationChange || prevState.connect.devices !== state.connect.devices) { if (locationChange || prevState.connect.devices !== state.connect.devices) {
const device = getSelectedDevice(state); const device = getSelectedDevice(state);
const currentDevice = state.wallet.selectedDevice; const currentDevice = state.wallet.selectedDevice;
if (state.wallet.selectedDevice !== device) { 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)) { 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 { } else {
api.dispatch( TrezorConnectActions.getSelectedDeviceState() ); api.dispatch({
type: WALLET.SET_SELECTED_DEVICE,
device
});
if (device) {
api.dispatch( TrezorConnectActions.getSelectedDeviceState() );
} else {
api.dispatch( TrezorConnectActions.switchToFirstAvailableDevice() );
}
} }
} }
} }

Loading…
Cancel
Save