import React, { Component } from 'react' import PropTypes from 'prop-types' import classnames from 'classnames' import debounce from 'lodash.debounce' export default class AdvancedTabContent extends Component { static contextTypes = { t: PropTypes.func, } static propTypes = { updateCustomGasPrice: PropTypes.func, updateCustomGasLimit: PropTypes.func, customGasPrice: PropTypes.number, customGasLimit: PropTypes.number, insufficientBalance: PropTypes.bool, customPriceIsSafe: PropTypes.bool, isSpeedUp: PropTypes.bool, showGasPriceInfoModal: PropTypes.func, showGasLimitInfoModal: PropTypes.func, } debouncedGasLimitReset = debounce((dVal) => { if (dVal < 21000) { this.props.updateCustomGasLimit(21000) } }, 1000, { trailing: true }) onChangeGasLimit = (val) => { this.props.updateCustomGasLimit(val) this.debouncedGasLimitReset(val) } gasInputError ({ labelKey, insufficientBalance, customPriceIsSafe, isSpeedUp, value }) { const { t } = this.context let errorText let errorType let isInError = true if (insufficientBalance) { errorText = t('insufficientBalance') errorType = 'error' } else if (labelKey === 'gasPrice' && isSpeedUp && value === 0) { errorText = t('zeroGasPriceOnSpeedUpError') errorType = 'error' } else if (labelKey === 'gasPrice' && !customPriceIsSafe) { errorText = t('gasPriceExtremelyLow') errorType = 'warning' } else { isInError = false } return { isInError, errorText, errorType, } } gasInput ({ labelKey, value, onChange, insufficientBalance, showGWEI, customPriceIsSafe, isSpeedUp }) { const { isInError, errorText, errorType, } = this.gasInputError({ labelKey, insufficientBalance, customPriceIsSafe, isSpeedUp, value }) return (
onChange(Number(event.target.value))} />
onChange(value + 1)}>
onChange(value - 1)}>
{ isInError ?
{ errorText }
: null }
) } infoButton (onClick) { return } renderGasEditRow (gasInputArgs) { return (
{ this.context.t(gasInputArgs.labelKey) } { this.infoButton(() => gasInputArgs.infoOnClick()) }
{ this.gasInput(gasInputArgs) }
) } render () { const { customGasPrice, updateCustomGasPrice, customGasLimit, insufficientBalance, customPriceIsSafe, isSpeedUp, showGasPriceInfoModal, showGasLimitInfoModal, } = this.props return (
{ this.renderGasEditRow({ labelKey: 'gasPrice', value: customGasPrice, onChange: updateCustomGasPrice, insufficientBalance, customPriceIsSafe, showGWEI: true, isSpeedUp, infoOnClick: showGasPriceInfoModal, }) } { this.renderGasEditRow({ labelKey: 'gasLimit', value: customGasLimit, onChange: this.onChangeGasLimit, insufficientBalance, customPriceIsSafe, infoOnClick: showGasLimitInfoModal, }) }
) } }