From 50bb6d7845cebc50e7430403f59089c6dab3a728 Mon Sep 17 00:00:00 2001 From: Helene Lotsberg Date: Thu, 5 Nov 2026 19:21:48 +0100 Subject: [PATCH 1/6] persist Explore state in URL and update test --- .../src/pages/explorePlants/ExplorePlants.tsx | 79 ++++++++---- frontend/src/tests/ExplorePage.test.tsx | 112 ++++++++++++------ 2 files changed, 130 insertions(+), 61 deletions(-) diff --git a/frontend/src/pages/explorePlants/ExplorePlants.tsx b/frontend/src/pages/explorePlants/ExplorePlants.tsx index 9690cd79..e46eac5b 100644 --- a/frontend/src/pages/explorePlants/ExplorePlants.tsx +++ b/frontend/src/pages/explorePlants/ExplorePlants.tsx @@ -3,8 +3,8 @@ import { BiSortAlt2 } from "react-icons/bi"; import { FaSearch } from "react-icons/fa"; import { useTranslation } from "react-i18next"; import { useQuery } from "@apollo/client/react"; +import { useSearchParams } from "react-router-dom"; import { useDebounce } from "../../hooks/useDebounce"; -import { useSessionState } from "../../hooks/useSessionState"; import { EXPLORE_PLANTS, type ExplorePlantsData, @@ -19,21 +19,42 @@ const SORT_OPTIONS = ["name_asc", "name_desc", "watering_asc", "careLevel_asc"]; export default function ExplorePlants() { const { t } = useTranslation(); + const [searchParams, setSearchParams] = useSearchParams(); // Matches come from the user in the database, so every page sees the same list const { matches, addMatch } = useMatchedPlants(); - const [search, setSearch] = useSessionState("ExplorePlants.search"); + const search = searchParams.get("search") ?? ""; const debouncedSearch = useDebounce(search, 800); - const [watering, setWatering] = useSessionState("ExplorePlants.watering"); - const [sunlight, setSunlight] = useSessionState("ExplorePlants.sunlight"); - const [careLevel, setCareLevel] = useSessionState("ExplorePlants.careLevel"); - const [sort, setSort] = useSessionState("ExplorePlants.sort"); - const [page, setPage] = useState(1); + const watering = searchParams.get("watering") ?? ""; + const sunlight = searchParams.get("sunlight") ?? ""; + const careLevel = searchParams.get("careLevel") ?? ""; + const sort = searchParams.get("sort") ?? ""; + const parsedPage = Number.parseInt(searchParams.get("page") ?? "", 10); + const page = Number.isInteger(parsedPage) && parsedPage > 0 ? parsedPage : 1; const [sortOpen, setSortOpen] = useState(false); const sortRef = useRef(null); const currentSort = sort || "name_asc"; + const updateExploreParams = (updates: Record) => { + setSearchParams( + (currentParams) => { + const nextParams = new URLSearchParams(currentParams); + + Object.entries(updates).forEach(([key, value]) => { + if (value) { + nextParams.set(key, value); + } else { + nextParams.delete(key); + } + }); + + return nextParams; + }, + { replace: true }, + ); + }; + // Close the sort menu on outside click or Escape useEffect(() => { if (!sortOpen) return; @@ -88,8 +109,7 @@ export default function ExplorePlants() { placeholder={t("ExplorePlants.search")} value={search} onChange={(e) => { - setSearch(e.target.value); - setPage(1); + updateExploreParams({ search: e.target.value, page: null }); }} /> @@ -104,8 +124,10 @@ export default function ExplorePlants() { { - setSunlight(e.target.value); - setPage(1); + updateExploreParams({ + sunlight: e.target.value, + page: null, + }); }} > @@ -138,8 +162,10 @@ export default function ExplorePlants() { diff --git a/frontend/src/tests/ExplorePage.test.tsx b/frontend/src/tests/ExplorePage.test.tsx index 0e07ca57..64bd50c8 100644 --- a/frontend/src/tests/ExplorePage.test.tsx +++ b/frontend/src/tests/ExplorePage.test.tsx @@ -200,6 +200,7 @@ describe("ExplorePlants", () => { it.each<[keyof Variables, string, string]>([ ["watering", "ExplorePlants.watering", "minimum"], ["sunlight", "ExplorePlants.sunlight", "part_shade"], + ["sunlight", "ExplorePlants.sunlight", "filtered_shade"], ["careLevel", "ExplorePlants.careLevel", "high"], ])( "sends the selected %s filter in the query", diff --git a/frontend/src/tests/__snapshots__/ExplorePage.test.tsx.snap b/frontend/src/tests/__snapshots__/ExplorePage.test.tsx.snap index 9c80ce7e..02bce4e1 100644 --- a/frontend/src/tests/__snapshots__/ExplorePage.test.tsx.snap +++ b/frontend/src/tests/__snapshots__/ExplorePage.test.tsx.snap @@ -94,14 +94,14 @@ exports[`ExplorePlants > snapshots > matches the snapshot of the page with no re ExplorePlants.full_sun @@ -302,14 +302,14 @@ exports[`ExplorePlants > snapshots > matches the snapshot of the page with resul ExplorePlants.full_sun @@ -841,14 +841,14 @@ exports[`ExplorePlants > snapshots > matches the snapshot of the page with the s ExplorePlants.full_sun