2021-02-04 19:15:23 +01:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { debounce } from 'lodash';
|
|
|
|
import SendRowWrapper from '../send-row-wrapper';
|
|
|
|
import UserPreferencedCurrencyInput from '../../../../components/app/user-preferenced-currency-input';
|
|
|
|
import UserPreferencedTokenInput from '../../../../components/app/user-preferenced-token-input';
|
|
|
|
import AmountMaxButton from './amount-max-button';
|
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,
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken: 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,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-07-02 04:09:28 +02:00
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-04-11 16:21:54 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentDidUpdate(prevProps) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { maxModeOn: prevMaxModeOn, gasTotal: prevGasTotal } = prevProps;
|
|
|
|
const { maxModeOn, amount, gasTotal, sendToken } = this.props;
|
2020-03-11 21:13:48 +01:00
|
|
|
|
2020-05-29 19:46:10 +02:00
|
|
|
if (maxModeOn && sendToken && !prevMaxModeOn) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.updateGas(amount);
|
2020-03-11 21:13:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (prevGasTotal !== gasTotal) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.validateAmount(amount);
|
2020-03-11 21:13:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
updateGas = debounce(this.updateGas.bind(this), 500);
|
2020-01-10 15:23:54 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
validateAmount(amount) {
|
2018-04-11 16:21:54 +02:00
|
|
|
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,
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken,
|
2018-04-11 16:21:54 +02:00
|
|
|
tokenBalance,
|
2018-06-29 19:19:40 +02:00
|
|
|
updateGasFeeError,
|
2018-04-11 16:21:54 +02:00
|
|
|
updateSendAmountError,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
2018-04-11 16:21:54 +02:00
|
|
|
|
|
|
|
updateSendAmountError({
|
|
|
|
amount,
|
|
|
|
balance,
|
|
|
|
conversionRate,
|
|
|
|
gasTotal,
|
|
|
|
primaryCurrency,
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken,
|
2018-04-11 16:21:54 +02:00
|
|
|
tokenBalance,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-06-29 19:19:40 +02:00
|
|
|
|
2020-05-29 19:46:10 +02:00
|
|
|
if (sendToken) {
|
2018-06-29 19:19:40 +02:00
|
|
|
updateGasFeeError({
|
|
|
|
balance,
|
|
|
|
conversionRate,
|
|
|
|
gasTotal,
|
|
|
|
primaryCurrency,
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken,
|
2018-06-29 19:19:40 +02:00
|
|
|
tokenBalance,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-06-29 19:19:40 +02:00
|
|
|
}
|
2018-04-11 16:21:54 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
updateAmount(amount) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { updateSendAmount, setMaxModeTo } = this.props;
|
2018-04-11 16:21:54 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
setMaxModeTo(false);
|
|
|
|
updateSendAmount(amount);
|
2018-06-15 23:36:52 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
updateGas(amount) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { sendToken, updateGas } = this.props;
|
2018-06-15 23:36:52 +02:00
|
|
|
|
2020-05-29 19:46:10 +02:00
|
|
|
if (sendToken) {
|
2021-02-04 19:15:23 +01:00
|
|
|
updateGas({ amount });
|
2018-06-15 23:36:52 +02:00
|
|
|
}
|
2018-04-11 16:21:54 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 21:13:48 +01:00
|
|
|
handleChange = (newAmount) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.validateAmount(newAmount);
|
|
|
|
this.updateGas(newAmount);
|
|
|
|
this.updateAmount(newAmount);
|
|
|
|
};
|
2020-03-11 21:13:48 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderInput() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { amount, inError, sendToken } = this.props;
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
return sendToken ? (
|
|
|
|
<UserPreferencedTokenInput
|
|
|
|
error={inError}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
token={sendToken}
|
|
|
|
value={amount}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<UserPreferencedCurrencyInput
|
|
|
|
error={inError}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
value={amount}
|
|
|
|
/>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-10-17 01:03:29 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2021-02-04 19:15:23 +01: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} />}
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.renderInput()}
|
2018-04-11 16:21:54 +02:00
|
|
|
</SendRowWrapper>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-04-11 16:21:54 +02:00
|
|
|
}
|
|
|
|
}
|