1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-21 00:43:21 +01:00
metamask-extension/ui/app/pages/swaps/select-quote-popover/select-quote-popover.js

129 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-10-06 20:28:38 +02: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'
const SelectQuotePopover = ({
quoteDataRows = [],
onClose = null,
onSubmit = null,
swapToSymbol,
initialAggId,
onQuoteDetailsIsOpened,
}) => {
const t = useContext(I18nContext)
const [sortDirection, setSortDirection] = useState(1)
const [sortColumn, setSortColumn] = useState(null)
const [selectedAggId, setSelectedAggId] = useState(initialAggId)
const [contentView, setContentView] = useState('sortList')
const [viewingAgg, setViewingAgg] = useState(null)
const onSubmitClick = useCallback(() => {
onSubmit(selectedAggId)
onClose()
}, [selectedAggId, onClose, onSubmit])
const closeQuoteDetails = useCallback(() => {
setViewingAgg(null)
setContentView('sortList')
}, [])
2020-11-03 00:41:28 +01:00
const onRowClick = useCallback((aggId) => setSelectedAggId(aggId), [
setSelectedAggId,
])
2020-10-06 20:28:38 +02:00
2020-11-03 00:41:28 +01:00
const onCaretClick = useCallback(
(aggId) => {
const agg = quoteDataRows.find((quote) => quote.aggId === aggId)
setContentView('quoteDetails')
onQuoteDetailsIsOpened()
setViewingAgg(agg)
},
[quoteDataRows, onQuoteDetailsIsOpened],
)
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],
)
2020-10-06 20:28:38 +02:00
const footer = (
<>
<Button
type="default"
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
type="confirm"
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>
</>
)
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}
/>
)}
{contentView === 'quoteDetails' && viewingAgg && (
2020-11-03 00:41:28 +01:00
<QuoteDetails {...viewingAgg} />
2020-10-06 20:28:38 +02:00
)}
</Popover>
</div>
)
}
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,
}
export default SelectQuotePopover