2021-12-03 16:59:48 +01:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
import { useGasFeeContext } from '../../../../contexts/gasFee';
|
2021-12-06 19:47:26 +01:00
|
|
|
import { bnGreaterThan, bnLessThan } from '../../../../helpers/utils/util';
|
2021-12-03 16:59:48 +01:00
|
|
|
import { TYPOGRAPHY } from '../../../../helpers/constants/design-system';
|
|
|
|
import { useI18nContext } from '../../../../hooks/useI18nContext';
|
2021-12-06 19:47:26 +01:00
|
|
|
import { MAX_GAS_LIMIT_DEC } from '../../../../pages/send/send.constants';
|
2021-12-03 16:59:48 +01:00
|
|
|
import Button from '../../../ui/button';
|
|
|
|
import FormField from '../../../ui/form-field';
|
|
|
|
import Typography from '../../../ui/typography';
|
|
|
|
|
2021-12-06 19:47:26 +01:00
|
|
|
import { useAdvancedGasFeePopoverContext } from '../context';
|
|
|
|
|
|
|
|
const validateGasLimit = (gasLimit, minimumGasLimitDec) => {
|
|
|
|
return bnLessThan(gasLimit, minimumGasLimitDec) ||
|
|
|
|
bnGreaterThan(gasLimit, MAX_GAS_LIMIT_DEC)
|
|
|
|
? 'editGasLimitOutOfBoundsV2'
|
|
|
|
: null;
|
|
|
|
};
|
2021-12-03 16:59:48 +01:00
|
|
|
|
|
|
|
const AdvancedGasFeeGasLimit = () => {
|
|
|
|
const t = useI18nContext();
|
2022-07-31 20:26:40 +02:00
|
|
|
const { setGasLimit: setGasLimitInContext, setErrorValue } =
|
|
|
|
useAdvancedGasFeePopoverContext();
|
|
|
|
const { gasLimit: gasLimitInTransaction, minimumGasLimitDec } =
|
|
|
|
useGasFeeContext();
|
2021-12-03 16:59:48 +01:00
|
|
|
const [isEditing, setEditing] = useState(false);
|
|
|
|
const [gasLimit, setGasLimit] = useState(gasLimitInTransaction);
|
2021-12-06 19:47:26 +01:00
|
|
|
const [gasLimitError, setGasLimitError] = useState();
|
2021-12-03 16:59:48 +01:00
|
|
|
|
|
|
|
const updateGasLimit = (value) => {
|
|
|
|
setGasLimit(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setGasLimitInContext(gasLimit);
|
2021-12-06 19:47:26 +01:00
|
|
|
const error = validateGasLimit(gasLimit, minimumGasLimitDec);
|
|
|
|
setGasLimitError(error);
|
2022-02-11 15:29:17 +01:00
|
|
|
setErrorValue('gasLimit', error === 'editGasLimitOutOfBoundsV2');
|
|
|
|
}, [gasLimit, minimumGasLimitDec, setGasLimitInContext, setErrorValue]);
|
2021-12-03 16:59:48 +01:00
|
|
|
|
|
|
|
if (isEditing) {
|
|
|
|
return (
|
|
|
|
<FormField
|
2022-01-19 00:08:41 +01:00
|
|
|
dataTestId="gas-limit-input"
|
2021-12-06 19:47:26 +01:00
|
|
|
error={
|
|
|
|
gasLimitError
|
|
|
|
? t(gasLimitError, [minimumGasLimitDec - 1, MAX_GAS_LIMIT_DEC])
|
|
|
|
: ''
|
|
|
|
}
|
2021-12-03 16:59:48 +01:00
|
|
|
onChange={updateGasLimit}
|
|
|
|
titleText={t('gasLimitV2')}
|
|
|
|
value={gasLimit}
|
|
|
|
numeric
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2021-12-06 19:47:26 +01:00
|
|
|
<Typography
|
|
|
|
tag={TYPOGRAPHY.Paragraph}
|
|
|
|
variant={TYPOGRAPHY.H7}
|
|
|
|
className="advanced-gas-fee-gas-limit"
|
2022-07-20 22:47:51 +02:00
|
|
|
marginTop={4}
|
|
|
|
marginLeft={2}
|
|
|
|
marginRight={2}
|
2021-12-06 19:47:26 +01:00
|
|
|
>
|
2022-07-12 15:59:38 +02:00
|
|
|
<strong>{t('gasLimitV2')}</strong>
|
2021-12-06 19:47:26 +01:00
|
|
|
<span>{gasLimit}</span>
|
|
|
|
<Button
|
2022-01-19 00:08:41 +01:00
|
|
|
data-testid="advanced-gas-fee-edit"
|
2021-12-06 19:47:26 +01:00
|
|
|
className="advanced-gas-fee-gas-limit__edit-link"
|
|
|
|
onClick={() => setEditing(true)}
|
|
|
|
type="link"
|
2021-12-03 16:59:48 +01:00
|
|
|
>
|
2022-07-12 15:59:38 +02:00
|
|
|
{t('edit')}
|
2021-12-06 19:47:26 +01:00
|
|
|
</Button>
|
2021-12-03 16:59:48 +01:00
|
|
|
</Typography>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AdvancedGasFeeGasLimit;
|