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:
@@ -371,6 +371,34 @@ describe('GET /api/items filtering', () => {
|
||||
expect(res.body.map((i: { name: string }) => i.name).sort()).toEqual(['Deep', 'Mid', 'Top']);
|
||||
});
|
||||
|
||||
it('combines several categories as any-of rather than all-of', async () => {
|
||||
const furniture = await createCategory('Furniture');
|
||||
const tables = await createCategory('Tables', furniture);
|
||||
const decor = await createCategory('Decor');
|
||||
const art = await createCategory('Art');
|
||||
|
||||
await createItem('Nested', 1000, tables);
|
||||
await createItem('Elsewhere', 1000, decor);
|
||||
await createItem('Unrelated', 1000, art);
|
||||
|
||||
// Two branches sharing no items, so an AND would answer with nothing at
|
||||
// all — which is the failure this exists to catch.
|
||||
const res = await request(app).get(`/api/items?category=${furniture},${decor}`);
|
||||
expect(res.body.map((i: { name: string }) => i.name).sort()).toEqual(['Elsewhere', 'Nested']);
|
||||
});
|
||||
|
||||
it('lists an item once when its category sits under two selected branches', async () => {
|
||||
const furniture = await createCategory('Furniture');
|
||||
const tables = await createCategory('Tables', furniture);
|
||||
await createItem('Nested', 1000, tables);
|
||||
|
||||
// The subtrees overlap: Tables is walked from both seeds, so the recursion
|
||||
// yields its id twice. Selecting on IN makes that a set test rather than a
|
||||
// join, so the item still appears once.
|
||||
const res = await request(app).get(`/api/items?category=${furniture},${tables}`);
|
||||
expect(res.body.map((i: { name: string }) => i.name)).toEqual(['Nested']);
|
||||
});
|
||||
|
||||
it('excludes uncategorized items from a category filter', async () => {
|
||||
const furniture = await createCategory('Furniture');
|
||||
await createItem('Filed', 1000, furniture);
|
||||
@@ -444,6 +472,16 @@ describe('GET /api/items filtering', () => {
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
it('rejects a category list with one unreadable entry rather than honouring the rest', async () => {
|
||||
const furniture = await createCategory('Furniture');
|
||||
await createItem('A', 1000, furniture);
|
||||
|
||||
// Filtering on the readable half would answer a narrower question than the
|
||||
// one asked, and look indistinguishable from a filter that worked.
|
||||
const res = await request(app).get(`/api/items?category=${furniture},furniture`);
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
it('rejects an inverted price range', async () => {
|
||||
const res = await request(app).get('/api/items?min_price=5000&max_price=1000');
|
||||
expect(res.status).toBe(400);
|
||||
|
||||
Reference in New Issue
Block a user