From f964ecefb4ff8e9b0935147ad6bb607065ad3570 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Mon, 9 May 2022 11:31:56 -0400 Subject: [PATCH] Implement select for filtering on open, closed, resolved, all. --- web/components/contract-search.tsx | 90 +++++++++++++--------- web/components/contract/contracts-list.tsx | 7 +- 2 files changed, 54 insertions(+), 43 deletions(-) diff --git a/web/components/contract-search.tsx b/web/components/contract-search.tsx index ac6b31e1..9451b0d3 100644 --- a/web/components/contract-search.tsx +++ b/web/components/contract-search.tsx @@ -31,11 +31,12 @@ const sortIndexes = [ { label: 'Oldest', value: 'contracts-oldest' }, { label: 'Most traded', value: 'contracts-most-traded' }, { label: '24h volume', value: 'contracts-24-hour-vol' }, - { label: 'Closing soon', value: 'contracts-closing-soon' }, - { label: 'Closed', value: 'contracts-closed' }, - { label: 'Resolved', value: 'contracts-resolved' }, + { label: 'Close date', value: 'contracts-close-date' }, + { label: 'Resolve date', value: 'contracts-resolve-date' }, ] +type filter = 'open' | 'closed' | 'resolved' | 'all' + export function ContractSearch(props: { querySortOptions?: { defaultSort: Sort @@ -56,22 +57,44 @@ export function ContractSearch(props: { ? initialSort : querySortOptions?.defaultSort - console.log('sort', sort) + const [filter, setFilter] = useState('open') + if (!sort) return <> return ( - + - + + + + - + ) } @@ -85,8 +108,9 @@ export function ContractSearchInner(props: { } shouldLoadFromStorage?: boolean } + filter: filter }) { - const { querySortOptions } = props + const { querySortOptions, filter } = props const { initialQuery } = useInitialQueryAndSort(querySortOptions) const { query, setQuery, setSort } = useUpdateQueryAndSort({ @@ -121,17 +145,16 @@ export function ContractSearchInner(props: { const tag = querySortOptions?.filter?.tag useFilterTag(tag) - if ( - !creatorId || - index === 'contracts-closed' || - index === 'contracts-resolved' - ) { - // eslint-disable-next-line react-hooks/rules-of-hooks - useFilterClosed(index) - - // eslint-disable-next-line react-hooks/rules-of-hooks - useFilterResolved(index) - } + useFilterClosed( + filter === 'closed' + ? true + : filter === 'all' || filter === 'resolved' + ? undefined + : false + ) + useFilterResolved( + filter === 'resolved' ? true : filter === 'all' ? undefined : false + ) const { showMore, hits, isLastPage } = useInfiniteHits() const contracts = hits as any as Contract[] @@ -169,30 +192,23 @@ const useFilterTag = (tag: string | undefined) => { }, [tag, refine]) } -const useFilterClosed = (index: string) => { +const useFilterClosed = (value: boolean | undefined) => { const [now] = useState(Date.now()) useRange({ attribute: 'closeTime', - min: - index === 'contracts-resolved' || index === 'contracts-closed' ? 0 : now, - max: index === 'contracts-closed' ? now : undefined, + min: value === false ? now : undefined, + max: value ? now : undefined, }) } -const useFilterResolved = (index: string) => { +const useFilterResolved = (value: boolean | undefined) => { + // Note (James): I don't know why this works. const { refine: refineResolved } = useToggleRefinement({ - attribute: 'isResolved', + attribute: value === undefined ? 'non-existant-field' : 'isResolved', on: true, - off: false, + off: value === undefined ? undefined : false, }) useEffect(() => { - console.log( - 'effect', - 'curr', - index, - 'update', - index === 'contracts-resolved' - ) - refineResolved({ isRefined: index !== 'contracts-resolved' }) - }, [index]) + refineResolved({ isRefined: !value }) + }, [value]) } diff --git a/web/components/contract/contracts-list.tsx b/web/components/contract/contracts-list.tsx index dbbb9a10..705de807 100644 --- a/web/components/contract/contracts-list.tsx +++ b/web/components/contract/contracts-list.tsx @@ -13,12 +13,7 @@ export function ContractsGrid(props: { hasMore: boolean showCloseTime?: boolean }) { - const { showCloseTime, hasMore, loadMore } = props - const [resolvedContracts, activeContracts] = _.partition( - props.contracts, - (c) => c.isResolved - ) - const contracts = [...activeContracts, ...resolvedContracts] + const { contracts, showCloseTime, hasMore, loadMore } = props if (contracts.length === 0) { return (