set address from ens on blur
This commit is contained in:
parent
2beb8afc29
commit
e5061a5731
|
@ -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 }}
|
||||||
try {
|
render={({ meta, input }) => {
|
||||||
const resolverAddr = await getAddressFromENS(value)
|
const [prevActive, setPrevActive] = useState<boolean>(!!meta.active)
|
||||||
} catch {
|
|
||||||
console.error('No resolver for ENS name')
|
useEffect(() => {
|
||||||
|
async function setAddressFromENS() {
|
||||||
|
if (isValidEnsName(input.value)) {
|
||||||
|
try {
|
||||||
|
const resolverAddr = await getAddressFromENS(input.value)
|
||||||
|
fieldMutator(resolverAddr)
|
||||||
|
} 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>
|
/>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -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>
|
||||||
|
|
Loading…
Reference in New Issue