Refactor useQueryAndSortParams into the SearchableGrid component

This commit is contained in:
Austin Chen 2022-03-29 08:09:46 -07:00
parent 419a3106e8
commit a40d593d32
4 changed files with 15 additions and 44 deletions

View File

@ -199,13 +199,16 @@ const MAX_CONTRACTS_DISPLAYED = 99
export function SearchableGrid(props: { export function SearchableGrid(props: {
contracts: Contract[] contracts: Contract[]
query: string
setQuery: (query: string) => void
sort: Sort
setSort: (sort: Sort) => void
byOneCreator?: boolean byOneCreator?: boolean
querySortOptions?: {
defaultSort: Sort
shouldLoadFromStorage?: boolean
}
}) { }) {
const { contracts, query, setQuery, sort, setSort, byOneCreator } = props const { contracts, byOneCreator, querySortOptions } = props
const { query, setQuery, sort, setSort } =
useQueryAndSortParams(querySortOptions)
const queryWords = query.toLowerCase().split(' ') const queryWords = query.toLowerCase().split(' ')
function check(corpus: String) { function check(corpus: String) {
@ -324,11 +327,6 @@ export function CreatorContractsList(props: { creator: User }) {
const { creator } = props const { creator } = props
const [contracts, setContracts] = useState<Contract[] | 'loading'>('loading') const [contracts, setContracts] = useState<Contract[] | 'loading'>('loading')
const { query, setQuery, sort, setSort } = useQueryAndSortParams({
defaultSort: 'all',
shouldLoadFromStorage: false,
})
useEffect(() => { useEffect(() => {
if (creator?.id) { if (creator?.id) {
// TODO: stream changes from firestore // TODO: stream changes from firestore
@ -342,10 +340,10 @@ export function CreatorContractsList(props: { creator: User }) {
<SearchableGrid <SearchableGrid
contracts={contracts} contracts={contracts}
byOneCreator byOneCreator
query={query} querySortOptions={{
setQuery={setQuery} defaultSort: 'all',
sort={sort} shouldLoadFromStorage: false,
setSort={setSort} }}
/> />
) )
} }

View File

@ -22,7 +22,6 @@ import { Col } from '../../../components/layout/col'
import { useUser } from '../../../hooks/use-user' import { useUser } from '../../../hooks/use-user'
import { useFold } from '../../../hooks/use-fold' import { useFold } from '../../../hooks/use-fold'
import { SearchableGrid } from '../../../components/contracts-list' import { SearchableGrid } from '../../../components/contracts-list'
import { useQueryAndSortParams } from '../../../hooks/use-sort-and-query-params'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import clsx from 'clsx' import clsx from 'clsx'
import { scoreCreators, scoreTraders } from '../../../../common/scoring' import { scoreCreators, scoreTraders } from '../../../../common/scoring'
@ -138,8 +137,6 @@ export default function FoldPage(props: {
const fold = useFold(props.fold?.id) ?? props.fold const fold = useFold(props.fold?.id) ?? props.fold
const { query, setQuery, sort, setSort } = useQueryAndSortParams()
const user = useUser() const user = useUser()
const isCurator = user && fold && user.id === fold.curatorId const isCurator = user && fold && user.id === fold.curatorId
@ -259,13 +256,7 @@ export default function FoldPage(props: {
<LoadingIndicator className="mt-4" /> <LoadingIndicator className="mt-4" />
) )
) : ( ) : (
<SearchableGrid <SearchableGrid contracts={contracts} />
contracts={contracts}
query={query}
setQuery={setQuery}
sort={sort}
setSort={setSort}
/>
)} )}
</Col> </Col>
<Col className="hidden w-full max-w-xs gap-12 md:flex"> <Col className="hidden w-full max-w-xs gap-12 md:flex">

View File

@ -3,7 +3,6 @@ import { Page } from '../components/page'
import { SEO } from '../components/SEO' import { SEO } from '../components/SEO'
import { Title } from '../components/title' import { Title } from '../components/title'
import { useContracts } from '../hooks/use-contracts' import { useContracts } from '../hooks/use-contracts'
import { useQueryAndSortParams } from '../hooks/use-sort-and-query-params'
import { Contract, listAllContracts } from '../lib/firebase/contracts' import { Contract, listAllContracts } from '../lib/firebase/contracts'
export async function getStaticProps() { export async function getStaticProps() {
@ -21,8 +20,6 @@ export async function getStaticProps() {
export default function Markets(props: { contracts: Contract[] }) { export default function Markets(props: { contracts: Contract[] }) {
const contracts = useContracts() ?? props.contracts ?? [] const contracts = useContracts() ?? props.contracts ?? []
const { query, setQuery, sort, setSort } = useQueryAndSortParams()
return ( return (
<Page> <Page>
<SEO <SEO
@ -35,13 +32,7 @@ export default function Markets(props: { contracts: Contract[] }) {
<ClosingSoonMarkets contracts={closingSoonContracts} /> <ClosingSoonMarkets contracts={closingSoonContracts} />
<Spacer h={10} /> */} <Spacer h={10} /> */}
<SearchableGrid <SearchableGrid contracts={contracts} />
contracts={contracts}
query={query}
setQuery={setQuery}
sort={sort}
setSort={setSort}
/>
</Page> </Page>
) )
} }

View File

@ -3,7 +3,6 @@ import { SearchableGrid } from '../../components/contracts-list'
import { Page } from '../../components/page' import { Page } from '../../components/page'
import { Title } from '../../components/title' import { Title } from '../../components/title'
import { useContracts } from '../../hooks/use-contracts' import { useContracts } from '../../hooks/use-contracts'
import { useQueryAndSortParams } from '../../hooks/use-sort-and-query-params'
import { Contract, listAllContracts } from '../../lib/firebase/contracts' import { Contract, listAllContracts } from '../../lib/firebase/contracts'
export async function getStaticProps() { export async function getStaticProps() {
@ -31,18 +30,10 @@ export default function TagPage(props: { contracts: Contract[] }) {
contract.lowercaseTags.includes(tag.toLowerCase()) contract.lowercaseTags.includes(tag.toLowerCase())
) )
const { query, setQuery, sort, setSort } = useQueryAndSortParams()
return ( return (
<Page> <Page>
<Title text={`#${tag}`} /> <Title text={`#${tag}`} />
<SearchableGrid <SearchableGrid contracts={taggedContracts} />
contracts={taggedContracts}
query={query}
setQuery={setQuery}
sort={sort}
setSort={setSort}
/>
</Page> </Page>
) )
} }