2021-02-04 19:15:23 +01:00
|
|
|
import React, { useState, useCallback, useContext } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { I18nContext } from '../../../contexts/i18n';
|
|
|
|
import Popover from '../../../components/ui/popover';
|
|
|
|
import Button from '../../../components/ui/button';
|
|
|
|
import QuoteDetails from './quote-details';
|
|
|
|
import SortList from './sort-list';
|
|
|
|
import { QUOTE_DATA_ROWS_PROPTYPES_SHAPE } from './select-quote-popover-constants';
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const SelectQuotePopover = ({
|
|
|
|
quoteDataRows = [],
|
|
|
|
onClose = null,
|
|
|
|
onSubmit = null,
|
|
|
|
swapToSymbol,
|
|
|
|
initialAggId,
|
|
|
|
onQuoteDetailsIsOpened,
|
2022-02-18 17:48:38 +01:00
|
|
|
hideEstimatedGasFee,
|
2020-10-06 20:28:38 +02:00
|
|
|
}) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const t = useContext(I18nContext);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const [sortDirection, setSortDirection] = useState(1);
|
|
|
|
const [sortColumn, setSortColumn] = useState(null);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const [selectedAggId, setSelectedAggId] = useState(initialAggId);
|
|
|
|
const [contentView, setContentView] = useState('sortList');
|
|
|
|
const [viewingAgg, setViewingAgg] = useState(null);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const onSubmitClick = useCallback(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
onSubmit(selectedAggId);
|
|
|
|
onClose();
|
|
|
|
}, [selectedAggId, onClose, onSubmit]);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const closeQuoteDetails = useCallback(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
setViewingAgg(null);
|
|
|
|
setContentView('sortList');
|
|
|
|
}, []);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const onRowClick = useCallback((aggId) => setSelectedAggId(aggId), [
|
|
|
|
setSelectedAggId,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const onCaretClick = useCallback(
|
|
|
|
(aggId) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const agg = quoteDataRows.find((quote) => quote.aggId === aggId);
|
|
|
|
setContentView('quoteDetails');
|
|
|
|
onQuoteDetailsIsOpened();
|
|
|
|
setViewingAgg(agg);
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
[quoteDataRows, onQuoteDetailsIsOpened],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const CustomBackground = useCallback(
|
|
|
|
() => (
|
|
|
|
<div className="select-quote-popover__popover-bg" onClick={onClose} />
|
|
|
|
),
|
|
|
|
[onClose],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
const footer = (
|
|
|
|
<>
|
|
|
|
<Button
|
2021-10-05 21:20:42 +02:00
|
|
|
type="secondary"
|
2020-10-06 20:28:38 +02:00
|
|
|
className="page-container__footer-button select-quote-popover__button"
|
|
|
|
onClick={onClose}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{t('close')}
|
2020-10-06 20:28:38 +02:00
|
|
|
</Button>
|
|
|
|
|
|
|
|
<Button
|
2021-10-05 21:20:42 +02:00
|
|
|
type="primary"
|
2020-10-06 20:28:38 +02:00
|
|
|
className="page-container__footer-button select-quote-popover__button"
|
|
|
|
onClick={onSubmitClick}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{t('swapSelect')}
|
2020-10-06 20:28:38 +02:00
|
|
|
</Button>
|
|
|
|
</>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="select-quote-popover">
|
|
|
|
<Popover
|
2020-11-03 00:41:28 +01:00
|
|
|
title={
|
|
|
|
contentView === 'quoteDetails'
|
|
|
|
? t('swapSelectAQuote')
|
|
|
|
: t('swapQuoteDetails')
|
|
|
|
}
|
|
|
|
subtitle={
|
|
|
|
contentView === 'sortList'
|
|
|
|
? t('swapSelectQuotePopoverDescription')
|
|
|
|
: null
|
|
|
|
}
|
2020-10-06 20:28:38 +02:00
|
|
|
onClose={onClose}
|
|
|
|
CustomBackground={CustomBackground}
|
|
|
|
className="select-quote-popover__popover-wrap"
|
|
|
|
footerClassName="swaps__footer"
|
|
|
|
footer={contentView === 'quoteDetails' ? null : footer}
|
|
|
|
onBack={contentView === 'quoteDetails' ? closeQuoteDetails : null}
|
|
|
|
>
|
|
|
|
{contentView === 'sortList' && (
|
|
|
|
<SortList
|
|
|
|
quoteDataRows={quoteDataRows}
|
|
|
|
selectedAggId={selectedAggId}
|
|
|
|
onSelect={onRowClick}
|
|
|
|
onCaretClick={onCaretClick}
|
|
|
|
swapToSymbol={swapToSymbol}
|
|
|
|
sortDirection={sortDirection}
|
|
|
|
setSortDirection={setSortDirection}
|
|
|
|
sortColumn={sortColumn}
|
|
|
|
setSortColumn={setSortColumn}
|
2022-02-18 17:48:38 +01:00
|
|
|
hideEstimatedGasFee={hideEstimatedGasFee}
|
2020-10-06 20:28:38 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{contentView === 'quoteDetails' && viewingAgg && (
|
2022-02-18 17:48:38 +01:00
|
|
|
<QuoteDetails
|
|
|
|
{...viewingAgg}
|
|
|
|
hideEstimatedGasFee={hideEstimatedGasFee}
|
|
|
|
/>
|
2020-10-06 20:28:38 +02:00
|
|
|
)}
|
|
|
|
</Popover>
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
SelectQuotePopover.propTypes = {
|
|
|
|
onClose: PropTypes.func,
|
|
|
|
onSubmit: PropTypes.func,
|
|
|
|
swapToSymbol: PropTypes.string,
|
|
|
|
renderableData: PropTypes.array,
|
|
|
|
quoteDataRows: PropTypes.arrayOf(QUOTE_DATA_ROWS_PROPTYPES_SHAPE),
|
|
|
|
initialAggId: PropTypes.string,
|
|
|
|
onQuoteDetailsIsOpened: PropTypes.func,
|
2022-02-18 17:48:38 +01:00
|
|
|
hideEstimatedGasFee: PropTypes.bool.isRequired,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export default SelectQuotePopover;
|