import React from 'react'; import { Input } from 'components/ui'; import { translateRaw } from 'translations'; import { IGenerateAddressLookup } from './AddCustomTokenForm'; import { isValidETHAddress } from 'libs/validators'; import { Result } from 'mycrypto-nano-result'; interface OwnProps { addressLookup: IGenerateAddressLookup; onChange(address: Result): void; } enum ErrType { INVALIDADDR = 'Not a valid address', ADDRTAKEN = 'A token with this address already exists' } interface State { address: Result; userInput: string; } export class AddressField extends React.Component { public state: State = { address: Result.from({ res: '' }), userInput: '' }; public render() { const { userInput, address } = this.state; return ( ); } private handleFieldChange = (e: React.FormEvent) => { const userInput = e.currentTarget.value; const addrTaken = this.props.addressLookup[userInput]; const validAddr = isValidETHAddress(userInput); const err = addrTaken ? ErrType.ADDRTAKEN : !validAddr ? ErrType.INVALIDADDR : undefined; const address: Result = err ? Result.from({ err }) : Result.from({ res: userInput }); this.setState({ userInput, address }); this.props.onChange(address); }; }