From e5061a5731007f8a536e3b2c08e0dfb4f578f463 Mon Sep 17 00:00:00 2001 From: Mikhail Mikheev Date: Fri, 26 Jul 2019 17:47:31 +0400 Subject: [PATCH] set address from ens on blur --- src/components/forms/AddressInput/index.jsx | 45 ++++++++++++++----- .../SendModal/screens/SendFunds/index.jsx | 4 ++ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/components/forms/AddressInput/index.jsx b/src/components/forms/AddressInput/index.jsx index c038a198..33116a16 100644 --- a/src/components/forms/AddressInput/index.jsx +++ b/src/components/forms/AddressInput/index.jsx @@ -1,7 +1,6 @@ // @flow import * as React from 'react' import { Field } from 'react-final-form' -import { OnChange } from 'react-final-form-listeners' import TextField from '~/components/forms/TextField' import { composeValidators, required, mustBeEthereumAddress } from '~/components/forms/validator' import { getAddressFromENS } from '~/logic/wallets/getWeb3' @@ -11,37 +10,61 @@ type Props = { name?: string, text?: string, placeholder?: string, + fieldMutator: Function, } 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 = ({ className = '', name = 'recipientAddress', text = 'Recipient*', placeholder = 'Recipient*', + fieldMutator, }: Props): React.Element<*> => ( <> - - {async (value) => { - if (isValidEnsName(value)) { - try { - const resolverAddr = await getAddressFromENS(value) - } catch { - console.error('No resolver for ENS name') + { + const [prevActive, setPrevActive] = useState(!!meta.active) + + 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 }} - + /> ) diff --git a/src/routes/safe/components/Balances/SendModal/screens/SendFunds/index.jsx b/src/routes/safe/components/Balances/SendModal/screens/SendFunds/index.jsx index f0334822..1e3e039f 100644 --- a/src/routes/safe/components/Balances/SendModal/screens/SendFunds/index.jsx +++ b/src/routes/safe/components/Balances/SendModal/screens/SendFunds/index.jsx @@ -69,6 +69,9 @@ const SendFunds = ({ onTokenChange: (args, state, utils) => { utils.changeValue(state, 'amount', () => '') }, + setRecipient: (args, state, utils) => { + utils.changeValue(state, 'recipientAddress', () => args[0]) + }, } return ( @@ -111,6 +114,7 @@ const SendFunds = ({ placeholder="Recipient*" text="Recipient*" className={classes.addressInput} + fieldMutator={mutators.setRecipient} />