1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-24 04:13:27 +02:00
metamask-extension/ui/app/pages/send/send-content/send-amount-row/send-amount-row.component.js

120 lines
2.8 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 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,
amountConversionRate: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]),
balance: PropTypes.string,
2018-04-26 18:38:38 +02:00
conversionRate: PropTypes.number,
convertedCurrency: PropTypes.string,
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,
}
static contextTypes = {
t: PropTypes.func,
}
2018-04-11 16:21:54 +02:00
validateAmount (amount) {
const {
amountConversionRate,
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,
amountConversionRate,
balance,
conversionRate,
gasTotal,
primaryCurrency,
selectedToken,
tokenBalance,
})
2018-06-29 19:19:40 +02:00
if (selectedToken) {
updateGasFeeError({
amountConversionRate,
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
}
renderInput () {
const { amount, inError, selectedToken } = this.props
const Component = selectedToken ? UserPreferencedTokenInput : UserPreferencedCurrencyInput
return (
<Component
onChange={newAmount => this.validateAmount(newAmount)}
onBlur={newAmount => {
this.updateGas(newAmount)
this.updateAmount(newAmount)
}}
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
}
}