1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-25 16:38:08 +00:00

findSelectedDevice > WalletReducer.selectedDevice

This commit is contained in:
Szymon Lesisz 2018-05-22 22:07:25 +02:00
parent 751a96ccc1
commit 60c4390925
13 changed files with 35 additions and 49 deletions

View File

@ -2,7 +2,6 @@
'use strict';
import TrezorConnect from 'trezor-connect';
import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
import * as DISCOVERY from './constants/discovery';
import * as ACCOUNT from './constants/account';
import * as TOKEN from './constants/token';
@ -54,7 +53,7 @@ export type DiscoveryCompleteAction = {
export const start = (device: TrezorDevice, network: string, ignoreCompleted?: boolean): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => {
const selected = findSelectedDevice(getState().connect);
const selected = getState().wallet.selectedDevice;
if (!selected) {
// TODO: throw error
console.error("Start discovery: no selected device", device)
@ -330,7 +329,7 @@ const discoverAccount = (device: TrezorDevice, discoveryProcess: Discovery): Asy
export const restore = (): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => {
const selected = findSelectedDevice(getState().connect);
const selected = getState().wallet.selectedDevice;
if (selected && selected.connected && selected.features) {
const discoveryProcess: ?Discovery = getState().discovery.find(d => d.deviceState === selected.state && d.waitingForDevice);
@ -347,7 +346,7 @@ export const restore = (): ThunkAction => {
// try to start discovery after CONNECT.AUTH_DEVICE action
export const check = (): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => {
const selected = findSelectedDevice(getState().connect);
const selected = getState().wallet.selectedDevice;
if (!selected) return;
const urlParams = getState().router.location.state;

View File

@ -7,7 +7,6 @@ import * as NOTIFICATION from './constants/notification';
import { initialState } from '../reducers/ReceiveReducer';
import type { State } from '../reducers/ReceiveReducer';
import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
import type { TrezorDevice, ThunkAction, AsyncAction, Action, GetState, Dispatch } from '~/flowtype';
@ -55,7 +54,7 @@ export const showUnverifiedAddress = (): Action => {
export const showAddress = (address_n: Array<number>): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const selected = findSelectedDevice(getState().connect);
const selected = getState().wallet.selectedDevice;
if (!selected) return;
if (selected && (!selected.connected || !selected.available)) {

View File

@ -4,7 +4,6 @@
import * as ACCOUNT from './constants/account';
import { initialState } from '../reducers/SelectedAccountReducer';
import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
import type { AsyncAction, ThunkAction, Action, GetState, Dispatch, TrezorDevice } from '~/flowtype';
import type { State } from '../reducers/SelectedAccountReducer';
@ -23,7 +22,7 @@ export const init = (): ThunkAction => {
const { location } = getState().router;
const urlParams = location.state;
const selected: ?TrezorDevice = findSelectedDevice( getState().connect );
const selected: ?TrezorDevice = getState().wallet.selectedDevice;;
if (!selected) return;
if (!selected.state || !selected.features) return;

View File

@ -17,7 +17,7 @@ import BigNumber from 'bignumber.js';
import { initialState } from '../reducers/SendFormReducer';
import { findAccount } from '../reducers/AccountsReducer';
import { findToken } from '../reducers/TokensReducer';
import { findSelectedDevice, findDevice } from '../reducers/TrezorConnectReducer';
import { findDevice } from '../reducers/TrezorConnectReducer';
import type {
Dispatch,
@ -203,7 +203,7 @@ export const init = (): ThunkAction => {
const { location } = getState().router;
const urlParams: RouterLocationState = location.state;
const selected: ?TrezorDevice = findSelectedDevice( getState().connect );
const selected: ?TrezorDevice = getState().wallet.selectedDevice;
if (!selected) return;
const web3instance: ?Web3Instance = getState().web3.find(w3 => w3.network === urlParams.network);
@ -893,7 +893,7 @@ export const onSend = (): AsyncAction => {
v: ''
}
const selected: ?TrezorDevice = findSelectedDevice(getState().connect);
const selected: ?TrezorDevice = getState().wallet.selectedDevice;
if (!selected) return;
let signedTransaction = await TrezorConnect.ethereumSignTransaction({

View File

@ -6,7 +6,6 @@ import * as SUMMARY from './constants/summary';
import * as TOKEN from './constants/token';
import { resolveAfter } from '../utils/promiseUtils';
import { initialState } from '../reducers/SummaryReducer';
import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
import type { ThunkAction, AsyncAction, Action, GetState, Dispatch } from '~/flowtype';
import type { State } from '../reducers/SummaryReducer';

View File

@ -10,7 +10,6 @@ import * as WALLET from './constants/wallet';
import { push } from 'react-router-redux';
import * as DiscoveryActions from './DiscoveryActions';
import { resolveAfter } from '../utils/promiseUtils';
import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
import type {
@ -194,7 +193,7 @@ const sortDevices = (devices: Array<TrezorDevice>): Array<TrezorDevice> => {
export const initConnectedDevice = (device: TrezorDevice | Device): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => {
const selected = findSelectedDevice(getState().connect);
const selected = getState().wallet.selectedDevice;
// if (!selected || (selected && selected.state)) {
dispatch( onSelectDevice(device) );
// }
@ -283,7 +282,7 @@ export const switchToFirstAvailableDevice = (): AsyncAction => {
export const getSelectedDeviceState = (): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const selected = findSelectedDevice(getState().connect);
const selected = getState().wallet.selectedDevice;
if (selected
&& selected.connected
&& (selected.features && !selected.features.bootloader_mode && selected.features.initialized)
@ -335,7 +334,7 @@ export const getSelectedDeviceState = (): AsyncAction => {
export const deviceDisconnect = (device: Device): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const selected: ?TrezorDevice = findSelectedDevice(getState().connect);
const selected: ?TrezorDevice = getState().wallet.selectedDevice;
if (device && device.features) {
if (selected && selected.features && selected.features.device_id === device.features.device_id) {
@ -361,7 +360,7 @@ export const deviceDisconnect = (device: Device): AsyncAction => {
export const coinChanged = (network: ?string): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => {
const selected: ?TrezorDevice = findSelectedDevice(getState().connect);
const selected: ?TrezorDevice = getState().wallet.selectedDevice;
if (!selected) return;
dispatch( DiscoveryActions.stop(selected) );
@ -380,7 +379,7 @@ export function reload(): AsyncAction {
export function acquire(): AsyncAction {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const selected: ?TrezorDevice = findSelectedDevice(getState().connect);
const selected: ?TrezorDevice = getState().wallet.selectedDevice;
if (!selected) return;
const response = await TrezorConnect.getFeatures({
@ -416,7 +415,7 @@ export const gotoDeviceSettings = (device: TrezorDevice): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => {
if (device.features) {
const devUrl: string = `${device.features.device_id}${ device.instance ? `:${ device.instance}` : '' }`;
dispatch( push( `/device/${ devUrl}/settings` ) );
dispatch( push( `/device/${ devUrl }/settings` ) );
}
}
}
@ -438,18 +437,10 @@ export const duplicateDevice = (device: TrezorDevice): AsyncAction => {
}
}
export const selectDuplicatedDevice = (): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const selected: ?TrezorDevice = findSelectedDevice(getState().connect);
if (selected)
dispatch(onSelectDevice(selected));
}
}
export function addAccount(): ThunkAction {
return (dispatch: Dispatch, getState: GetState): void => {
const selected = findSelectedDevice(getState().connect);
const selected = getState().wallet.selectedDevice;
if (!selected) return;
dispatch( DiscoveryActions.start(selected, getState().router.location.state.network, true) ); // TODO: network nicer
}

View File

@ -3,7 +3,6 @@
import React, { Component } from 'react';
import { findAccount } from '~/js/reducers/AccountsReducer';
import { findSelectedDevice } from '~/js/reducers/TrezorConnectReducer';
import type { Props } from './index';

View File

@ -7,6 +7,7 @@ import type { Props } from './index';
type State = {
defaultName: string;
instance: number;
instanceName: ?string;
isUsed: boolean;
}
@ -27,6 +28,7 @@ export default class DuplicateDevice extends Component<Props, State> {
this.state = {
defaultName: `${device.label} (${instance.toString()})`,
instance,
instanceName: null,
isUsed: false,
}
@ -66,7 +68,7 @@ export default class DuplicateDevice extends Component<Props, State> {
submit() {
if (!this.props.modal.opened) return;
this.props.modalActions.onDuplicateDevice( { ...this.props.modal.device, instanceName: this.state.instanceName } );
this.props.modalActions.onDuplicateDevice( { ...this.props.modal.device, instanceName: this.state.instanceName, instance: this.state.instance } );
}
render() {

View File

@ -3,7 +3,6 @@
import React, { Component } from 'react';
import raf from 'raf';
import { findSelectedDevice } from '~/js/reducers/TrezorConnectReducer';
import type { Props } from './index';
type State = {
@ -33,7 +32,7 @@ export default class PinModal extends Component<Props, State> {
// check if this device is already known
// const isSavedDevice = props.devices.find(d => d.path === props.modal.device.path && d.remember);
const selected = findSelectedDevice(props.connect);
const selected = props.wallet.selectedDevice;
let deviceLabel =device.label;
let singleInput = false;
if (selected && selected.path === device.path) {

View File

@ -40,6 +40,7 @@ type StateProps = {
sendForm: $ElementType<State, 'sendForm'>,
receive: $ElementType<State, 'receive'>,
localStorage: $ElementType<State, 'localStorage'>,
wallet: $ElementType<State, 'wallet'>,
}
type DispatchProps = {
@ -131,7 +132,8 @@ const mapStateToProps: MapStateToProps<State, OwnProps, StateProps> = (state: St
selectedAccount: state.selectedAccount,
sendForm: state.sendForm,
receive: state.receive,
localStorage: state.localStorage
localStorage: state.localStorage,
wallet: state.wallet
};
}

View File

@ -323,18 +323,6 @@ const devicesFromLocalStorage = (devices: Array<TrezorDevice>): Array<TrezorDevi
const duplicate = (state: State, device: TrezorDevice): State => {
const newState: State = { ...state };
// const affectedDevices: Array<TrezorDevice> = state.devices.filter(d => d.features && device.features && d.features.device_id === device.features.device_id)
// .sort((a, b) => {
// if (!a.instance) {
// return -1;
// } else {
// return !b.instance || a.instance > b.instance ? 1 : -1;
// }
// });
// const instance: number = affectedDevices.reduce((inst, dev) => {
// return dev.instance ? dev.instance + 1 : inst + 1;
// }, 0);
const instance: number = getNewInstance(state.devices, device);
@ -347,7 +335,7 @@ const duplicate = (state: State, device: TrezorDevice): State => {
// path: device.path,
// label: device.label,
state: null,
instance,
// instance,
// instanceLabel: `${device.label} (${instance})`,
instanceLabel: `${device.label} (${ device.instanceName || instance })`,
ts: new Date().getTime(),

View File

@ -80,9 +80,9 @@ const TrezorConnectService: Middleware = (api: MiddlewareAPI) => (next: Middlewa
} else if (action.type === CONNECT.AUTH_DEVICE) {
api.dispatch( DiscoveryActions.check() );
} else if (action.type === CONNECT.DUPLICATE) {
api.dispatch( TrezorConnectActions.selectDuplicatedDevice() );
} else if (action.type === CONNECT.SELECT_DEVICE) {
api.dispatch( TrezorConnectActions.getSelectedDeviceState() );
api.dispatch( TrezorConnectActions.onSelectDevice( action.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 ) );
}

View File

@ -6,6 +6,7 @@ import * as WALLET from '../actions/constants/wallet';
import * as WalletActions from '../actions/WalletActions';
import * as LocalStorageActions from '../actions/LocalStorageActions';
import * as TrezorConnectActions from '../actions/TrezorConnectActions';
import type {
Middleware,
@ -62,11 +63,19 @@ const WalletService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispa
// listening 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!")
} else {
api.dispatch( TrezorConnectActions.getSelectedDeviceState() );
}
}
}