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 {
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,
device: ?TrezorDevice
} | {
type: typeof WALLET.UPDATE_SELECTED_DEVICE,
device: TrezorDevice
}
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 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 };
newState.selectedDevice = action.payload;
const selected = findSelectedDevice(newState);
if (selected) {
selected.ts = new Date().getTime();
if (device) {
const otherDevices: Array<TrezorDevice> = 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 {

@ -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

@ -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() );
}
}
}
}

Loading…
Cancel
Save