check token in relay and update the form state if found
This commit is contained in:
parent
4b1a0f785d
commit
92e77deb60
|
@ -35,26 +35,9 @@ class Tokens extends React.Component<Props, State> {
|
||||||
activeScreen,
|
activeScreen,
|
||||||
})
|
})
|
||||||
|
|
||||||
onTokenAdd = (formValues) => {
|
|
||||||
const {
|
|
||||||
addToken, updateActiveTokens, safeAddress, activeTokens,
|
|
||||||
} = this.props
|
|
||||||
|
|
||||||
const activeTokensAddresses = List(activeTokens.map(({ address }) => address))
|
|
||||||
const token = {
|
|
||||||
address: formValues.tokenAddress,
|
|
||||||
decimals: formValues.tokenDecimals,
|
|
||||||
symbol: formValues.tokenSymbol,
|
|
||||||
name: formValues.tokenSymbol,
|
|
||||||
}
|
|
||||||
|
|
||||||
addToken(token)
|
|
||||||
updateActiveTokens(safeAddress, activeTokensAddresses.push(token.address))
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const {
|
const {
|
||||||
onClose, classes, tokens, activeTokens, fetchTokens, updateActiveTokens, safeAddress,
|
onClose, classes, tokens, activeTokens, fetchTokens, updateActiveTokens, safeAddress, addToken,
|
||||||
} = this.props
|
} = this.props
|
||||||
const { activeScreen } = this.state
|
const { activeScreen } = this.state
|
||||||
|
|
||||||
|
@ -80,7 +63,14 @@ class Tokens extends React.Component<Props, State> {
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{activeScreen === 'addCustomToken' && (
|
{activeScreen === 'addCustomToken' && (
|
||||||
<AddCustomToken setActiveScreen={this.setActiveScreen} onTokenAdd={this.onTokenAdd} onClose={onClose} />
|
<AddCustomToken
|
||||||
|
setActiveScreen={this.setActiveScreen}
|
||||||
|
onClose={onClose}
|
||||||
|
addToken={addToken}
|
||||||
|
safeAddress={safeAddress}
|
||||||
|
activeTokens={activeTokens}
|
||||||
|
updateActiveTokens={updateActiveTokens}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// @flow
|
// @flow
|
||||||
import React from 'react'
|
import React, { useState } from 'react'
|
||||||
|
import { List } from 'immutable'
|
||||||
import { withStyles } from '@material-ui/core/styles'
|
import { withStyles } from '@material-ui/core/styles'
|
||||||
import Block from '~/components/layout/Block'
|
import Block from '~/components/layout/Block'
|
||||||
import Paragraph from '~/components/layout/Paragraph'
|
import Paragraph from '~/components/layout/Paragraph'
|
||||||
|
@ -15,32 +16,48 @@ import Hairline from '~/components/layout/Hairline'
|
||||||
import {
|
import {
|
||||||
composeValidators, required, mustBeEthereumAddress, mustBeInteger,
|
composeValidators, required, mustBeEthereumAddress, mustBeInteger,
|
||||||
} from '~/components/forms/validator'
|
} from '~/components/forms/validator'
|
||||||
|
import { type TokenProps } from '~/logic/tokens/store/model/token'
|
||||||
import TokenPlaceholder from '~/routes/safe/components/Balances/assets/token_placeholder.svg'
|
import TokenPlaceholder from '~/routes/safe/components/Balances/assets/token_placeholder.svg'
|
||||||
|
import { checkTokenExistence, INITIAL_FORM_STATE } from './validators'
|
||||||
import { styles } from './style'
|
import { styles } from './style'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
classes: Object,
|
classes: Object,
|
||||||
onTokenAdd: Function,
|
addToken: Function,
|
||||||
|
updateActiveTokens: Function,
|
||||||
|
safeAddress: string,
|
||||||
|
activeTokens: List<TokenProps>,
|
||||||
setActiveScreen: Function,
|
setActiveScreen: Function,
|
||||||
onClose: Function,
|
onClose: Function,
|
||||||
}
|
}
|
||||||
|
|
||||||
const AddCustomToken = (props: Props) => {
|
const AddCustomToken = (props: Props) => {
|
||||||
const {
|
const {
|
||||||
classes, setActiveScreen, onTokenAdd, onClose,
|
classes, setActiveScreen, onClose, addToken, updateActiveTokens, safeAddress, activeTokens,
|
||||||
} = props
|
} = props
|
||||||
|
const [formValue, setFormValue] = useState(INITIAL_FORM_STATE)
|
||||||
|
|
||||||
|
const handleSubmit = (formValues) => {
|
||||||
|
const activeTokensAddresses = activeTokens.map(({ address }) => address)
|
||||||
|
const token = {
|
||||||
|
address: formValues.address,
|
||||||
|
decimals: formValues.decimals,
|
||||||
|
symbol: formValues.symbol,
|
||||||
|
name: formValues.symbol,
|
||||||
|
}
|
||||||
|
|
||||||
|
addToken(token)
|
||||||
|
updateActiveTokens(safeAddress, activeTokensAddresses.push(token.address))
|
||||||
|
onClose()
|
||||||
|
}
|
||||||
|
|
||||||
const goBackToTokenList = () => {
|
const goBackToTokenList = () => {
|
||||||
setActiveScreen('tokenList')
|
setActiveScreen('tokenList')
|
||||||
}
|
}
|
||||||
const handleSubmit = (values) => {
|
|
||||||
onTokenAdd(values)
|
|
||||||
onClose()
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
<GnoForm onSubmit={handleSubmit}>
|
<GnoForm onSubmit={handleSubmit} initialValues={formValue}>
|
||||||
{() => (
|
{() => (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
<Block className={classes.formContainer}>
|
<Block className={classes.formContainer}>
|
||||||
|
@ -48,10 +65,10 @@ const AddCustomToken = (props: Props) => {
|
||||||
Add custom token
|
Add custom token
|
||||||
</Paragraph>
|
</Paragraph>
|
||||||
<Field
|
<Field
|
||||||
name="tokenAddress"
|
name="address"
|
||||||
component={TextField}
|
component={TextField}
|
||||||
type="text"
|
type="text"
|
||||||
validate={composeValidators(required, mustBeEthereumAddress)}
|
validate={composeValidators(required, mustBeEthereumAddress, checkTokenExistence(setFormValue))}
|
||||||
placeholder="Token contract address*"
|
placeholder="Token contract address*"
|
||||||
text="Token contract address*"
|
text="Token contract address*"
|
||||||
className={classes.addressInput}
|
className={classes.addressInput}
|
||||||
|
@ -59,7 +76,7 @@ const AddCustomToken = (props: Props) => {
|
||||||
<Row>
|
<Row>
|
||||||
<Col xs={6} layout="column">
|
<Col xs={6} layout="column">
|
||||||
<Field
|
<Field
|
||||||
name="tokenSymbol"
|
name="symbol"
|
||||||
component={TextField}
|
component={TextField}
|
||||||
type="text"
|
type="text"
|
||||||
validate={required}
|
validate={required}
|
||||||
|
@ -68,8 +85,9 @@ const AddCustomToken = (props: Props) => {
|
||||||
className={classes.addressInput}
|
className={classes.addressInput}
|
||||||
/>
|
/>
|
||||||
<Field
|
<Field
|
||||||
name="tokenDecimals"
|
name="decimals"
|
||||||
component={TextField}
|
component={TextField}
|
||||||
|
disabled
|
||||||
type="text"
|
type="text"
|
||||||
validate={composeValidators(required, mustBeInteger)}
|
validate={composeValidators(required, mustBeInteger)}
|
||||||
placeholder="Token decimals*"
|
placeholder="Token decimals*"
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
// @flow
|
||||||
|
import { fetchToken } from '~/logic/tokens/api'
|
||||||
|
|
||||||
|
export const INITIAL_FORM_STATE = {
|
||||||
|
address: '',
|
||||||
|
decimals: '',
|
||||||
|
symbol: '',
|
||||||
|
logoUri: '',
|
||||||
|
}
|
||||||
|
|
||||||
|
export const simpleMemoize = (fn: Function) => {
|
||||||
|
let lastArg
|
||||||
|
let lastResult
|
||||||
|
return (arg: any) => {
|
||||||
|
if (arg !== lastArg) {
|
||||||
|
lastArg = arg
|
||||||
|
lastResult = fn(arg)
|
||||||
|
}
|
||||||
|
return lastResult
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const checkTokenExistence = (updateForm: ?Function) => simpleMemoize(async (tokenAddress: string) => {
|
||||||
|
if (!tokenAddress && updateForm) {
|
||||||
|
updateForm(INITIAL_FORM_STATE)
|
||||||
|
}
|
||||||
|
|
||||||
|
const relayToken = await fetchToken(tokenAddress)
|
||||||
|
|
||||||
|
if (!relayToken.data.count) {
|
||||||
|
return "Couldn't find the token"
|
||||||
|
}
|
||||||
|
|
||||||
|
if (updateForm) {
|
||||||
|
const {
|
||||||
|
address, symbol, decimals, logoUri,
|
||||||
|
} = relayToken.data.results[0]
|
||||||
|
updateForm({
|
||||||
|
address,
|
||||||
|
symbol,
|
||||||
|
decimals: String(decimals),
|
||||||
|
logoUri,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
Loading…
Reference in New Issue