feat(filters): make the storefront filter panel searchable and multi-select (#139)
The filter drawer did not scale with the taxonomy behind it. Categories were a bare antd `Tree` rendered at whatever depth it had grown to, with no search and single selection, and tags were a wall of every tag in the system. Neither said what was selected except through highlighting and chip colour. Both are now searchable multi-selects. Categories keep their hierarchy in a `TreeSelect`, matching the admin's `CategoryTreeSelect` so the two screens behave alike; tags become a multiple `Select` whose selected pills keep their colours, which is the only place a tag's colour was load-bearing. Several categories combine as OR. A customer picking Furniture and Decor wants both, not the empty intersection, and each selected id still expands to its descendants, so the answer is the union of the subtrees. That is deliberately the opposite of the tag rule, which stays AND, and both headings now state their rule rather than leaving it to be discovered. `ItemFilters.categoryId` becomes `categoryIds` end to end. The recursive CTE is seeded with `= ANY($n::int[])` rather than one id, which walks every selected root in one recursion and gives the OR for free; matching on `IN` keeps it a set test, so an item under two selected branches still appears once. The query parameter keeps its singular name and becomes comma-separated, the shape `tags` and `status` already use, so every `?category=1` link written before this still parses as a list of one. A list containing anything unreadable is still a 400, per decision 9 — honouring the readable half would answer a narrower question than the one asked and look indistinguishable from a filter that worked. The admin's inventory filter stays single-select, since it asks what is in a category rather than in any of several, but reads and writes a list of at most one so there is one shared filter type rather than two that drift. Closes #139
This commit is contained in:
+19
-5
@@ -3,7 +3,14 @@ import type { Category } from './api';
|
||||
export type ItemStatus = 'pending' | 'available' | 'reserved' | 'sold';
|
||||
|
||||
export interface ItemFilters {
|
||||
categoryId: number | null;
|
||||
// Several categories, combined as OR — picking Furniture and Decor means
|
||||
// either, not the empty intersection. Deliberately the opposite of tagIds
|
||||
// below, which is AND, and both controls label their rule so the difference
|
||||
// is stated rather than discovered.
|
||||
//
|
||||
// The admin's inventory filter is single-select and holds a list of one; the
|
||||
// type is shared, and one shape is better than two that drift.
|
||||
categoryIds: number[];
|
||||
tagIds: number[];
|
||||
minPriceCents: number | null;
|
||||
maxPriceCents: number | null;
|
||||
@@ -69,7 +76,7 @@ export function saleStateFromStatuses(
|
||||
}
|
||||
|
||||
export const EMPTY_FILTERS: ItemFilters = {
|
||||
categoryId: null,
|
||||
categoryIds: [],
|
||||
tagIds: [],
|
||||
minPriceCents: null,
|
||||
maxPriceCents: null,
|
||||
@@ -82,7 +89,9 @@ export const EMPTY_FILTERS: ItemFilters = {
|
||||
// what GET /api/items accepts, so the same object serializes for both.
|
||||
export function filtersToSearchParams(filters: ItemFilters): URLSearchParams {
|
||||
const params = new URLSearchParams();
|
||||
if (filters.categoryId !== null) params.set('category', String(filters.categoryId));
|
||||
// Comma-separated under the singular name it has always had, so a link
|
||||
// written before this went multi-valued still means what it meant.
|
||||
if (filters.categoryIds.length) params.set('category', filters.categoryIds.join(','));
|
||||
if (filters.tagIds.length) params.set('tags', filters.tagIds.join(','));
|
||||
if (filters.minPriceCents !== null) params.set('min_price', String(filters.minPriceCents));
|
||||
if (filters.maxPriceCents !== null) params.set('max_price', String(filters.maxPriceCents));
|
||||
@@ -98,6 +107,11 @@ function readInt(raw: string | null): number | null {
|
||||
}
|
||||
|
||||
export function filtersFromSearchParams(params: URLSearchParams): ItemFilters {
|
||||
const categories = (params.get('category') || '')
|
||||
.split(',')
|
||||
.map((part) => readInt(part))
|
||||
.filter((id): id is number => id !== null && id > 0);
|
||||
|
||||
const tags = (params.get('tags') || '')
|
||||
.split(',')
|
||||
.map((part) => readInt(part))
|
||||
@@ -127,7 +141,7 @@ export function filtersFromSearchParams(params: URLSearchParams): ItemFilters {
|
||||
const favorites = params.get('favorites');
|
||||
|
||||
return {
|
||||
categoryId: readInt(params.get('category')),
|
||||
categoryIds: categories,
|
||||
tagIds: tags,
|
||||
minPriceCents: readInt(params.get('min_price')),
|
||||
maxPriceCents: readInt(params.get('max_price')),
|
||||
@@ -145,7 +159,7 @@ export function filtersFromSearchParams(params: URLSearchParams): ItemFilters {
|
||||
// displays its own position.
|
||||
export function activeFilterCount(filters: ItemFilters): number {
|
||||
let count = 0;
|
||||
if (filters.categoryId !== null) count++;
|
||||
count += filters.categoryIds.length;
|
||||
count += filters.tagIds.length;
|
||||
if (filters.minPriceCents !== null || filters.maxPriceCents !== null) count++;
|
||||
if (filters.favoritesOnly) count++;
|
||||
|
||||
Reference in New Issue
Block a user