import React from 'react'; import { Result } from 'mycrypto-nano-result'; import { HELP_ARTICLE } from 'config'; import translate from 'translations'; import { Token } from 'types/network'; import { HelpLink } from 'components/ui'; import { AddressField } from './AddressField'; import { DecimalField } from './DecimalField'; import { SymbolField } from './SymbolField'; import { BalanceField } from './BalanceField'; import './AddCustomTokenForm.scss'; interface Props { allTokens: Token[]; onSave(params: Token): void; toggleForm(): void; } export interface IGenerateSymbolLookup { [tokenSymbol: string]: boolean; } export interface IGenerateAddressLookup { [address: string]: boolean; } interface State { address: Result; symbol: Result; decimal: Result; } export class AddCustomTokenForm extends React.PureComponent { public state: State = { address: Result.from({ err: 'This field is empty' }), symbol: Result.from({ err: 'This field is empty' }), decimal: Result.from({ err: 'This field is empty' }) }; private tokenSymbolLookup = this.generateSymbolLookup(); private tokenAddressLookup = this.generateAddressMap(); public render() { const address = this.state.address.toVal().res; return (
{translate('ADD_CUSTOM_TKN_HELP')}
); } public onSave = (ev: React.FormEvent) => { ev.preventDefault(); if (!this.isValid()) { return; } const { address, symbol, decimal } = this.state; this.props.onSave({ address: address.unwrap(), symbol: symbol.unwrap(), decimal: parseInt(decimal.unwrap(), 10) }); }; private handleFieldChange = (fieldName: keyof State) => (res: Result) => { this.setState({ [fieldName as any]: res }); }; private isValid() { const { address, decimal, symbol } = this.state; const valid = address.ok() && decimal.ok() && symbol.ok(); return valid; } private generateSymbolLookup() { return this.tokenArrayToMap('symbol'); } private generateAddressMap() { return this.tokenArrayToMap('address'); } private tokenArrayToMap(key: Exclude) { const tokens = this.props.allTokens; return tokens.reduce<{ [k: string]: boolean }>((prev, tk) => { prev[tk[key]] = true; return prev; }, {}); } }