import React, { Component } from 'react' import PropTypes from 'prop-types' import GasModalCard from '../../customize-gas-modal/gas-modal-card' import { MIN_GAS_PRICE_GWEI } from '../../send_/send.constants' import { getDecimalGasLimit, getDecimalGasPrice, getPrefixedHexGasLimit, getPrefixedHexGasPrice, } from './customize-gas.util' export default class CustomizeGas extends Component { static contextTypes = { t: PropTypes.func, } static propTypes = { txData: PropTypes.object.isRequired, hideModal: PropTypes.func, validate: PropTypes.func, onSubmit: PropTypes.func, } state = { gasPrice: 0, gasLimit: 0, originalGasPrice: 0, originalGasLimit: 0, } componentDidMount () { const { txData = {} } = this.props const { txParams: { gas: hexGasLimit, gasPrice: hexGasPrice } = {} } = txData const gasLimit = getDecimalGasLimit(hexGasLimit) const gasPrice = getDecimalGasPrice(hexGasPrice) this.setState({ gasPrice, gasLimit, originalGasPrice: gasPrice, originalGasLimit: gasLimit, }) } handleRevert () { const { originalGasPrice, originalGasLimit } = this.state this.setState({ gasPrice: originalGasPrice, gasLimit: originalGasLimit, }) } handleSave () { const { onSubmit, hideModal } = this.props const { gasLimit, gasPrice } = this.state const prefixedHexGasPrice = getPrefixedHexGasPrice(gasPrice) const prefixedHexGasLimit = getPrefixedHexGasLimit(gasLimit) Promise.resolve(onSubmit({ gasPrice: prefixedHexGasPrice, gasLimit: prefixedHexGasLimit })) .then(() => hideModal()) } validate () { const { gasLimit, gasPrice } = this.state return this.props.validate({ gasPrice: getPrefixedHexGasPrice(gasPrice), gasLimit: getPrefixedHexGasLimit(gasLimit), }) } render () { const { t } = this.context const { hideModal } = this.props const { gasPrice, gasLimit } = this.state const { valid, errorMessage } = this.validate() return (
{ this.context.t('customGas') }
hideModal()} />
this.setState({ gasPrice: value })} title={t('gasPrice')} copy={t('gasPriceCalculation')} /> this.setState({ gasLimit: value })} title={t('gasLimit')} copy={t('gasLimitCalculation')} />
{ !valid &&
{ errorMessage }
}
this.handleRevert()} > { t('revert') }
) } }