Implement select for filtering on open, closed, resolved, all.

This commit is contained in:
James Grugett 2022-05-09 11:31:56 -04:00
parent 5d38018a53
commit f964ecefb4
2 changed files with 54 additions and 43 deletions

View File

@ -31,11 +31,12 @@ const sortIndexes = [
{ label: 'Oldest', value: 'contracts-oldest' }, { label: 'Oldest', value: 'contracts-oldest' },
{ label: 'Most traded', value: 'contracts-most-traded' }, { label: 'Most traded', value: 'contracts-most-traded' },
{ label: '24h volume', value: 'contracts-24-hour-vol' }, { label: '24h volume', value: 'contracts-24-hour-vol' },
{ label: 'Closing soon', value: 'contracts-closing-soon' }, { label: 'Close date', value: 'contracts-close-date' },
{ label: 'Closed', value: 'contracts-closed' }, { label: 'Resolve date', value: 'contracts-resolve-date' },
{ label: 'Resolved', value: 'contracts-resolved' },
] ]
type filter = 'open' | 'closed' | 'resolved' | 'all'
export function ContractSearch(props: { export function ContractSearch(props: {
querySortOptions?: { querySortOptions?: {
defaultSort: Sort defaultSort: Sort
@ -56,22 +57,44 @@ export function ContractSearch(props: {
? initialSort ? initialSort
: querySortOptions?.defaultSort : querySortOptions?.defaultSort
console.log('sort', sort) const [filter, setFilter] = useState<filter>('open')
if (!sort) return <></> if (!sort) return <></>
return ( return (
<InstantSearch searchClient={searchClient} indexName={`contracts-${sort}`}> <InstantSearch searchClient={searchClient} indexName={`contracts-${sort}`}>
<Row className="gap-2"> <Row className="flex-wrap gap-2">
<SearchBox <SearchBox
className="flex-1" className="flex-1"
classNames={{ classNames={{
form: 'before:top-5', form: 'before:top-6',
input: 'pl-10 input input-bordered h-[40px]', input: '!pl-10 !input !input-bordered shadow-none',
resetIcon: 'mt-2',
}} }}
placeholder="Search markets" placeholder="Search markets"
/> />
<SortBy items={sortIndexes} /> <Row className="mt-2 gap-2 sm:mt-0">
<select
className="!select !select-bordered"
value={filter}
onChange={(e) => setFilter(e.target.value as filter)}
>
<option value="open">Open</option>
<option value="closed">Closed</option>
<option value="resolved">Resolved</option>
<option value="all">All</option>
</select>
<SortBy
items={sortIndexes}
classNames={{
select: '!select !select-bordered',
}}
/>
</Row> </Row>
<ContractSearchInner querySortOptions={querySortOptions} /> </Row>
<ContractSearchInner
querySortOptions={querySortOptions}
filter={filter}
/>
</InstantSearch> </InstantSearch>
) )
} }
@ -85,8 +108,9 @@ export function ContractSearchInner(props: {
} }
shouldLoadFromStorage?: boolean shouldLoadFromStorage?: boolean
} }
filter: filter
}) { }) {
const { querySortOptions } = props const { querySortOptions, filter } = props
const { initialQuery } = useInitialQueryAndSort(querySortOptions) const { initialQuery } = useInitialQueryAndSort(querySortOptions)
const { query, setQuery, setSort } = useUpdateQueryAndSort({ const { query, setQuery, setSort } = useUpdateQueryAndSort({
@ -121,17 +145,16 @@ export function ContractSearchInner(props: {
const tag = querySortOptions?.filter?.tag const tag = querySortOptions?.filter?.tag
useFilterTag(tag) useFilterTag(tag)
if ( useFilterClosed(
!creatorId || filter === 'closed'
index === 'contracts-closed' || ? true
index === 'contracts-resolved' : filter === 'all' || filter === 'resolved'
) { ? undefined
// eslint-disable-next-line react-hooks/rules-of-hooks : false
useFilterClosed(index) )
useFilterResolved(
// eslint-disable-next-line react-hooks/rules-of-hooks filter === 'resolved' ? true : filter === 'all' ? undefined : false
useFilterResolved(index) )
}
const { showMore, hits, isLastPage } = useInfiniteHits() const { showMore, hits, isLastPage } = useInfiniteHits()
const contracts = hits as any as Contract[] const contracts = hits as any as Contract[]
@ -169,30 +192,23 @@ const useFilterTag = (tag: string | undefined) => {
}, [tag, refine]) }, [tag, refine])
} }
const useFilterClosed = (index: string) => { const useFilterClosed = (value: boolean | undefined) => {
const [now] = useState(Date.now()) const [now] = useState(Date.now())
useRange({ useRange({
attribute: 'closeTime', attribute: 'closeTime',
min: min: value === false ? now : undefined,
index === 'contracts-resolved' || index === 'contracts-closed' ? 0 : now, max: value ? now : undefined,
max: index === 'contracts-closed' ? now : undefined,
}) })
} }
const useFilterResolved = (index: string) => { const useFilterResolved = (value: boolean | undefined) => {
// Note (James): I don't know why this works.
const { refine: refineResolved } = useToggleRefinement({ const { refine: refineResolved } = useToggleRefinement({
attribute: 'isResolved', attribute: value === undefined ? 'non-existant-field' : 'isResolved',
on: true, on: true,
off: false, off: value === undefined ? undefined : false,
}) })
useEffect(() => { useEffect(() => {
console.log( refineResolved({ isRefined: !value })
'effect', }, [value])
'curr',
index,
'update',
index === 'contracts-resolved'
)
refineResolved({ isRefined: index !== 'contracts-resolved' })
}, [index])
} }

View File

@ -13,12 +13,7 @@ export function ContractsGrid(props: {
hasMore: boolean hasMore: boolean
showCloseTime?: boolean showCloseTime?: boolean
}) { }) {
const { showCloseTime, hasMore, loadMore } = props const { contracts, showCloseTime, hasMore, loadMore } = props
const [resolvedContracts, activeContracts] = _.partition(
props.contracts,
(c) => c.isResolved
)
const contracts = [...activeContracts, ...resolvedContracts]
if (contracts.length === 0) { if (contracts.length === 0) {
return ( return (