From 446131bf471dc1c468cc2bb9f8e36a9603732a3f Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 23 Dec 2023 07:28:52 -0500 Subject: [PATCH] feat: implement filtering in search page --- app/components/SimplifiedSearchBar.tsx | 36 +++++++++++++++++++++----- app/routes/search.tsx | 4 ++- app/utils.ts | 17 +++++++++++- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/app/components/SimplifiedSearchBar.tsx b/app/components/SimplifiedSearchBar.tsx index bfd9888..aea4ae4 100644 --- a/app/components/SimplifiedSearchBar.tsx +++ b/app/components/SimplifiedSearchBar.tsx @@ -1,4 +1,4 @@ -import React, { FormEvent, useState } from "react"; +import React, { FormEvent, useCallback, useEffect, useState } from "react"; import { useLocation, useNavigate } from "@remix-run/react"; import { Select, @@ -31,19 +31,43 @@ const SimplifiedSearchBar = ({ let navigate = useNavigate(); let location = useLocation(); const [value, setValue] = useState(initialValue); + const [selectedSite, setSelectedSite] = useState(null); + const [selectedTime, setSelectedTime] = useState(null); - const handleSearch = (event: FormEvent) => { - event.preventDefault(); + const handleSearch = useCallback( + (event: FormEvent) => { + event.preventDefault(); + doSearch(); + }, + [value] + ); + + function doSearch() { const newSearchParams = new URLSearchParams(location.search); if (value) { newSearchParams.set("q", value); } else { newSearchParams.delete("q"); + return; + } + + if (selectedSite) { + newSearchParams.set("site", selectedSite); + } + + if (selectedTime) { + const timestampInMilliseconds = Date.parse(selectedTime); // Date.parse returns the timestamp in milliseconds + const timestamp = timestampInMilliseconds / 1000; + newSearchParams.set("time", timestamp.toString()); } navigate(`${location.pathname}?${newSearchParams}`); - }; + } + + useEffect(() => { + doSearch(); + }, [selectedSite, selectedTime]); return (
{/* Dropdown component should be here */} - + {/* Dropdown component should be here */} - setSelectedTime(v)}> diff --git a/app/routes/search.tsx b/app/routes/search.tsx index ff69827..eac504c 100644 --- a/app/routes/search.tsx +++ b/app/routes/search.tsx @@ -16,7 +16,9 @@ export let loader: LoaderFunction = async ({ request }) => { const sites = getAvailableSites(); const search = new URL(request.url).searchParams; const query = search.get("q") ?? ""; - const results = await getResults({ query: query }); + const site = search.get("site") ?? undefined; + const time = search.get("time") ?? undefined; + const results = await getResults({ query: query, site, time }); // Return the fetched data as JSON return json({ sites, results, query }); diff --git a/app/utils.ts b/app/utils.ts index 9fccc98..853a6f0 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -23,14 +23,29 @@ export const formatDate = (date: string | Date) => { export async function getResults({ query, + site, + time, }: { query?: string; + site?: string; + time?: string; }): Promise { if (!query) { return []; } - const results = await search.index("articles").search(query); + let filters = []; + + if (site) { + filters.push(`siteKey = ${site}`); + } + if (time) { + filters.push(`createdTimestamp >= ${parseInt(time).toString()}`); + } + + const results = await search.index("articles").search(query, { + filter: filters.length ? filters.join(" AND ") : undefined, + }); return results.hits.map((item) => { return {