From bafad57e9b9be5394b5dca3f9c3a0578cd12a6aa Mon Sep 17 00:00:00 2001 From: Allen Ziegenfus Date: Mon, 29 May 2023 16:27:19 -0500 Subject: [PATCH] Add search result navigation --- src/components/SearchBar.jsx | 76 ++++++++++++++++++++--------- src/components/SearchNavigation.jsx | 21 ++++++++ src/layouts/Layout.astro | 2 +- src/pages/[slug]/[workSlug].astro | 3 ++ 4 files changed, 77 insertions(+), 25 deletions(-) create mode 100644 src/components/SearchNavigation.jsx diff --git a/src/components/SearchBar.jsx b/src/components/SearchBar.jsx index 007e4fc..8358434 100644 --- a/src/components/SearchBar.jsx +++ b/src/components/SearchBar.jsx @@ -2,20 +2,49 @@ import { React, useEffect, useState } from "react"; import Fuse from "fuse.js"; export default function SearchBar({}) { - const [searchInput, setSearchInput] = useState(); + const urlParams = new URLSearchParams(window.location.search); + const search = urlParams.get("search"); + + const [searchInput, setSearchInput] = useState(search || ""); const [displayRows, setDisplayRows] = useState([]); - const [records, setRecords] = useState([]) + const [records, setRecords] = useState([]); const [open, setOpen] = useState(false); - useEffect(async () => { - try { - const recordsresponse = await fetch("/records.json") - setRecords(await recordsresponse.json()); + function openSearch(e) { + setOpen(true); + } + useEffect(() => { + window.addEventListener("openSearch", openSearch); + return () => window.removeEventListener("openSearch", openSearch); + }, []); + + useEffect(() => { + function performSearch() { + const searchResults = fuse.search(searchInput); + if (searchResults) { + setDisplayRows( + searchResults.slice(0, 8).map((searchResult) => ({ + Titel: searchResult.item.Titel, + Slug: `/${searchResult.item.WerkgruppeSlug}/${searchResult.item.Slug}`, + Thumbnail: searchResult.item.Thumbnail, + InvNr: searchResult.item.InvNr, + })) + ); + } } - catch (error) { - console.log(error); + performSearch(); + }, [searchInput, records]); + useEffect(() => { + async function fetchRecords() { + try { + const recordsresponse = await fetch("/records.json"); + setRecords(await recordsresponse.json()); + } catch (error) { + console.log(error); + } } - }, {}); + fetchRecords(); + }, []); const fuse = new Fuse(records, { threshold: 0.4, @@ -31,7 +60,14 @@ export default function SearchBar({}) { }); if (!open) { - return + return ( + + ); } else { return (
@@ -39,23 +75,12 @@ export default function SearchBar({}) { { const searchTerm = e.target.value; setSearchInput(searchTerm); if (searchTerm) { - const searchResults = fuse.search(searchTerm); - console.log(searchResults); - setSearchInput(searchTerm); - if (searchResults) { - setDisplayRows( - searchResults.slice(0, 8).map((searchResult) => ({ - Titel: searchResult.item.Titel, - Slug: `/${searchResult.item.WerkgruppeSlug}/${searchResult.item.Slug}`, - Thumbnail: searchResult.item.Thumbnail, - InvNr: searchResult.item.InvNr, - })) - ); - } + performSearch(); } else { setDisplayRows([]); setSearchInput(""); @@ -71,7 +96,10 @@ export default function SearchBar({}) { {displayRows.length > 1 && displayRows.map((row) => (
  • - + {row.Titel}

    {row.Titel}

    {row.InvNr}

    diff --git a/src/components/SearchNavigation.jsx b/src/components/SearchNavigation.jsx new file mode 100644 index 0000000..4bd961a --- /dev/null +++ b/src/components/SearchNavigation.jsx @@ -0,0 +1,21 @@ +import { React, useEffect, useState } from "react"; + +function setSearchState(search) { + const event = new Event("openSearch"); + window.dispatchEvent(event); +} + +export default function SearchNavigation({}) { + const urlParams = new URLSearchParams(window.location.search); + + const search = urlParams.get("search"); + return ( + search && ( +
    + ) + ); +} diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro index 25bf724..106a02d 100644 --- a/src/layouts/Layout.astro +++ b/src/layouts/Layout.astro @@ -26,7 +26,7 @@ const { title } = Astro.props; Werkverzeichnis von Vollrad Kutscher 1968 bis 2000 - +