set address from ens on blur

This commit is contained in:
Mikhail Mikheev 2019-07-26 17:47:31 +04:00
parent 2beb8afc29
commit e5061a5731
2 changed files with 38 additions and 11 deletions

View File

@ -1,7 +1,6 @@
// @flow // @flow
import * as React from 'react' import * as React from 'react'
import { Field } from 'react-final-form' import { Field } from 'react-final-form'
import { OnChange } from 'react-final-form-listeners'
import TextField from '~/components/forms/TextField' import TextField from '~/components/forms/TextField'
import { composeValidators, required, mustBeEthereumAddress } from '~/components/forms/validator' import { composeValidators, required, mustBeEthereumAddress } from '~/components/forms/validator'
import { getAddressFromENS } from '~/logic/wallets/getWeb3' import { getAddressFromENS } from '~/logic/wallets/getWeb3'
@ -11,37 +10,61 @@ type Props = {
name?: string, name?: string,
text?: string, text?: string,
placeholder?: string, placeholder?: string,
fieldMutator: Function,
} }
const isValidEnsName = name => /^([\w-]+\.)+(eth|test)$/.test(name) const isValidEnsName = name => /^([\w-]+\.)+(eth|test)$/.test(name)
const { useState, useEffect } = React
// an idea for second field was taken from here
// https://github.com/final-form/react-final-form-listeners/blob/master/src/OnBlur.js
const AddressInput = ({ const AddressInput = ({
className = '', className = '',
name = 'recipientAddress', name = 'recipientAddress',
text = 'Recipient*', text = 'Recipient*',
placeholder = 'Recipient*', placeholder = 'Recipient*',
fieldMutator,
}: Props): React.Element<*> => ( }: Props): React.Element<*> => (
<> <>
<Field <Field
name={name} name={name}
component={TextField} component={TextField}
type="text" type="text"
// validate={composeValidators(required, mustBeEthereumAddress)} validate={composeValidators(required, mustBeEthereumAddress)}
placeholder={placeholder} placeholder={placeholder}
text={text} text={text}
className={className} className={className}
/> />
<OnChange name={name}> <Field
{async (value) => { name={name}
if (isValidEnsName(value)) { subscription={{ active: true, value: true }}
render={({ meta, input }) => {
const [prevActive, setPrevActive] = useState<boolean>(!!meta.active)
useEffect(() => {
async function setAddressFromENS() {
if (isValidEnsName(input.value)) {
try { try {
const resolverAddr = await getAddressFromENS(value) const resolverAddr = await getAddressFromENS(input.value)
} catch { fieldMutator(resolverAddr)
console.error('No resolver for ENS name') } catch (err) {
console.error('Error when trying to fetch address for ENS name: ', err)
} }
} }
}
if (prevActive && !meta.active) {
setAddressFromENS()
} else if (prevActive !== meta.active) {
setPrevActive(meta.active)
}
}, [meta.active])
return null
}} }}
</OnChange> />
</> </>
) )

View File

@ -69,6 +69,9 @@ const SendFunds = ({
onTokenChange: (args, state, utils) => { onTokenChange: (args, state, utils) => {
utils.changeValue(state, 'amount', () => '') utils.changeValue(state, 'amount', () => '')
}, },
setRecipient: (args, state, utils) => {
utils.changeValue(state, 'recipientAddress', () => args[0])
},
} }
return ( return (
@ -111,6 +114,7 @@ const SendFunds = ({
placeholder="Recipient*" placeholder="Recipient*"
text="Recipient*" text="Recipient*"
className={classes.addressInput} className={classes.addressInput}
fieldMutator={mutators.setRecipient}
/> />
</Col> </Col>
</Row> </Row>