// @flow import './DeterministicWalletsModal.scss'; import React from 'react'; import { connect } from 'react-redux'; import Modal from 'components/ui/Modal'; import { getDeterministicWallets, setDesiredToken } from 'actions/deterministicWallets'; import { toUnit } from 'libs/units'; import { getNetworkConfig } from 'selectors/config'; import { getTokens } from 'selectors/wallet'; import { isValidPath } from 'libs/validators'; import type { DeterministicWalletData, GetDeterministicWalletsArgs, GetDeterministicWalletsAction, SetDesiredTokenAction } from 'actions/deterministicWallets'; import type { NetworkConfig, Token } from 'config/data'; const WALLETS_PER_PAGE = 5; type Props = { // Redux state wallets: DeterministicWalletData[], desiredToken: string, network: NetworkConfig, tokens: Token[], // Redux actions getDeterministicWallets: GetDeterministicWalletsArgs => GetDeterministicWalletsAction, setDesiredToken: (tkn: ?string) => SetDesiredTokenAction, // Passed props isOpen?: boolean, walletType: ?string, dPath: string, dPaths: { label: string, value: string }[], publicKey: string, chainCode: string, onCancel: () => void, onConfirmAddress: string => void, onPathChange: string => void }; type State = { selectedAddress: string, isCustomPath: boolean, customPath: string, page: number }; class DeterministicWalletsModal extends React.Component { props: Props; state: State = { selectedAddress: '', isCustomPath: false, customPath: '', page: 0 }; componentDidMount() { this._getAddresses(); } componentWillReceiveProps(nextProps) { const { publicKey, chainCode } = this.props; if ( nextProps.publicKey !== publicKey || nextProps.chainCode !== chainCode ) { this._getAddresses(nextProps); } } _getAddresses(props: Props = this.props) { const { dPath, publicKey, chainCode } = props; if (dPath && publicKey && chainCode && isValidPath(dPath)) { this.props.getDeterministicWallets({ dPath, publicKey, chainCode, limit: WALLETS_PER_PAGE, offset: WALLETS_PER_PAGE * this.state.page }); } } _handleChangePath = (ev: SyntheticInputEvent) => { const { value } = ev.target; if (value === 'custom') { this.setState({ isCustomPath: true }); } else { this.setState({ isCustomPath: false }); if (this.props.dPath !== value) { this.props.onPathChange(value); } } }; _handleChangeCustomPath = (ev: SyntheticInputEvent) => { this.setState({ customPath: ev.target.value }); }; _handleSubmitCustomPath = (ev: SyntheticInputEvent) => { ev.preventDefault(); if (!isValidPath(this.state.customPath)) return; this.props.onPathChange(this.state.customPath); }; _handleChangeToken = (ev: SyntheticInputEvent) => { this.props.setDesiredToken(ev.target.value || null); }; _handleConfirmAddress = () => { if (this.state.selectedAddress) { this.props.onConfirmAddress(this.state.selectedAddress); } }; _selectAddress(selectedAddress) { this.setState({ selectedAddress }); } _nextPage = () => { this.setState({ page: this.state.page + 1 }, this._getAddresses); }; _prevPage = () => { this.setState( { page: Math.max(this.state.page - 1, 0) }, this._getAddresses ); }; _renderWalletRow(wallet) { const { desiredToken, network } = this.props; const { selectedAddress } = this.state; // Get renderable values, but keep 'em short const value = wallet.value ? toUnit(wallet.value, 'wei', 'ether').toPrecision(4) : ''; const tokenValue = wallet.tokenValues[desiredToken] ? wallet.tokenValues[desiredToken].toPrecision(4) : ''; return (
# | Address | {network.unit} | More |