A Metamask fork with Infura removed and default networks editable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ciphermask/ui/app/pages/send/send-content/send-amount-row/send-amount-row.component.js

123 lines
2.9 KiB

7 years ago
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import debounce from 'lodash.debounce'
import SendRowWrapper from '../send-row-wrapper'
import AmountMaxButton from './amount-max-button'
import UserPreferencedCurrencyInput from '../../../../components/app/user-preferenced-currency-input'
import UserPreferencedTokenInput from '../../../../components/app/user-preferenced-token-input'
7 years ago
export default class SendAmountRow extends Component {
static propTypes = {
amount: PropTypes.string,
amountConversionRate: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]),
balance: PropTypes.string,
conversionRate: PropTypes.number,
convertedCurrency: PropTypes.string,
7 years ago
gasTotal: PropTypes.string,
inError: PropTypes.bool,
7 years ago
primaryCurrency: PropTypes.string,
selectedToken: PropTypes.object,
setMaxModeTo: PropTypes.func,
7 years ago
tokenBalance: PropTypes.string,
updateGasFeeError: PropTypes.func,
7 years ago
updateSendAmount: PropTypes.func,
updateSendAmountError: PropTypes.func,
updateGas: PropTypes.func,
}
static contextTypes = {
t: PropTypes.func,
}
7 years ago
updateGas = debounce(this.updateGas.bind(this), 500)
7 years ago
validateAmount (amount) {
const {
amountConversionRate,
balance,
conversionRate,
7 years ago
gasTotal,
primaryCurrency,
selectedToken,
tokenBalance,
updateGasFeeError,
7 years ago
updateSendAmountError,
} = this.props
updateSendAmountError({
amount,
amountConversionRate,
balance,
conversionRate,
gasTotal,
primaryCurrency,
selectedToken,
tokenBalance,
})
if (selectedToken) {
updateGasFeeError({
amountConversionRate,
balance,
conversionRate,
gasTotal,
primaryCurrency,
selectedToken,
tokenBalance,
})
}
7 years ago
}
updateAmount (amount) {
7 years ago
const { updateSendAmount, setMaxModeTo } = this.props
setMaxModeTo(false)
updateSendAmount(amount)
}
updateGas (amount) {
const { selectedToken, updateGas } = this.props
if (selectedToken) {
updateGas({ amount })
}
7 years ago
}
renderInput () {
const { amount, inError, selectedToken } = this.props
const Component = selectedToken ? UserPreferencedTokenInput : UserPreferencedCurrencyInput
return (
<Component
onChange={newAmount => {
this.validateAmount(newAmount)
this.updateGas(newAmount)
this.updateAmount(newAmount)
}}
error={inError}
value={amount}
/>
)
}
7 years ago
render () {
const { gasTotal, inError } = this.props
7 years ago
return (
<SendRowWrapper
label={`${this.context.t('amount')}:`}
showError={inError}
errorType="amount"
7 years ago
>
{gasTotal && <AmountMaxButton inError={inError} />}
{ this.renderInput() }
7 years ago
</SendRowWrapper>
)
7 years ago
}
}