1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-01 00:28:06 +01:00
metamask-extension/ui/app/pages/send/send-content/send-amount-row/send-amount-row.component.js

131 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-04-11 16:21:54 +02:00
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'
2018-04-11 16:21:54 +02:00
export default class SendAmountRow extends Component {
static propTypes = {
amount: PropTypes.string,
balance: PropTypes.string,
2018-04-26 18:38:38 +02:00
conversionRate: PropTypes.number,
2018-04-11 16:21:54 +02:00
gasTotal: PropTypes.string,
inError: PropTypes.bool,
2018-04-11 16:21:54 +02:00
primaryCurrency: PropTypes.string,
selectedToken: PropTypes.object,
setMaxModeTo: PropTypes.func,
2018-04-11 16:21:54 +02:00
tokenBalance: PropTypes.string,
2018-06-29 19:19:40 +02:00
updateGasFeeError: PropTypes.func,
2018-04-11 16:21:54 +02:00
updateSendAmount: PropTypes.func,
updateSendAmountError: PropTypes.func,
updateGas: PropTypes.func,
maxModeOn: PropTypes.bool,
}
static contextTypes = {
t: PropTypes.func,
}
2018-04-11 16:21:54 +02:00
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)
2018-04-11 16:21:54 +02:00
validateAmount (amount) {
const {
2018-04-26 18:38:38 +02:00
balance,
conversionRate,
2018-04-11 16:21:54 +02:00
gasTotal,
primaryCurrency,
selectedToken,
tokenBalance,
2018-06-29 19:19:40 +02:00
updateGasFeeError,
2018-04-11 16:21:54 +02:00
updateSendAmountError,
} = this.props
updateSendAmountError({
amount,
balance,
conversionRate,
gasTotal,
primaryCurrency,
selectedToken,
tokenBalance,
})
2018-06-29 19:19:40 +02:00
if (selectedToken) {
updateGasFeeError({
balance,
conversionRate,
gasTotal,
primaryCurrency,
selectedToken,
tokenBalance,
})
}
2018-04-11 16:21:54 +02:00
}
updateAmount (amount) {
2018-04-11 16:21:54 +02:00
const { updateSendAmount, setMaxModeTo } = this.props
setMaxModeTo(false)
updateSendAmount(amount)
}
updateGas (amount) {
const { selectedToken, updateGas } = this.props
if (selectedToken) {
updateGas({ amount })
}
2018-04-11 16:21:54 +02:00
}
handleChange = (newAmount) => {
this.validateAmount(newAmount)
this.updateGas(newAmount)
this.updateAmount(newAmount)
}
renderInput () {
const { amount, inError, selectedToken } = this.props
const Component = selectedToken ? UserPreferencedTokenInput : UserPreferencedCurrencyInput
return (
<Component
onChange={this.handleChange}
error={inError}
value={amount}
/>
)
}
2018-04-11 16:21:54 +02:00
render () {
const { gasTotal, inError } = this.props
2018-04-11 16:21:54 +02:00
return (
<SendRowWrapper
label={`${this.context.t('amount')}:`}
showError={inError}
errorType="amount"
2018-04-11 16:21:54 +02:00
>
{gasTotal && <AmountMaxButton inError={inError} />}
{ this.renderInput() }
2018-04-11 16:21:54 +02:00
</SendRowWrapper>
)
2018-04-11 16:21:54 +02:00
}
}