import React from 'react'; import { Button, Spin, Icon, Alert } from 'antd'; import classnames from 'classnames'; import Identicon from 'components/Identicon'; import ShortAddress from 'components/ShortAddress'; import './ChooseAddress.less'; interface Props { addresses: string[]; loadingMessage: string; handleDeriveAddresses(index: number, numNeeded: number): Promise; onSelectAddress(address: string): void; } interface State { index: number; isLoading: boolean; error: null | string; } const ADDRESSES_PER_PAGE = 6; export default class ChooseAddress extends React.PureComponent { state: State = { index: 0, isLoading: false, error: null, }; componentDidMount() { this.deriveAddresses(); } componentDidUpdate(prevProps: Props) { // Detect resets of the array, kick off derive if (prevProps.addresses !== this.props.addresses && !this.props.addresses.length) { this.setState({ index: 0 }, () => { this.deriveAddresses(); }); } } render() { const { addresses } = this.props; const { index, isLoading, error } = this.state; let content; if (error) { content = (
); } else { if (isLoading) { content = (
{new Array(ADDRESSES_PER_PAGE).fill(null).map((_, idx) => ( ))}
); } else { const pageAddresses = addresses.slice(index, index + ADDRESSES_PER_PAGE); content = (
{pageAddresses.map(address => ( ))}
); } content = ( <> {content}
); } return
{content}
; } private deriveAddresses = () => { this.setState( { isLoading: true, error: null, }, () => { this.props .handleDeriveAddresses(this.state.index, ADDRESSES_PER_PAGE) .then(() => this.setState({ isLoading: false })) .catch(err => this.setState({ isLoading: false, error: err.message })); }, ); }; private next = () => { this.setState({ index: this.state.index + ADDRESSES_PER_PAGE }, () => { if (!this.props.addresses[this.state.index + ADDRESSES_PER_PAGE]) { this.deriveAddresses(); } }); }; private prev = () => { this.setState({ index: Math.max(0, this.state.index - ADDRESSES_PER_PAGE) }); }; } interface AddressChoiceProps { address: string; name: string; isFake?: boolean; onClick?(address: string): void; } const AddressChoice: React.SFC = props => ( );