From fe6531cf7874873cd7a8a3991528337bbdbe83dd Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Tue, 14 Dec 2021 00:57:27 -0800 Subject: [PATCH] Add a search bar and sort dropdown --- web/pages/markets.tsx | 53 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/web/pages/markets.tsx b/web/pages/markets.tsx index cbc6c934..ef9836ad 100644 --- a/web/pages/markets.tsx +++ b/web/pages/markets.tsx @@ -88,15 +88,60 @@ export default function Markets() { listAllContracts().then(setContracts) }, []) + const [query, setQuery] = useState('') + type Sort = 'createdTime' | 'volume' + const [sort, setSort] = useState('createdTime') + + function check(corpus: String) { + return corpus.toLowerCase().includes(query.toLowerCase()) + } + const matches = contracts.filter( + (c) => check(c.question) || check(c.description) || check(c.creatorName) + ) + + function volume(contract: Contract) { + return ( + contract.pot.YES + + contract.pot.NO - + contract.seedAmounts.YES - + contract.seedAmounts.NO + ) + } + + if (sort === 'createdTime') { + matches.sort((a, b) => b.createdTime - a.createdTime) + } else if (sort === 'volume') { + matches.sort((a, b) => volume(b) - volume(a)) + } + return (
- - <ContractsGrid contracts={contracts.filter((c) => !c.resolution)} /> + {/* Show a search input next to a sort dropdown */} + <div className="flex justify-between gap-2"> + <input + type="text" + value={query} + onChange={(e) => setQuery(e.target.value)} + placeholder="Search markets" + className="input input-bordered w-full" + /> + <select + className="select select-bordered" + value={sort} + onChange={(e) => setSort(e.target.value as Sort)} + > + <option value="createdTime">Newest first</option> + <option value="volume">Most traded</option> + </select> + </div> - <Title text="Resolved markets" className="mt-20" /> - <ContractsGrid contracts={contracts.filter((c) => c.resolution)} /> + <Title text="Open markets" className="mt-16" /> + <ContractsGrid contracts={matches.filter((c) => !c.resolution)} /> + + <Title text="Resolved markets" className="mt-16" /> + <ContractsGrid contracts={matches.filter((c) => c.resolution)} /> </div> </div> )