2021-06-28 16:45:08 +02:00
|
|
|
import React, { useCallback, useContext, useState } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import Popover from '../../ui/popover';
|
|
|
|
import Button from '../../ui/button';
|
|
|
|
import EditGasDisplay from '../edit-gas-display';
|
|
|
|
import EditGasDisplayEducation from '../edit-gas-display-education';
|
|
|
|
|
|
|
|
import { I18nContext } from '../../../contexts/i18n';
|
2021-07-12 21:35:54 +02:00
|
|
|
import {
|
|
|
|
createCancelTransaction,
|
|
|
|
createSpeedUpTransaction,
|
|
|
|
hideModal,
|
|
|
|
hideSidebar,
|
|
|
|
updateTransaction,
|
|
|
|
} from '../../../store/actions';
|
|
|
|
|
|
|
|
export const EDIT_GAS_MODE = {
|
|
|
|
SPEED_UP: 'speed-up',
|
|
|
|
CANCEL: 'cancel',
|
|
|
|
MODIFY_IN_PLACE: 'modify-in-place',
|
|
|
|
};
|
2021-06-28 16:45:08 +02:00
|
|
|
|
|
|
|
export default function EditGasPopover({
|
|
|
|
popoverTitle,
|
|
|
|
confirmButtonText,
|
|
|
|
editGasDisplayProps,
|
2021-07-12 21:35:54 +02:00
|
|
|
transaction,
|
|
|
|
mode,
|
2021-07-08 22:23:00 +02:00
|
|
|
onClose,
|
2021-06-28 16:45:08 +02:00
|
|
|
}) {
|
|
|
|
const t = useContext(I18nContext);
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const showSidebar = useSelector((state) => state.appState.sidebar.isOpen);
|
|
|
|
const [showEducationContent, setShowEducationContent] = useState(false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Temporary placeholder, this should be managed by the parent component but
|
|
|
|
* we will be extracting this component from the hard to maintain modal/
|
|
|
|
* sidebar component. For now this is just to be able to appropriately close
|
|
|
|
* the modal in testing
|
|
|
|
*/
|
|
|
|
const closePopover = useCallback(() => {
|
2021-07-08 22:23:00 +02:00
|
|
|
if (onClose) {
|
|
|
|
onClose();
|
|
|
|
} else if (showSidebar) {
|
2021-06-28 16:45:08 +02:00
|
|
|
dispatch(hideSidebar());
|
|
|
|
} else {
|
|
|
|
dispatch(hideModal());
|
|
|
|
}
|
2021-07-08 22:23:00 +02:00
|
|
|
}, [showSidebar, onClose, dispatch]);
|
2021-06-28 16:45:08 +02:00
|
|
|
|
2021-07-12 21:35:54 +02:00
|
|
|
const onSubmit = useCallback(() => {
|
|
|
|
if (!transaction || !mode) {
|
|
|
|
closePopover();
|
|
|
|
}
|
|
|
|
switch (mode) {
|
|
|
|
case EDIT_GAS_MODE.CANCEL:
|
|
|
|
dispatch(
|
|
|
|
createCancelTransaction(transaction.id, {
|
|
|
|
/** new gas settings */
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case EDIT_GAS_MODE.SPEED_UP:
|
|
|
|
dispatch(
|
|
|
|
createSpeedUpTransaction(transaction.id, {
|
|
|
|
/** new gas settings */
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case EDIT_GAS_MODE.MODIFY_IN_PLACE:
|
|
|
|
dispatch(
|
|
|
|
updateTransaction({
|
|
|
|
...transaction,
|
|
|
|
txParams: { ...transaction.txParams /** ...newGasSettings */ },
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
closePopover();
|
|
|
|
}, [transaction, mode, dispatch, closePopover]);
|
|
|
|
|
2021-06-28 16:45:08 +02:00
|
|
|
const title = showEducationContent
|
|
|
|
? t('editGasEducationModalTitle')
|
|
|
|
: popoverTitle || t('editGasTitle');
|
|
|
|
const footerButtonText = confirmButtonText || t('save');
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Popover
|
|
|
|
title={title}
|
|
|
|
onClose={closePopover}
|
|
|
|
onBack={
|
|
|
|
showEducationContent ? () => setShowEducationContent(false) : undefined
|
|
|
|
}
|
|
|
|
footer={
|
|
|
|
<>
|
2021-07-12 21:35:54 +02:00
|
|
|
<Button type="primary" onClick={onSubmit}>
|
2021-06-28 16:45:08 +02:00
|
|
|
{footerButtonText}
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<div style={{ padding: '0 20px 20px 20px' }}>
|
|
|
|
{showEducationContent ? (
|
|
|
|
<EditGasDisplayEducation />
|
|
|
|
) : (
|
|
|
|
<EditGasDisplay
|
|
|
|
{...editGasDisplayProps}
|
|
|
|
onEducationClick={() => setShowEducationContent(true)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Popover>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
EditGasPopover.propTypes = {
|
|
|
|
popoverTitle: PropTypes.string,
|
|
|
|
editGasDisplayProps: PropTypes.object,
|
|
|
|
confirmButtonText: PropTypes.string,
|
|
|
|
showEducationButton: PropTypes.bool,
|
2021-07-08 22:23:00 +02:00
|
|
|
onClose: PropTypes.func,
|
2021-07-12 21:35:54 +02:00
|
|
|
transaction: PropTypes.object,
|
|
|
|
mode: PropTypes.oneOf(Object.values(EDIT_GAS_MODE)),
|
2021-06-28 16:45:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
EditGasPopover.defaultProps = {
|
|
|
|
popoverTitle: '',
|
|
|
|
editGasDisplayProps: {},
|
|
|
|
confirmButtonText: '',
|
|
|
|
showEducationButton: false,
|
|
|
|
};
|