2018-04-11 16:21:54 +02:00
|
|
|
import React, { Component } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
2020-01-27 17:45:48 +01:00
|
|
|
import { debounce } from 'lodash'
|
2019-03-22 00:03:30 +01:00
|
|
|
import SendRowWrapper from '../send-row-wrapper'
|
|
|
|
import AmountMaxButton from './amount-max-button'
|
2019-04-17 21:15:13 +02:00
|
|
|
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 = {
|
2018-04-27 02:38:14 +02:00
|
|
|
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,
|
2018-04-27 02:38:14 +02:00
|
|
|
inError: PropTypes.bool,
|
2018-04-11 16:21:54 +02:00
|
|
|
primaryCurrency: PropTypes.string,
|
|
|
|
selectedToken: PropTypes.object,
|
2018-04-27 02:38:14 +02:00
|
|
|
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,
|
2018-04-27 02:38:14 +02:00
|
|
|
updateSendAmountError: PropTypes.func,
|
2018-06-15 23:36:52 +02:00
|
|
|
updateGas: PropTypes.func,
|
2020-03-11 21:13:48 +01:00
|
|
|
maxModeOn: PropTypes.bool,
|
2018-11-20 01:06:34 +01:00
|
|
|
}
|
2018-07-02 04:09:28 +02:00
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2018-11-20 01:06:34 +01:00
|
|
|
}
|
2018-04-11 16:21:54 +02:00
|
|
|
|
2020-03-11 21:13:48 +01: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:23:54 +01:00
|
|
|
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,
|
2018-04-27 02:38:14 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-28 18:41:23 +02:00
|
|
|
updateAmount (amount) {
|
2018-04-11 16:21:54 +02:00
|
|
|
const { updateSendAmount, setMaxModeTo } = this.props
|
|
|
|
|
|
|
|
setMaxModeTo(false)
|
|
|
|
updateSendAmount(amount)
|
2018-06-15 23:36:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
updateGas (amount) {
|
|
|
|
const { selectedToken, updateGas } = this.props
|
|
|
|
|
|
|
|
if (selectedToken) {
|
|
|
|
updateGas({ amount })
|
|
|
|
}
|
2018-04-11 16:21:54 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 21:13:48 +01:00
|
|
|
handleChange = (newAmount) => {
|
|
|
|
this.validateAmount(newAmount)
|
|
|
|
this.updateGas(newAmount)
|
|
|
|
this.updateAmount(newAmount)
|
|
|
|
}
|
|
|
|
|
2018-10-17 01:03:29 +02:00
|
|
|
renderInput () {
|
|
|
|
const { amount, inError, selectedToken } = this.props
|
|
|
|
const Component = selectedToken ? UserPreferencedTokenInput : UserPreferencedCurrencyInput
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Component
|
2020-03-11 21:13:48 +01:00
|
|
|
onChange={this.handleChange}
|
2018-10-17 01:03:29 +02:00
|
|
|
error={inError}
|
|
|
|
value={amount}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:21:54 +02:00
|
|
|
render () {
|
2018-10-17 01:03:29 +02:00
|
|
|
const { gasTotal, inError } = this.props
|
2018-04-11 16:21:54 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SendRowWrapper
|
|
|
|
label={`${this.context.t('amount')}:`}
|
|
|
|
showError={inError}
|
2019-11-18 16:08:47 +01:00
|
|
|
errorType="amount"
|
2018-04-11 16:21:54 +02:00
|
|
|
>
|
2019-05-20 18:38:08 +02:00
|
|
|
{gasTotal && <AmountMaxButton inError={inError} />}
|
2018-10-17 01:03:29 +02:00
|
|
|
{ this.renderInput() }
|
2018-04-11 16:21:54 +02:00
|
|
|
</SendRowWrapper>
|
2018-04-27 02:38:14 +02:00
|
|
|
)
|
2018-04-11 16:21:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|