add validator for already existing token
This commit is contained in:
parent
d57c4caa15
commit
aff310806d
|
@ -70,6 +70,7 @@ class Tokens extends React.Component<Props, State> {
|
|||
safeAddress={safeAddress}
|
||||
activeTokens={activeTokens}
|
||||
updateActiveTokens={updateActiveTokens}
|
||||
tokens={tokens}
|
||||
/>
|
||||
)}
|
||||
</React.Fragment>
|
||||
|
|
|
@ -15,10 +15,10 @@ import GnoForm from '~/components/forms/GnoForm'
|
|||
import TextField from '~/components/forms/TextField'
|
||||
import Hairline from '~/components/layout/Hairline'
|
||||
import { composeValidators, required, mustBeEthereumAddress } from '~/components/forms/validator'
|
||||
import { type TokenProps } from '~/logic/tokens/store/model/token'
|
||||
import { type TokenProps, type Token } from '~/logic/tokens/store/model/token'
|
||||
import { setImageToPlaceholder } from '~/routes/safe/components/Balances/utils'
|
||||
import TokenPlaceholder from '~/routes/safe/components/Balances/assets/token_placeholder.svg'
|
||||
import { addressIsTokenContract } from './validators'
|
||||
import { addressIsTokenContract, doesntExistInTokenList } from './validators'
|
||||
import { styles } from './style'
|
||||
|
||||
type Props = {
|
||||
|
@ -27,6 +27,7 @@ type Props = {
|
|||
updateActiveTokens: Function,
|
||||
safeAddress: string,
|
||||
activeTokens: List<TokenProps>,
|
||||
tokens: List<Token>,
|
||||
setActiveScreen: Function,
|
||||
onClose: Function,
|
||||
}
|
||||
|
@ -40,7 +41,7 @@ const INITIAL_FORM_STATE = {
|
|||
|
||||
const AddCustomToken = (props: Props) => {
|
||||
const {
|
||||
classes, setActiveScreen, onClose, addToken, updateActiveTokens, safeAddress, activeTokens,
|
||||
classes, setActiveScreen, onClose, addToken, updateActiveTokens, safeAddress, activeTokens, tokens,
|
||||
} = props
|
||||
const [formValues, setFormValues] = useState(INITIAL_FORM_STATE)
|
||||
|
||||
|
@ -84,7 +85,7 @@ const AddCustomToken = (props: Props) => {
|
|||
name="address"
|
||||
component={TextField}
|
||||
type="text"
|
||||
validate={composeValidators(required, mustBeEthereumAddress, addressIsTokenContract)}
|
||||
validate={composeValidators(required, mustBeEthereumAddress, doesntExistInTokenList(tokens), addressIsTokenContract)}
|
||||
placeholder="Token contract address*"
|
||||
text="Token contract address*"
|
||||
className={classes.addressInput}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
// @flow
|
||||
import { List } from 'immutable'
|
||||
import { getWeb3 } from '~/logic/wallets/getWeb3'
|
||||
import { type Token } from '~/logic/tokens/store/model/token'
|
||||
import { sameAddress } from '~/logic/wallets/ethAddresses'
|
||||
|
||||
export const simpleMemoize = (fn: Function) => {
|
||||
let lastArg
|
||||
|
@ -22,3 +25,11 @@ export const addressIsTokenContract = simpleMemoize(async (tokenAddress: string)
|
|||
return 'Not a token address'
|
||||
}
|
||||
})
|
||||
|
||||
export const doesntExistInTokenList = (tokenList: List<Token>) => simpleMemoize((tokenAddress: string) => {
|
||||
const tokenIndex = tokenList.findIndex(({ address }) => sameAddress(address, tokenAddress))
|
||||
|
||||
if (tokenIndex !== -1) {
|
||||
return 'Token already exists in your token list'
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue