Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit ab92263

Browse files
authored
Merge pull request #284 from City-of-Helsinki/TILA-1457
TILA-1457 redo filters for application listing page
2 parents 412affd + a1d9d2e commit ab92263

4 files changed

Lines changed: 404 additions & 264 deletions

File tree

admin-ui/src/common/util.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,16 @@ test("filterData", () => {
5050
[{ title: "", key: "name", value: "bar" }]
5151
)
5252
).toEqual([{ name: "bar" }]);
53+
expect(
54+
filterData(
55+
[
56+
{ name: "bar", size: 4 },
57+
{ name: "bar", size: 5 },
58+
],
59+
[
60+
{ title: "", key: "name", value: "bar" },
61+
{ title: "", key: "size", value: 5 },
62+
]
63+
)
64+
).toEqual([{ name: "bar", size: 5 }]);
5365
});

admin-ui/src/common/util.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import i18next from "i18next";
33
import trim from "lodash/trim";
44
import upperFirst from "lodash/upperFirst";
55
import get from "lodash/get";
6-
import { uniqBy } from "lodash";
6+
import { groupBy } from "lodash";
77
import {
88
AllocationResult,
99
ApplicationEventSchedule,
@@ -336,15 +336,23 @@ export const parseAddressLine2 = (
336336
);
337337
};
338338

339+
/** Filtering logic "OR within the group, AND between groups" */
339340
export const filterData = <T>(data: T[], filters: DataFilterOption[]): T[] => {
340-
const len = uniqBy(filters, "key").length;
341-
return data.filter(
342-
(row) =>
343-
filters.filter((filter) => {
341+
const groups = groupBy(filters, "key");
342+
const groupCount = Object.keys(groups).length;
343+
344+
return data.filter((row) => {
345+
const groupsNames = Object.keys(groups);
346+
const groupsMatched = groupsNames.filter((name) => {
347+
const found = groups[name].find((filter) => {
344348
if (filter.function) {
345349
return filter.function(row);
346350
}
347351
return get(row, filter.key as string) === filter.value;
348-
}).length === len
349-
);
352+
});
353+
return Boolean(found);
354+
});
355+
356+
return groupsMatched.length === groupCount;
357+
});
350358
};

0 commit comments

Comments
 (0)