From a5f9981ab993bdb660fbf41405578f63103135c4 Mon Sep 17 00:00:00 2001 From: Helene Lotsberg Date: Sun, 22 Nov 2026 17:11:49 +0100 Subject: [PATCH 01/11] Add search, filters and filter counts to myMatches in the backend --- backend/src/queryBuilders.ts | 27 +++++---- backend/src/resolvers.ts | 105 ++++++++++++++++++----------------- backend/src/schema.ts | 13 ++--- backend/src/types.ts | 24 ++++---- backend/tests/server.test.ts | 101 +++++++++++++++++++++++++++++++++ 5 files changed, 191 insertions(+), 79 deletions(-) diff --git a/backend/src/queryBuilders.ts b/backend/src/queryBuilders.ts index 99305ca5..e5ce14a3 100644 --- a/backend/src/queryBuilders.ts +++ b/backend/src/queryBuilders.ts @@ -1,9 +1,9 @@ import type { Document, Filter, ObjectId } from "mongodb"; import type { - ExplorePlantsArgs, FilterCounts, FilterParam, PlantDoc, + PlantFilterArgs, } from "./types.js"; // Helpers for searching, filtering and sorting plants @@ -95,10 +95,8 @@ export const buildMatchSortStages = ( } }; -// Builds the MongoDB filter for the search and filters on Explore Plants -export const buildExploreFilter = ( - args: ExplorePlantsArgs, -): Filter => { +// Builds the MongoDB filter for the search and filters on Explore Plants and My Matches +export const buildPlantFilter = (args: PlantFilterArgs): Filter => { const filter: Filter = {}; if (args.search?.trim()) { const search = { $regex: escapeRegex(args.search.trim()), $options: "i" }; @@ -125,8 +123,8 @@ export const buildExploreFilter = ( return filter; }; -// The values each filter can have. Must match FILTERS in -// frontend/src/pages/explorePlants/ExplorePlantsPage.tsx +// The values each filter can have. Must match PLANT_FILTERS in +// frontend/src/components/plantSearchForm/plantFilters.ts export const FILTER_OPTIONS: Record = { watering: ["minimum", "average", "frequent"], sunlight: ["full_sun", "part_shade", "filtered_shade"], @@ -138,22 +136,27 @@ const facetKey = (param: string, value: string) => `${param}:${value}`; // Stages that count, for every filter value, how many plants the user would get // by choosing it. The value replaces the current choice in that filter, while the -// search and the other filters stay. Uses buildExploreFilter, so the counts +// search and the other filters stay. Uses buildPlantFilter, so the counts // always agree with the results. All counts are done in one query. -export const buildFilterCountStages = (args: ExplorePlantsArgs): Document[] => { +// baseFilter limits which plants are counted at all, e.g. only the user's +// matches on My Matches. On Explore Plants every plant is counted. +export const buildFilterCountStages = ( + args: PlantFilterArgs, + baseFilter: Filter = {}, +): Document[] => { const facets: Record = {}; for (const [param, options] of Object.entries(FILTER_OPTIONS)) { // "" is "All": nothing chosen in this filter, so only the search and the other filters count for (const value of ["", ...options]) { facets[facetKey(param, value)] = [ - { $match: buildExploreFilter({ ...args, [param]: value }) }, + { $match: buildPlantFilter({ ...args, [param]: value }) }, { $count: "count" }, ]; } } return [ - // Only the plants matching the search need to be looked at - { $match: buildExploreFilter({ search: args.search }) }, + // Only the plants matching the base filter and the search need to be looked at + { $match: { ...baseFilter, ...buildPlantFilter({ search: args.search }) } }, { $facet: facets }, ]; }; diff --git a/backend/src/resolvers.ts b/backend/src/resolvers.ts index 67ed8d96..833e5f6b 100644 --- a/backend/src/resolvers.ts +++ b/backend/src/resolvers.ts @@ -1,9 +1,9 @@ import { GraphQLError } from "graphql"; import { ObjectId } from "mongodb"; -import type { Document, WithId } from "mongodb"; +import type { Document, Filter, WithId } from "mongodb"; import { db } from "./db.js"; import { - buildExploreFilter, + buildPlantFilter, buildFilterCountStages, buildMatchSortStages, buildSortStages, @@ -13,11 +13,12 @@ import { } from "./queryBuilders.js"; import type { ExplorePlantsArgs, - ExplorePage, GetPlantsArgs, MyMatchesArgs, Plant, PlantDoc, + PlantFilterArgs, + PlantPage, UpdateUserPreferencesArgs, UpdateUserProfileArgs, User, @@ -112,6 +113,42 @@ const cleanText = ( return trimmed || null; }; +// One page of plants for Explore Plants and My Matches: the plants matching the +// base filter (on My Matches the user's matches) and the user's search and +// filters, the total count, and the match counts for the filter values, so the +// frontend can show them and disable the values that would give no plants. +// The three queries run at the same time. +const findPlantPage = async ( + baseFilter: Filter, + args: PlantFilterArgs & { limit?: number | null; offset?: number | null }, + sortStages: Document[], +): Promise => { + const filter = { ...baseFilter, ...buildPlantFilter(args) }; + + const [docs, totalCount, [facets]] = await Promise.all([ + plants + .aggregate>([ + { $match: filter }, + ...sortStages, + { $skip: clampOffset(args.offset) }, + { $limit: clampLimit(args.limit, DEFAULT_PAGE_SIZE) }, + ]) + .toArray(), + plants.countDocuments(filter), + plants + .aggregate>( + buildFilterCountStages(args, baseFilter), + ) + .toArray(), + ]); + + return { + plants: docs.map(toPlant), + totalCount, + filterCounts: toFilterCounts(facets), + }; +}; + // Applies an update to one user and returns the updated user, or null if not found const updateUser = async ( userId: string, @@ -184,34 +221,8 @@ export const resolvers = { explorePlants: async ( _parent: unknown, args: ExplorePlantsArgs, - ): Promise => { - const filter = buildExploreFilter(args); - - const [docs, totalCount, [facets]] = await Promise.all([ - plants - .aggregate>([ - { $match: filter }, - ...buildSortStages(args.sort), - { $skip: clampOffset(args.offset) }, - { $limit: clampLimit(args.limit, DEFAULT_PAGE_SIZE) }, - ]) - .toArray(), - plants.countDocuments(filter), - // Match counts for the filter values, so the frontend can show them - // and disable the values that would give no plants - plants - .aggregate>( - buildFilterCountStages(args), - ) - .toArray(), - ]); - - return { - plants: docs.map(toPlant), - totalCount, - filterCounts: toFilterCounts(facets), - }; - }, + ): Promise => + findPlantPage({}, args, buildSortStages(args.sort)), getUser: async ( _parent: unknown, @@ -223,35 +234,29 @@ export const resolvers = { return doc ? toUser(doc) : null; }, - // One page of the user's matched plants, sorted. Only the ids are stored on - // the user, so the plants are looked up and sorted in the plants collection. - // An unknown or malformed user id gives an empty page. + // One page of the user's matched plants, searched, filtered and sorted. + // Only the ids are stored on the user, so the plants are looked up and + // sorted in the plants collection. An unknown or malformed user id gives an + // empty page. myMatches: async ( _parent: unknown, args: MyMatchesArgs, - ): Promise<{ plants: Plant[]; totalCount: number }> => { + ): Promise => { const _id = toObjectId(args.userId); const user = _id ? await users.findOne({ _id }, { projection: { matchedPlantIds: 1 } }) : null; const matchedPlantIds = user?.matchedPlantIds ?? []; - if (matchedPlantIds.length === 0) return { plants: [], totalCount: 0 }; + if (matchedPlantIds.length === 0) { + return { plants: [], totalCount: 0, filterCounts: toFilterCounts() }; + } // Only plants that still exist are shown and counted (e.g. after a reseed) - const filter = { _id: { $in: matchedPlantIds } }; - const [docs, totalCount] = await Promise.all([ - plants - .aggregate>([ - { $match: filter }, - ...buildMatchSortStages(args.sort, matchedPlantIds), - { $skip: clampOffset(args.offset) }, - { $limit: clampLimit(args.limit, DEFAULT_PAGE_SIZE) }, - ]) - .toArray(), - plants.countDocuments(filter), - ]); - - return { plants: docs.map(toPlant), totalCount }; + return findPlantPage( + { _id: { $in: matchedPlantIds } }, + args, + buildMatchSortStages(args.sort, matchedPlantIds), + ); }, }, diff --git a/backend/src/schema.ts b/backend/src/schema.ts index 87193276..ba34670b 100644 --- a/backend/src/schema.ts +++ b/backend/src/schema.ts @@ -10,10 +10,14 @@ export const typeDefs = ` sort: String limit: Int = 12 offset: Int = 0 - ): ExplorePage! + ): PlantPage! getUser(userId: ID!): User myMatches( userId: ID! + search: String + sunlight: String + watering: String + careLevel: String sort: String limit: Int = 12 offset: Int = 0 @@ -33,11 +37,6 @@ export const typeDefs = ` removeUserMatch(userId: ID!, plantId: ID!): User } - type PlantPage { - plants: [Plant!]! - totalCount: Int! - } - type OptionCount { value: String! count: Int! @@ -49,7 +48,7 @@ export const typeDefs = ` careLevel: [OptionCount!]! } - type ExplorePage { + type PlantPage { plants: [Plant!]! totalCount: Int! filterCounts: FilterCounts! diff --git a/backend/src/types.ts b/backend/src/types.ts index ffe1d580..583b550d 100644 --- a/backend/src/types.ts +++ b/backend/src/types.ts @@ -35,17 +35,21 @@ export interface GetPlantsArgs { preferences?: string[] | null; } -export interface ExplorePlantsArgs { - search?: string; - sunlight?: string; - watering?: string; - careLevel?: string; +// The search and filters on Explore Plants and My Matches +export interface PlantFilterArgs { + search?: string | null; + sunlight?: string | null; + watering?: string | null; + careLevel?: string | null; +} + +export interface ExplorePlantsArgs extends PlantFilterArgs { sort?: string; limit?: number | null; offset?: number | null; } -// The filters on Explore Plants +// The filters on Explore Plants and My Matches export type FilterParam = "watering" | "sunlight" | "careLevel"; // How many plants a filter value would give, with the current search and other filters @@ -56,9 +60,9 @@ export interface OptionCount { export type FilterCounts = Record; -// What explorePlants returns. A separate type from PlantPage, since -// My Matches also uses PlantPage but has no filters to count. -export interface ExplorePage { +// One page of plants, as explorePlants and myMatches return it, with the +// total count and the match counts for the filters +export interface PlantPage { plants: Plant[]; totalCount: number; filterCounts: FilterCounts; @@ -107,7 +111,7 @@ export interface UserMatchArgs extends UserIdArgs { plantId: string; } -export interface MyMatchesArgs extends UserIdArgs { +export interface MyMatchesArgs extends UserIdArgs, PlantFilterArgs { sort?: string; limit?: number | null; offset?: number | null; diff --git a/backend/tests/server.test.ts b/backend/tests/server.test.ts index 7ef20356..018f8120 100644 --- a/backend/tests/server.test.ts +++ b/backend/tests/server.test.ts @@ -734,6 +734,107 @@ describe("my matches", () => { totalCount: 0, }); }); + + const FILTERED_MATCHES = ` + query ($userId: ID!, $search: String, $watering: String) { + myMatches(userId: $userId, search: $search, watering: $watering) { + totalCount + plants { name } + filterCounts { + watering { value count } + careLevel { value count } + } + } + } + `; + + type FilteredPage = Page & { + filterCounts: Record; + }; + + // The names, the total count and the filter counts as + // { watering: { "": 3, minimum: 2, ... }, ... }, easier to read in the tests + async function filteredMatches(variables: Record) { + const result = await execute<{ myMatches: FilteredPage }>( + FILTERED_MATCHES, + variables, + ); + expect(result.errors).toBeUndefined(); + const page = result.data!.myMatches; + return { + names: page.plants.map((p) => p.name), + totalCount: page.totalCount, + counts: Object.fromEntries( + Object.entries(page.filterCounts).map(([param, options]) => [ + param, + Object.fromEntries(options.map((o) => [o.value, o.count])), + ]), + ), + }; + } + + // Matches Basil, Cactus and Aloe in that order. Daisy is not matched, so + // it must never be shown or counted on My Matches. + async function userWithVariedMatches() { + const { id } = await createUser(); + const [aloe, basil, cactus] = await insertPlants( + { commonName: "Aloe", watering: "Minimum", careLevel: "Low" }, + { commonName: "Basil", watering: "Frequent", careLevel: "Medium" }, + { commonName: "Cactus", watering: "Minimum", careLevel: "High" }, + { commonName: "Daisy", watering: "Minimum", careLevel: "Low" }, + ); + for (const plantId of [basil, cactus, aloe]) { + await addMatch(id, plantId); + } + return id; + } + + it("filters and counts only among the user's matches", async () => { + const id = await userWithVariedMatches(); + + expect(await filteredMatches({ userId: id, watering: "minimum" })).toEqual({ + // Newest first, and Daisy is left out even though it has minimum watering + names: ["Aloe", "Cactus"], + totalCount: 2, + counts: { + // Choosing another watering value replaces "minimum" + watering: { "": 3, minimum: 2, average: 0, frequent: 1 }, + // Only Aloe and Cactus have minimum watering + careLevel: { "": 2, low: 1, medium: 0, high: 1 }, + }, + }); + }); + + it("searches only among the user's matches", async () => { + const id = await userWithVariedMatches(); + + const page = await filteredMatches({ userId: id, search: "a" }); + // Daisy also contains "a", but is not a match + expect(page.names).toEqual(["Aloe", "Cactus", "Basil"]); + expect(page.counts.watering).toEqual({ + "": 3, + minimum: 2, + average: 0, + frequent: 1, + }); + + expect( + (await filteredMatches({ userId: id, search: "cact" })).names, + ).toEqual(["Cactus"]); + }); + + it("gives zero counts for a user without matches", async () => { + const { id } = await createUser(); + + expect(await filteredMatches({ userId: id })).toEqual({ + names: [], + totalCount: 0, + counts: { + watering: { "": 0, minimum: 0, average: 0, frequent: 0 }, + careLevel: { "": 0, low: 0, medium: 0, high: 0 }, + }, + }); + }); }); describe("error messages", () => { From 03ce0136348ffbe4fa60a17030bd7226f964be38 Mon Sep 17 00:00:00 2001 From: Helene Lotsberg Date: Sun, 22 Nov 2026 17:12:09 +0100 Subject: [PATCH 02/11] Move search and filters into a shared PlantSearchForm and make visually-hidden global --- .../plantSearchForm/PlantSearchForm.css | 74 +++++++++++++ .../plantSearchForm/PlantSearchForm.tsx | 103 ++++++++++++++++++ .../plantSearchForm/plantFilters.ts | 24 ++++ frontend/src/hooks/useExploreParams.ts | 4 +- frontend/src/index.css | 11 ++ .../pages/explorePlants/ExplorePlantsPage.css | 79 -------------- .../pages/explorePlants/ExplorePlantsPage.tsx | 99 +++-------------- frontend/src/tests/ExplorePlantsPage.test.tsx | 2 +- .../ExplorePlantsPage.test.tsx.snap | 24 ++-- 9 files changed, 242 insertions(+), 178 deletions(-) create mode 100644 frontend/src/components/plantSearchForm/PlantSearchForm.css create mode 100644 frontend/src/components/plantSearchForm/PlantSearchForm.tsx create mode 100644 frontend/src/components/plantSearchForm/plantFilters.ts diff --git a/frontend/src/components/plantSearchForm/PlantSearchForm.css b/frontend/src/components/plantSearchForm/PlantSearchForm.css new file mode 100644 index 00000000..8ea7c78c --- /dev/null +++ b/frontend/src/components/plantSearchForm/PlantSearchForm.css @@ -0,0 +1,74 @@ +/* The search field, filters, sort menu and reset button on Explore Plants + and My Matches */ +.plant-search-form { + width: 100%; +} + +.plant-search { + position: relative; + display: block; +} + +.plant-search-icon { + position: absolute; + top: 50%; + left: 1rem; + font-size: 1.25rem; + transform: translateY(-50%); + color: var(--pink-text); + pointer-events: none; +} + +.plant-search input { + width: 100%; + padding: 0.75rem 1rem 0.75rem 2.75rem; + font-size: 1.25rem; + color: var(--text-dark); + background-color: var(--light-pink); + border: 1px solid var(--dark-pink); + border-radius: 8px; + box-sizing: border-box; +} + +.plant-search input:focus { + outline: none; + border-color: var(--dark-pink); + box-shadow: 0 0 0 2px var(--dark-pink); +} + +.plant-search input::placeholder { + color: var(--text-dark); +} + +.plant-filters { + display: flex; + flex-wrap: wrap; + gap: 1rem; + margin: 1rem 0 0; + padding: 0; + border: 0; +} + +/* At the end of the filter row, at the same height as the sort button */ +.reset-button { + align-self: flex-end; + height: 40px; + padding: 0 1rem; + border: 1px solid var(--dark-pink); + border-radius: 8px; + background-color: var(--dark-pink); + color: var(--text-light); + font-size: 15px; + cursor: pointer; +} + +.reset-button:hover { + background-color: var(--light-pink); + color: var(--text-dark); +} + +.reset-button.reset-button:focus-visible { + outline: none; + border-color: var(--dark-pink); + box-shadow: 0 0 0 2px var(--dark-pink); +} diff --git a/frontend/src/components/plantSearchForm/PlantSearchForm.tsx b/frontend/src/components/plantSearchForm/PlantSearchForm.tsx new file mode 100644 index 00000000..7772bcd9 --- /dev/null +++ b/frontend/src/components/plantSearchForm/PlantSearchForm.tsx @@ -0,0 +1,103 @@ +import { useTranslation } from "react-i18next"; +import { SearchIcon } from "../icons/Icons"; +import FilterSelect from "../filterSelect/FilterSelect"; +import SortMenu from "../sortMenu/SortMenu"; +import type { FilterCounts } from "../../graphql/plants/queries"; +import { + PLANT_FILTERS, + type FilterParam, + type PlantFilters, +} from "./plantFilters"; +import "./PlantSearchForm.css"; + +type SortOption = { value: string; label: string }; + +type PlantSearchFormProps = { + search: string; + onSearchChange: (value: string) => void; + filters: PlantFilters; + // How many plants each filter value would give. Undefined until the first + // result has come, and then nothing is disabled. + filterCounts?: FilterCounts; + onFilterChange: (param: FilterParam, value: string) => void; + sortLabel: string; + sort: string; + sortOptions: SortOption[]; + onSortChange: (value: string) => void; + onReset: () => void; +}; + +// The search field, the filters, the sort menu and the reset button above a +// list of plants. Used on both Explore Plants and My Matches, so the two pages +// look and work the same. The page decides where the choices are kept. +export default function PlantSearchForm({ + search, + onSearchChange, + filters, + filterCounts, + onFilterChange, + sortLabel, + sort, + sortOptions, + onSortChange, + onReset, +}: PlantSearchFormProps) { + const { t } = useTranslation(); + + // The number of plants a value would give, or undefined before the counts have come + const countFor = (param: FilterParam, value: string) => + filterCounts?.[param].find((c) => c.value === value)?.count; + + return ( +
e.preventDefault()} + > + + +
+ + {t("explorePlantsPage.filters")} + + + {PLANT_FILTERS.map((filter) => ( + ({ + value: option, + label: t(`explorePlantsPage.${option}`), + count: countFor(filter.param, option), + }))} + onChange={(value) => onFilterChange(filter.param, value)} + /> + ))} + + + + +
+
+ ); +} diff --git a/frontend/src/components/plantSearchForm/plantFilters.ts b/frontend/src/components/plantSearchForm/plantFilters.ts new file mode 100644 index 00000000..9530c92d --- /dev/null +++ b/frontend/src/components/plantSearchForm/plantFilters.ts @@ -0,0 +1,24 @@ +// The filters on Explore Plants and My Matches: the name of the filter, the +// translation key for the label, and the values to choose from. Must match +// FILTER_OPTIONS in backend/src/queryBuilders.ts. A new filter only needs a +// new line here (and in the backend). +export const PLANT_FILTERS = [ + { + param: "watering", + label: "explorePlantsPage.watering", + options: ["minimum", "average", "frequent"], + }, + { + param: "sunlight", + label: "explorePlantsPage.sunlight", + options: ["full_sun", "part_shade", "filtered_shade"], + }, + { + param: "careLevel", + label: "explorePlantsPage.careLevel", + options: ["low", "medium", "high"], + }, +] as const; + +export type FilterParam = (typeof PLANT_FILTERS)[number]["param"]; +export type PlantFilters = Record; diff --git a/frontend/src/hooks/useExploreParams.ts b/frontend/src/hooks/useExploreParams.ts index 2b804940..ff9c6e87 100644 --- a/frontend/src/hooks/useExploreParams.ts +++ b/frontend/src/hooks/useExploreParams.ts @@ -1,7 +1,5 @@ import { useUrlParams } from "./useUrlParams"; - -// The filters on Explore Plants, as they are named in the URL -type FilterParam = "watering" | "sunlight" | "careLevel"; +import type { FilterParam } from "../components/plantSearchForm/plantFilters"; const DEFAULT_SORT = "name_asc"; diff --git a/frontend/src/index.css b/frontend/src/index.css index 4ce02caa..0b2978ff 100644 --- a/frontend/src/index.css +++ b/frontend/src/index.css @@ -119,3 +119,14 @@ textarea { --card-front: var(--cream); --card-hint: var(--deep-green); } + +/* Text that is only read by screen readers. Global, since the pages are loaded + one at a time and several of them and the shared components use it. */ +.visually-hidden { + position: absolute; + width: 1px; + height: 1px; + overflow: hidden; + clip: rect(0 0 0 0); + white-space: nowrap; +} diff --git a/frontend/src/pages/explorePlants/ExplorePlantsPage.css b/frontend/src/pages/explorePlants/ExplorePlantsPage.css index de7f75c8..642087d9 100644 --- a/frontend/src/pages/explorePlants/ExplorePlantsPage.css +++ b/frontend/src/pages/explorePlants/ExplorePlantsPage.css @@ -11,89 +11,10 @@ gap: 30px; } -.visually-hidden { - position: absolute; - width: 1px; - height: 1px; - overflow: hidden; - clip: rect(0 0 0 0); - white-space: nowrap; -} - -.explore-form, .plant-grid { width: 100%; } -.explore-search { - position: relative; - display: block; -} - -.explore-search-icon { - position: absolute; - top: 50%; - left: 1rem; - font-size: 1.25rem; - transform: translateY(-50%); - color: var(--pink-text); - pointer-events: none; -} - -.explore-search input { - width: 100%; - padding: 0.75rem 1rem 0.75rem 2.75rem; - font-size: 1.25rem; - color: var(--text-dark); - background-color: var(--light-pink); - border: 1px solid var(--dark-pink); - border-radius: 8px; - box-sizing: border-box; -} - -.explore-search input:focus { - outline: none; - border-color: var(--dark-pink); - box-shadow: 0 0 0 2px var(--dark-pink); -} - -.explore-search input::placeholder { - color: var(--text-dark); -} - -.explore-filters { - display: flex; - flex-wrap: wrap; - gap: 1rem; - margin: 1rem 0 0; - padding: 0; - border: 0; -} - -/* At the end of the filter row, at the same height as the sort button */ -.reset-button { - align-self: flex-end; - height: 40px; - padding: 0 1rem; - border: 1px solid var(--dark-pink); - border-radius: 8px; - background-color: var(--dark-pink); - color: var(--text-light); - font-size: 15px; - cursor: pointer; -} - -.reset-button:hover { - background-color: var(--light-pink); - color: var(--text-dark); -} - -.reset-button.reset-button:focus-visible { - outline: none; - border-color: var(--dark-pink); - box-shadow: 0 0 0 2px var(--dark-pink); -} - .explore-results { width: 100%; box-sizing: border-box; diff --git a/frontend/src/pages/explorePlants/ExplorePlantsPage.tsx b/frontend/src/pages/explorePlants/ExplorePlantsPage.tsx index 182298ef..3a761658 100644 --- a/frontend/src/pages/explorePlants/ExplorePlantsPage.tsx +++ b/frontend/src/pages/explorePlants/ExplorePlantsPage.tsx @@ -1,4 +1,3 @@ -import { SearchIcon } from "../../components/icons/Icons"; import { useTranslation } from "react-i18next"; import { useQuery } from "@apollo/client/react"; import { useDebounce } from "../../hooks/useDebounce"; @@ -9,8 +8,7 @@ import { } from "../../graphql/plants/queries.ts"; import PlantCard from "../../components/plantCard/PlantCard.tsx"; import Pagination from "../../components/pagination/Pagination.tsx"; -import FilterSelect from "../../components/filterSelect/FilterSelect.tsx"; -import SortMenu from "../../components/sortMenu/SortMenu.tsx"; +import PlantSearchForm from "../../components/plantSearchForm/PlantSearchForm.tsx"; import MatchButton from "../../components/matchButton/MatchButton.tsx"; import { useMatchedPlants } from "../../hooks/useMatchedPlants.ts"; import "./ExplorePlantsPage.css"; @@ -19,26 +17,6 @@ const PAGE_SIZE = 12; const SORT_OPTIONS = ["name_asc", "name_desc", "watering_asc", "careLevel_asc"]; -// The filters on the page: the URL parameter, the translation key for the label, -// and the values to choose from. A new filter only needs a new line here. -const FILTERS = [ - { - param: "watering", - label: "explorePlantsPage.watering", - options: ["minimum", "average", "frequent"], - }, - { - param: "sunlight", - label: "explorePlantsPage.sunlight", - options: ["full_sun", "part_shade", "filtered_shade"], - }, - { - param: "careLevel", - label: "explorePlantsPage.careLevel", - options: ["low", "medium", "high"], - }, -] as const; - export default function ExplorePlantsPage() { const { t } = useTranslation(); @@ -86,70 +64,25 @@ export default function ExplorePlantsPage() { const plants = data?.explorePlants.plants ?? []; const totalCount = data?.explorePlants.totalCount ?? 0; const totalPages = Math.max(1, Math.ceil(totalCount / PAGE_SIZE)); - const filterCounts = data?.explorePlants.filterCounts; return (
{/* The page heading (h1) is in the Header, like on every page */} -
e.preventDefault()} - > - - -
- - {t("explorePlantsPage.filters")} - - - {FILTERS.map((filter) => ( - c.value === "")?.count - } - options={filter.options.map((option) => ({ - value: option, - label: t(`explorePlantsPage.${option}`), - // No counts until the first result has come, then nothing is disabled - count: filterCounts?.[filter.param].find( - (c) => c.value === option, - )?.count, - }))} - onChange={(value) => setFilter(filter.param, value)} - /> - ))} - - ({ - value: option, - label: t(`explorePlantsPage.${option}`), - }))} - onChange={setSort} - /> - - -
-
+ ({ + value: option, + label: t(`explorePlantsPage.${option}`), + }))} + onSortChange={setSort} + onReset={reset} + />
({ data: { explorePlants: { - __typename: "ExplorePage", + __typename: "PlantPage", totalCount, plants, filterCounts, diff --git a/frontend/src/tests/__snapshots__/ExplorePlantsPage.test.tsx.snap b/frontend/src/tests/__snapshots__/ExplorePlantsPage.test.tsx.snap index d307c28b..a536b78f 100644 --- a/frontend/src/tests/__snapshots__/ExplorePlantsPage.test.tsx.snap +++ b/frontend/src/tests/__snapshots__/ExplorePlantsPage.test.tsx.snap @@ -6,11 +6,11 @@ exports[`ExplorePlantsPage > snapshots > matches the snapshot of the page with n class="explore-page" >