From 917511a44912c1beb913248e1baca854ea03f775 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 23 Dec 2023 07:10:18 -0500 Subject: [PATCH] feat: implement time filter --- app/components/SearchBar.tsx | 14 ++++++++++++-- app/lib/prisma.ts | 12 +++++++++++- app/routes/api.search.ts | 12 +++++++++++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/app/components/SearchBar.tsx b/app/components/SearchBar.tsx index e039751..20886de 100644 --- a/app/components/SearchBar.tsx +++ b/app/components/SearchBar.tsx @@ -32,6 +32,7 @@ const SearchBar = ({ sites }: { sites: SiteList }) => { const [dirtyInput, setDirtyInput] = useState(false); const [results, setResults] = useState([]); const [selectedSite, setSelectedSite] = useState(null); + const [selectedTime, setSelectedTime] = useState(null); const fetcher = useFetcher({ key: "seach" }); @@ -58,6 +59,12 @@ const SearchBar = ({ sites }: { sites: SiteList }) => { 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()); + } + fetcher.load(`/api/search?${newSearchParams}`); } @@ -71,7 +78,7 @@ const SearchBar = ({ sites }: { sites: SiteList }) => { useEffect(() => { doSearch(); - }, [selectedSite]); + }, [selectedSite, selectedTime]); const isActive = results.length > 0 || dirtyInput; @@ -159,7 +166,10 @@ const SearchBar = ({ sites }: { sites: SiteList }) => { {/* Dropdown component should be here */} {/* Dropdown component should be here */} - setSelectedTime(v)} + > diff --git a/app/lib/prisma.ts b/app/lib/prisma.ts index f4815cf..9a67c19 100644 --- a/app/lib/prisma.ts +++ b/app/lib/prisma.ts @@ -12,7 +12,17 @@ export const prisma = (globalForPrisma.prisma || new PrismaClient()).$extends({ // Index the created record in MeiliSearch const index = search.index("articles"); - await index.addDocuments([createdRecord]); + const timestampInMilliseconds = Date.parse( + createdRecord.createdAt as any + ); // Date.parse returns the timestamp in milliseconds + const timestamp = timestampInMilliseconds / 1000; + await index.addDocuments([ + { + ...createdRecord, + + createdTimestamp: timestamp, + }, + ]); return createdRecord; }, diff --git a/app/routes/api.search.ts b/app/routes/api.search.ts index f875b6c..2f6f567 100644 --- a/app/routes/api.search.ts +++ b/app/routes/api.search.ts @@ -13,9 +13,19 @@ export async function loader({ request }: LoaderFunctionArgs) { } const site = searchParams.get("site"); + const time = searchParams.get("time"); + + 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: site ? `siteKey = ${site}` : undefined, + filter: filters.length ? filters.join(" AND ") : undefined, }); return results.hits.map((item) => {