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

135 lines
3.1 KiB
JavaScript
Raw Normal View History

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 = {
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,
sendToken: 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
2020-11-03 00:41:28 +01:00
componentDidUpdate(prevProps) {
const { maxModeOn: prevMaxModeOn, gasTotal: prevGasTotal } = prevProps;
const { maxModeOn, amount, gasTotal, sendToken } = this.props;
if (maxModeOn && sendToken && !prevMaxModeOn) {
this.updateGas(amount);
}
if (prevGasTotal !== gasTotal) {
this.validateAmount(amount);
}
}
updateGas = debounce(this.updateGas.bind(this), 500);
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,
conversionRate,
2018-04-11 16:21:54 +02:00
gasTotal,
primaryCurrency,
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,
} = this.props;
2018-04-11 16:21:54 +02:00
updateSendAmountError({
amount,
balance,
conversionRate,
gasTotal,
primaryCurrency,
sendToken,
2018-04-11 16:21:54 +02:00
tokenBalance,
});
2018-06-29 19:19:40 +02:00
if (sendToken) {
2018-06-29 19:19:40 +02:00
updateGasFeeError({
balance,
conversionRate,
gasTotal,
primaryCurrency,
sendToken,
2018-06-29 19:19:40 +02:00
tokenBalance,
});
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) {
const { updateSendAmount, setMaxModeTo } = this.props;
2018-04-11 16:21:54 +02:00
setMaxModeTo(false);
updateSendAmount(amount);
}
2020-11-03 00:41:28 +01:00
updateGas(amount) {
const { sendToken, updateGas } = this.props;
if (sendToken) {
updateGas({ amount });
}
2018-04-11 16:21:54 +02:00
}
handleChange = (newAmount) => {
this.validateAmount(newAmount);
this.updateGas(newAmount);
this.updateAmount(newAmount);
};
2020-11-03 00:41:28 +01:00
renderInput() {
const { amount, inError, sendToken } = this.props;
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}
/>
);
}
2020-11-03 00:41:28 +01: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} />}
2020-11-03 00:41:28 +01:00
{this.renderInput()}
2018-04-11 16:21:54 +02:00
</SendRowWrapper>
);
2018-04-11 16:21:54 +02:00
}
}