import React, { Component } from 'react'; import PropTypes from 'prop-types'; import PageContainer from '../../../components/ui/page-container'; import { Tabs, Tab } from '../../../components/ui/tabs'; import { calcGasTotal } from '../../send/send.utils'; import { sumHexWEIsToUnformattedFiat } from '../../../helpers/utils/conversions.util'; import AdvancedGasInputs from '../../../components/app/gas-customization/advanced-gas-inputs'; import BasicTabContent from '../../../components/app/gas-customization/gas-modal-page-container/basic-tab-content'; import { GAS_ESTIMATE_TYPES } from '../../../helpers/constants/common'; import { CUSTOM_GAS_ESTIMATE } from '../../../../shared/constants/gas'; import { EVENT } from '../../../../shared/constants/metametrics'; export default class GasModalPageContainer extends Component { static contextTypes = { t: PropTypes.func, trackEvent: PropTypes.func, }; static propTypes = { insufficientBalance: PropTypes.bool, gasPriceButtonGroupProps: PropTypes.object, infoRowProps: PropTypes.shape({ originalTotalFiat: PropTypes.string, originalTotalEth: PropTypes.string, newTotalFiat: PropTypes.string, newTotalEth: PropTypes.string, sendAmount: PropTypes.string, transactionFee: PropTypes.string, extraInfoRow: PropTypes.shape({ label: PropTypes.string, value: PropTypes.string, }), }), onSubmit: PropTypes.func, cancelAndClose: PropTypes.func, showCustomPriceTooLowWarning: PropTypes.bool, disableSave: PropTypes.bool, customGasLimitMessage: PropTypes.string, customTotalSupplement: PropTypes.string, usdConversionRate: PropTypes.number, customGasPrice: PropTypes.string, customGasLimit: PropTypes.string, setSwapsCustomizationModalPrice: PropTypes.func, setSwapsCustomizationModalLimit: PropTypes.func, gasEstimateLoadingHasFailed: PropTypes.bool, minimumGasLimit: PropTypes.number.isRequired, }; state = { gasSpeedType: '', }; setGasSpeedType(gasEstimateType) { if (gasEstimateType === GAS_ESTIMATE_TYPES.AVERAGE) { this.setState({ gasSpeedType: 'average' }); } else { this.setState({ gasSpeedType: 'fast' }); } } renderBasicTabContent(gasPriceButtonGroupProps) { return ( { this.setGasSpeedType(gasEstimateType); this.props.setSwapsCustomizationModalPrice(gasPrice); }, }} /> ); } renderAdvancedTabContent() { const { insufficientBalance, showCustomPriceTooLowWarning, infoRowProps: { transactionFee }, customGasLimitMessage, setSwapsCustomizationModalPrice, setSwapsCustomizationModalLimit, customGasPrice, customGasLimit, minimumGasLimit, } = this.props; return (
{this.context.t('newTransactionFee')}
{transactionFee}
{ this.setState({ gasSpeedType: CUSTOM_GAS_ESTIMATE }); setSwapsCustomizationModalPrice(updatedPrice); }} updateCustomGasLimit={(updatedLimit) => { this.setState({ gasSpeedType: CUSTOM_GAS_ESTIMATE }); setSwapsCustomizationModalLimit(updatedLimit); }} customGasPrice={customGasPrice} customGasLimit={customGasLimit} insufficientBalance={insufficientBalance} customPriceIsSafe={!showCustomPriceTooLowWarning} customGasLimitMessage={customGasLimitMessage} minimumGasLimit={minimumGasLimit} />
); } renderInfoRows( newTotalFiat, newTotalEth, sendAmount, transactionFee, extraInfoRow, ) { return (
{this.context.t('sendAmount')} {sendAmount}
{this.context.t('transactionFee')} {transactionFee}
{extraInfoRow && (
{extraInfoRow.label} {extraInfoRow.value}
)}
{this.context.t('newTotal')} {newTotalEth}
{newTotalFiat}
); } renderTabs() { const { gasPriceButtonGroupProps, infoRowProps: { newTotalFiat, newTotalEth, sendAmount, transactionFee, extraInfoRow, }, gasEstimateLoadingHasFailed, } = this.props; const basicTabInfo = { name: this.context.t('basic'), content: this.renderBasicTabContent({ ...gasPriceButtonGroupProps, handleGasPriceSelection: this.props.setSwapsCustomizationModalPrice, }), }; const advancedTabInfo = { name: this.context.t('advanced'), content: this.renderAdvancedTabContent(), }; const tabsToRender = gasEstimateLoadingHasFailed ? [advancedTabInfo] : [basicTabInfo, advancedTabInfo]; return ( {tabsToRender.map(({ name, content }, i) => (
{content} {this.renderInfoRows( newTotalFiat, newTotalEth, sendAmount, transactionFee, extraInfoRow, )}
))}
); } render() { const { cancelAndClose, onSubmit, disableSave, customGasPrice, customGasLimit, } = this.props; return (
cancelAndClose()} onClose={() => cancelAndClose()} onSubmit={() => { const newSwapGasTotal = calcGasTotal( customGasLimit, customGasPrice, ); this.context.trackEvent({ event: 'Gas Fees Changed', category: EVENT.CATEGORIES.SWAPS, properties: { speed_set: this.state.gasSpeedType, gas_fees: sumHexWEIsToUnformattedFiat( [newSwapGasTotal, this.props.customTotalSupplement], 'usd', this.props.usdConversionRate, )?.slice(1), }, }); onSubmit(customGasLimit, customGasPrice); }} submitText={this.context.t('save')} headerCloseText={this.context.t('close')} hideCancel />
); } }