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

138 lines
3.2 KiB

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { debounce } from 'lodash'
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'
export default class SendAmountRow extends Component {
static propTypes = {
amount: PropTypes.string,
balance: PropTypes.string,
conversionRate: PropTypes.number,
gasTotal: PropTypes.string,
inError: PropTypes.bool,
primaryCurrency: PropTypes.string,
selectedToken: PropTypes.object,
setMaxModeTo: PropTypes.func,
tokenBalance: PropTypes.string,
updateGasFeeError: PropTypes.func,
updateSendAmount: PropTypes.func,
updateSendAmountError: PropTypes.func,
updateGas: PropTypes.func,
maxModeOn: PropTypes.bool,
}
static contextTypes = {
t: PropTypes.func,
}
componentDidUpdate (prevProps) {
const { maxModeOn: prevMaxModeOn, gasTotal: prevGasTotal } = prevProps
const { maxModeOn, amount, gasTotal, selectedToken } = this.props
if (maxModeOn && selectedToken && !prevMaxModeOn) {
this.updateGas(amount)
}
if (prevGasTotal !== gasTotal) {
this.validateAmount(amount)
}
}
updateGas = debounce(this.updateGas.bind(this), 500)
validateAmount (amount) {
const {
balance,
conversionRate,
gasTotal,
primaryCurrency,
selectedToken,
tokenBalance,
updateGasFeeError,
updateSendAmountError,
} = this.props
updateSendAmountError({
amount,
balance,
conversionRate,
gasTotal,
primaryCurrency,
selectedToken,
tokenBalance,
})
if (selectedToken) {
updateGasFeeError({
balance,
conversionRate,
gasTotal,
primaryCurrency,
selectedToken,
tokenBalance,
})
}
}
updateAmount (amount) {
const { updateSendAmount, setMaxModeTo } = this.props
setMaxModeTo(false)
updateSendAmount(amount)
}
updateGas (amount) {
const { selectedToken, updateGas } = this.props
if (selectedToken) {
updateGas({ amount })
}
}
handleChange = (newAmount) => {
this.validateAmount(newAmount)
this.updateGas(newAmount)
this.updateAmount(newAmount)
}
renderInput () {
const { amount, inError, selectedToken } = this.props
return selectedToken ?
(
<UserPreferencedTokenInput
error={inError}
onChange={this.handleChange}
token={selectedToken}
value={amount}
/>
)
: (
<UserPreferencedCurrencyInput
error={inError}
onChange={this.handleChange}
value={amount}
/>
)
}
render () {
const { gasTotal, inError } = this.props
return (
<SendRowWrapper
label={`${this.context.t('amount')}:`}
showError={inError}
errorType="amount"
>
{gasTotal && <AmountMaxButton inError={inError} />}
{ this.renderInput() }
</SendRowWrapper>
)
}
}