/* @flow */ 'use strict'; import React, { Component } from 'react'; import { Notification } from '../../common/Notification'; import { findDevice } from '../../../utils/reducerUtils'; // import * as AbstractAccountActions from '../../actions/AbstractAccountActions'; import { default as AbstractAccountActions } from '../../../actions/AbstractAccountActions'; import type { State, TrezorDevice } from '../../../flowtype'; import type { Account } from '../../../reducers/AccountsReducer'; import type { Discovery } from '../../../reducers/DiscoveryReducer'; export type StateProps = { abstractAccount: $ElementType, devices: $PropertyType<$ElementType, 'devices'>, discovery: $ElementType, accounts: $ElementType, } export type DispatchProps = { abstractAccountActions: typeof AbstractAccountActions, initAccount: typeof AbstractAccountActions.init, updateAccount: typeof AbstractAccountActions.update, disposeAccount: typeof AbstractAccountActions.dispose, } export type Props = StateProps & DispatchProps; export type AccountState = { device: ?TrezorDevice; account: ?Account; discovery: ?Discovery; deviceStatusNotification: ?React$Element; } export default class AbstractAccount

extends Component { state: AccountState = { device: null, account: null, discovery: null, deviceStatusNotification: null }; componentDidMount() { this.props.abstractAccountActions.init(); this.props.initAccount(); } componentWillReceiveProps(props: Props & P) { this.props.abstractAccountActions.update(); this.props.updateAccount(); const currentState = props.abstractAccount; const device = findDevice(props.devices, currentState.deviceState, currentState.deviceId, currentState.deviceInstance); if (!device) return; const discovery = props.discovery.find(d => d.deviceState === device.state && d.network === currentState.network); if (!discovery) return; const account = props.accounts.find(a => a.deviceState === currentState.deviceState && a.index === currentState.index && a.network === currentState.network); let deviceStatusNotification: ?React$Element = null; if (account) { if (!device.connected) { deviceStatusNotification = ; } else if (!device.available) { deviceStatusNotification = ; } else if (!discovery.completed) { deviceStatusNotification = ; } } this.setState({ device, discovery, account, deviceStatusNotification }) } componentWillUnmount() { this.props.abstractAccountActions.dispose(); this.props.disposeAccount(); } render(): ?React$Element { const props = this.props; const currentState = props.abstractAccount; if (!currentState.deviceState) { return (

); } const { device, account, discovery } = this.state; // const device = findDevice(props.devices, accountState.deviceState, accountState.deviceId, accountState.deviceInstance); if (!device) { return (
Device with state {currentState.deviceState} not found
); } if (!account) { if (!discovery || discovery.waitingForDevice) { if (device.connected) { if (device.available) { return (
); } else { return (
); } } else { return (
); } } else if (discovery.completed) { return (
); } else { return (
); } } return null; } }