From 0c404bd3b101793e16b7bce3fee5752f8cc0df16 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 23 Dec 2023 06:42:46 -0500 Subject: [PATCH] feat: implement site filter --- app/components/SearchBar.tsx | 60 +++++++++++++++++--------------- app/components/SitesCombobox.tsx | 16 +++++++-- app/routes/api.search.ts | 6 +++- 3 files changed, 49 insertions(+), 33 deletions(-) diff --git a/app/components/SearchBar.tsx b/app/components/SearchBar.tsx index 05d1ff5..e039751 100644 --- a/app/components/SearchBar.tsx +++ b/app/components/SearchBar.tsx @@ -7,21 +7,14 @@ import React, { useRef, useState, } from "react"; -import { ChevronDownIcon, ChevronRightIcon } from "@heroicons/react/24/outline"; // Assuming usage of Heroicons for icons +import { ChevronRightIcon } from "@heroicons/react/24/outline"; // Assuming usage of Heroicons for icons import { flushSync } from "react-dom"; -import { - Link, - useFetcher, - useLocation, - useNavigate, - useSearchParams, -} from "@remix-run/react"; -import { FILTER_TIMES, formatDate, getResults } from "@/utils"; +import { Link, useFetcher, useSearchParams } from "@remix-run/react"; +import { FILTER_TIMES, formatDate } from "@/utils"; import { Select, SelectContent, SelectItem, - SelectLabel, SelectTrigger, SelectValue, } from "./ui/select"; @@ -31,8 +24,6 @@ import { SearchResult, SiteList } from "@/types.js"; type Props = {}; const SearchBar = ({ sites }: { sites: SiteList }) => { - let navigate = useNavigate(); - let { pathname } = useLocation(); let [searchParams] = useSearchParams(); const [query, setQuery] = useState(searchParams.get("q") ?? ""); const inputRef = useRef(); @@ -40,29 +31,36 @@ const SearchBar = ({ sites }: { sites: SiteList }) => { const [activeInput, setActiveInput] = useState(true); const [dirtyInput, setDirtyInput] = useState(false); const [results, setResults] = useState([]); + const [selectedSite, setSelectedSite] = useState(null); - const fetcher = useFetcher(); + const fetcher = useFetcher({ key: "seach" }); const handleSearch = useCallback( - async (event: FormEvent) => { + (event: FormEvent) => { event.preventDefault(); - setIsLoading(true); - let newSearchParams = new URLSearchParams(searchParams.toString()); - - if (query) { - newSearchParams.set("q", query); - } else { - newSearchParams.delete("q"); - return; - } - - navigate(`${pathname}?${newSearchParams.toString()}`); - - fetcher.load(`/api/search?${newSearchParams}`); + doSearch(); }, - [query, searchParams, navigate, pathname] + [query, selectedSite] ); + function doSearch() { + setIsLoading(true); + let newSearchParams = new URLSearchParams(searchParams.toString()); + + if (query) { + newSearchParams.set("q", query); + } else { + newSearchParams.delete("q"); + return; + } + + if (selectedSite) { + newSearchParams.set("site", selectedSite); + } + + fetcher.load(`/api/search?${newSearchParams}`); + } + useEffect(() => { if (fetcher.data) { setResults(fetcher.data as SearchResult[]); @@ -71,6 +69,10 @@ const SearchBar = ({ sites }: { sites: SiteList }) => { } }, [fetcher.data]); + useEffect(() => { + doSearch(); + }, [selectedSite]); + const isActive = results.length > 0 || dirtyInput; return ( @@ -155,7 +157,7 @@ const SearchBar = ({ sites }: { sites: SiteList }) => { ) : (
{/* Dropdown component should be here */} - + {/* Dropdown component should be here */}