add validator for already existing token

This commit is contained in:
Mikhail Mikheev 2019-04-24 16:15:00 +04:00
parent d57c4caa15
commit aff310806d
3 changed files with 17 additions and 4 deletions

View File

@ -70,6 +70,7 @@ class Tokens extends React.Component<Props, State> {
safeAddress={safeAddress} safeAddress={safeAddress}
activeTokens={activeTokens} activeTokens={activeTokens}
updateActiveTokens={updateActiveTokens} updateActiveTokens={updateActiveTokens}
tokens={tokens}
/> />
)} )}
</React.Fragment> </React.Fragment>

View File

@ -15,10 +15,10 @@ import GnoForm from '~/components/forms/GnoForm'
import TextField from '~/components/forms/TextField' import TextField from '~/components/forms/TextField'
import Hairline from '~/components/layout/Hairline' import Hairline from '~/components/layout/Hairline'
import { composeValidators, required, mustBeEthereumAddress } from '~/components/forms/validator' 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 { setImageToPlaceholder } from '~/routes/safe/components/Balances/utils'
import TokenPlaceholder from '~/routes/safe/components/Balances/assets/token_placeholder.svg' import TokenPlaceholder from '~/routes/safe/components/Balances/assets/token_placeholder.svg'
import { addressIsTokenContract } from './validators' import { addressIsTokenContract, doesntExistInTokenList } from './validators'
import { styles } from './style' import { styles } from './style'
type Props = { type Props = {
@ -27,6 +27,7 @@ type Props = {
updateActiveTokens: Function, updateActiveTokens: Function,
safeAddress: string, safeAddress: string,
activeTokens: List<TokenProps>, activeTokens: List<TokenProps>,
tokens: List<Token>,
setActiveScreen: Function, setActiveScreen: Function,
onClose: Function, onClose: Function,
} }
@ -40,7 +41,7 @@ const INITIAL_FORM_STATE = {
const AddCustomToken = (props: Props) => { const AddCustomToken = (props: Props) => {
const { const {
classes, setActiveScreen, onClose, addToken, updateActiveTokens, safeAddress, activeTokens, classes, setActiveScreen, onClose, addToken, updateActiveTokens, safeAddress, activeTokens, tokens,
} = props } = props
const [formValues, setFormValues] = useState(INITIAL_FORM_STATE) const [formValues, setFormValues] = useState(INITIAL_FORM_STATE)
@ -84,7 +85,7 @@ const AddCustomToken = (props: Props) => {
name="address" name="address"
component={TextField} component={TextField}
type="text" type="text"
validate={composeValidators(required, mustBeEthereumAddress, addressIsTokenContract)} validate={composeValidators(required, mustBeEthereumAddress, doesntExistInTokenList(tokens), addressIsTokenContract)}
placeholder="Token contract address*" placeholder="Token contract address*"
text="Token contract address*" text="Token contract address*"
className={classes.addressInput} className={classes.addressInput}

View File

@ -1,5 +1,8 @@
// @flow // @flow
import { List } from 'immutable'
import { getWeb3 } from '~/logic/wallets/getWeb3' 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) => { export const simpleMemoize = (fn: Function) => {
let lastArg let lastArg
@ -22,3 +25,11 @@ export const addressIsTokenContract = simpleMemoize(async (tokenAddress: string)
return 'Not a token address' 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'
}
})