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