mirror of https://github.com/trezor/trezor-wallet
thru WalletService + implementation new data model in componentspull/2/merge
parent
d74c2ceaba
commit
283af40356
File diff suppressed because it is too large
Load Diff
@ -1,171 +1,117 @@
|
||||
/* @flow */
|
||||
'use strict';
|
||||
|
||||
import React, { Component } from 'react';
|
||||
import * as React from 'react';
|
||||
import { Notification } from '~/js/components/common/Notification';
|
||||
import { findDevice } from '~/js/reducers/DevicesReducer';
|
||||
|
||||
// import * as SelectedAccountActions from '~/js/actions/SelectedAccountActions';
|
||||
import { default as SelectedAccountActions } from '~/js/actions/SelectedAccountActions';
|
||||
|
||||
import type { State, TrezorDevice, Action, ThunkAction } from '~/flowtype';
|
||||
import type { Account } from '~/js/reducers/AccountsReducer';
|
||||
import type { Discovery } from '~/js/reducers/DiscoveryReducer';
|
||||
|
||||
export type StateProps = {
|
||||
className: string;
|
||||
selectedAccount: $ElementType<State, 'selectedAccount'>,
|
||||
devices: $ElementType<State, 'devices'>,
|
||||
discovery: $ElementType<State, 'discovery'>,
|
||||
accounts: $ElementType<State, 'accounts'>,
|
||||
wallet: $ElementType<State, 'wallet'>,
|
||||
children?: React.Node
|
||||
}
|
||||
|
||||
export type DispatchProps = {
|
||||
selectedAccountActions: typeof SelectedAccountActions,
|
||||
initAccount: () => ThunkAction,
|
||||
disposeAccount: () => Action,
|
||||
|
||||
}
|
||||
|
||||
export type Props = StateProps & DispatchProps;
|
||||
|
||||
export type ComponentState = {
|
||||
device: ?TrezorDevice;
|
||||
account: ?Account;
|
||||
discovery: ?Discovery;
|
||||
deviceStatusNotification: ?React$Element<typeof Notification>;
|
||||
}
|
||||
|
||||
export default class SelectedAccount<P> extends Component<Props & P, ComponentState> {
|
||||
|
||||
state: ComponentState = {
|
||||
device: null,
|
||||
account: null,
|
||||
discovery: null,
|
||||
deviceStatusNotification: null
|
||||
};
|
||||
|
||||
componentDidMount() {
|
||||
this.props.selectedAccountActions.init();
|
||||
this.props.initAccount();
|
||||
}
|
||||
|
||||
componentWillReceiveProps(props: Props & P) {
|
||||
|
||||
this.props.selectedAccountActions.update( this.props.initAccount );
|
||||
|
||||
const accountState = props.selectedAccount;
|
||||
if (!accountState) return;
|
||||
|
||||
const device = findDevice(props.devices, accountState.deviceId, accountState.deviceState, accountState.deviceInstance);
|
||||
if (!device) return;
|
||||
const discovery = props.discovery.find(d => d.deviceState === device.state && d.network === accountState.network);
|
||||
// if (!discovery) return;
|
||||
const account = props.accounts.find(a => a.deviceState === accountState.deviceState && a.index === accountState.index && a.network === accountState.network);
|
||||
|
||||
let deviceStatusNotification: ?React$Element<typeof Notification> = null;
|
||||
if (account) {
|
||||
if (!device.connected) {
|
||||
deviceStatusNotification = <Notification className="info" title={ `Device ${ device.instanceLabel } is disconnected` } />;
|
||||
} else if (!device.available) {
|
||||
deviceStatusNotification = <Notification className="info" title={ `Device ${ device.instanceLabel } is unavailable` } message="Change passphrase settings to use this device" />;
|
||||
}
|
||||
}
|
||||
|
||||
if (discovery && !discovery.completed && !deviceStatusNotification) {
|
||||
deviceStatusNotification = <Notification className="info" title="Loading accounts..." />;
|
||||
}
|
||||
|
||||
this.setState({
|
||||
device,
|
||||
discovery,
|
||||
account,
|
||||
deviceStatusNotification
|
||||
})
|
||||
const SelectedAccount = (props: Props) => {
|
||||
const device = props.wallet.selectedDevice;
|
||||
if (!device || !device.state) {
|
||||
return (<section><Notification className="info" title="Loading device..." /></section>);
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this.props.selectedAccountActions.dispose();
|
||||
this.props.disposeAccount();
|
||||
}
|
||||
|
||||
render(): ?React$Element<string> {
|
||||
|
||||
const props = this.props;
|
||||
const accountState = props.selectedAccount;
|
||||
|
||||
if (!accountState) {
|
||||
return (<section><Notification className="info" title="Loading device..." /></section>);
|
||||
}
|
||||
const accountState = props.selectedAccount;
|
||||
|
||||
const {
|
||||
device,
|
||||
account,
|
||||
discovery
|
||||
} = this.state;
|
||||
|
||||
if (!device) {
|
||||
return (<section><Notification className="warning" title={ `Device with state ${accountState.deviceState} not found` } /></section>);
|
||||
}
|
||||
const {
|
||||
account,
|
||||
discovery
|
||||
} = accountState;
|
||||
|
||||
// account not found. checking why...
|
||||
if (!account) {
|
||||
if (!discovery || discovery.waitingForDevice) {
|
||||
|
||||
if (device.connected) {
|
||||
// case 1: device is connected but discovery not started yet (probably waiting for auth)
|
||||
if (device.available) {
|
||||
return (
|
||||
<section>
|
||||
<Notification className="info" title="Loading accounts..." />
|
||||
</section>
|
||||
);
|
||||
} else {
|
||||
// case 2: device is unavailable (created with different passphrase settings) account cannot be accessed
|
||||
return (
|
||||
<section>
|
||||
<Notification
|
||||
className="info"
|
||||
title={ `Device ${ device.instanceLabel } is unavailable` }
|
||||
message="Change passphrase settings to use this device"
|
||||
/>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
// account not found (yet). checking why...
|
||||
if (!account) {
|
||||
if (!discovery || discovery.waitingForDevice) {
|
||||
if (device.connected) {
|
||||
// case 1: device is connected but discovery not started yet (probably waiting for auth)
|
||||
if (device.available) {
|
||||
return (
|
||||
<section>
|
||||
<Notification className="info" title="Loading accounts..." />
|
||||
</section>
|
||||
);
|
||||
} else {
|
||||
// case 3: device is disconnected
|
||||
// case 2: device is unavailable (created with different passphrase settings) account cannot be accessed
|
||||
return (
|
||||
<section>
|
||||
<Notification
|
||||
className="info"
|
||||
title={ `Device ${ device.instanceLabel } is disconnected` }
|
||||
message="Connect device to load accounts"
|
||||
/>
|
||||
title={ `Device ${ device.instanceLabel } is unavailable` }
|
||||
message="Change passphrase settings to use this device"
|
||||
/>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
} else if (discovery.waitingForBackend) {
|
||||
// case 4: backend is not working
|
||||
return (
|
||||
<section>
|
||||
<Notification className="warning" title="Backend not working" />
|
||||
</section>
|
||||
);
|
||||
} else if (discovery.completed) {
|
||||
// case 5: account not found and discovery is completed
|
||||
return (
|
||||
<section>
|
||||
<Notification className="warning" title="Account does not exist" />
|
||||
</section>
|
||||
);
|
||||
} else {
|
||||
// case 6: discovery is not completed yet
|
||||
// case 3: device is disconnected
|
||||
return (
|
||||
<section>
|
||||
<Notification className="info" title="Account is loading..." />
|
||||
<Notification
|
||||
className="info"
|
||||
title={ `Device ${ device.instanceLabel } is disconnected` }
|
||||
message="Connect device to load accounts"
|
||||
/>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
} else if (discovery.waitingForBackend) {
|
||||
// case 4: backend is not working
|
||||
return (
|
||||
<section>
|
||||
<Notification className="warning" title="Backend not working" />
|
||||
</section>
|
||||
);
|
||||
} else if (discovery.completed) {
|
||||
// case 5: account not found and discovery is completed
|
||||
return (
|
||||
<section>
|
||||
<Notification className="warning" title="Account does not exist" />
|
||||
</section>
|
||||
);
|
||||
} else {
|
||||
// case 6: discovery is not completed yet
|
||||
return (
|
||||
<section>
|
||||
<Notification className="info" title="Loading accounts..." />
|
||||
</section>
|
||||
);
|
||||
}
|
||||
} else {
|
||||
|
||||
let notification: ?React$Element<typeof Notification> = null;
|
||||
if (!device.connected) {
|
||||
notification = <Notification className="info" title={ `Device ${ device.instanceLabel } is disconnected` } />;
|
||||
} else if (!device.available) {
|
||||
notification = <Notification className="info" title={ `Device ${ device.instanceLabel } is unavailable` } message="Change passphrase settings to use this device" />;
|
||||
}
|
||||
|
||||
return null;
|
||||
if (discovery && !discovery.completed && !notification) {
|
||||
notification = <Notification className="info" title="Loading accounts..." />;
|
||||
}
|
||||
|
||||
return (
|
||||
<section className={ props.className }>
|
||||
{ notification }
|
||||
{ props.children }
|
||||
</section>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default SelectedAccount;
|
Loading…
Reference in new issue