import React from 'react'; import { connect } from 'react-redux'; import Select from 'react-select'; import moment from 'moment'; import translate from 'translations'; import { isValidTxHash, isValidETHAddress } from 'libs/validators'; import { getRecentNetworkTransactions } from 'selectors/transactions'; import { AppState } from 'reducers'; import { Input } from 'components/ui'; import './TxHashInput.scss'; interface OwnProps { hash?: string; onSubmit(hash: string): void; } interface ReduxProps { recentTxs: AppState['transactions']['recent']; } type Props = OwnProps & ReduxProps; interface State { hash: string; } interface Option { label: string; value: string; } class TxHashInput extends React.Component { public constructor(props: Props) { super(props); this.state = { hash: props.hash || '' }; } public componentWillReceiveProps(nextProps: Props) { if (this.props.hash !== nextProps.hash && nextProps.hash) { this.setState({ hash: nextProps.hash }); } } public render() { const { recentTxs } = this.props; const { hash } = this.state; const validClass = hash ? (isValidTxHash(hash) ? 'is-valid' : 'is-invalid') : ''; let selectOptions: Option[] = []; if (recentTxs && recentTxs.length) { selectOptions = recentTxs.map(tx => ({ label: ` ${moment(tx.time).format('lll')} - ${tx.from.substr(0, 8)}... to ${tx.to.substr(0, 8)}... `, value: tx.hash })); } return (
{!!selectOptions.length && (
{isValidETHAddress(hash) && (

{translate('SELECT_RECENT_TX_BY_TXHASH')}

)} ); } private handleChange = (ev: React.FormEvent) => { this.setState({ hash: ev.currentTarget.value }); }; private handleSelectTx = (option: Option) => { if (option && option.value) { this.setState({ hash: option.value }); this.props.onSubmit(option.value); } else { this.setState({ hash: '' }); } }; private handleSubmit = (ev: React.FormEvent) => { ev.preventDefault(); if (isValidTxHash(this.state.hash)) { this.props.onSubmit(this.state.hash); } }; } export default connect((state: AppState): ReduxProps => ({ recentTxs: getRecentNetworkTransactions(state) }))(TxHashInput);