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

Commit

Permalink
Merge pull request #284 from City-of-Helsinki/TILA-1457
Browse files Browse the repository at this point in the history
TILA-1457 redo filters for application listing page
  • Loading branch information
siren authored Apr 20, 2022
2 parents 412affd + a1d9d2e commit ab92263
Show file tree
Hide file tree
Showing 4 changed files with 404 additions and 264 deletions.
12 changes: 12 additions & 0 deletions admin-ui/src/common/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,16 @@ test("filterData", () => {
[{ title: "", key: "name", value: "bar" }]
)
).toEqual([{ name: "bar" }]);
expect(
filterData(
[
{ name: "bar", size: 4 },
{ name: "bar", size: 5 },
],
[
{ title: "", key: "name", value: "bar" },
{ title: "", key: "size", value: 5 },
]
)
).toEqual([{ name: "bar", size: 5 }]);
});
22 changes: 15 additions & 7 deletions admin-ui/src/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import i18next from "i18next";
import trim from "lodash/trim";
import upperFirst from "lodash/upperFirst";
import get from "lodash/get";
import { uniqBy } from "lodash";
import { groupBy } from "lodash";
import {
AllocationResult,
ApplicationEventSchedule,
Expand Down Expand Up @@ -336,15 +336,23 @@ export const parseAddressLine2 = (
);
};

/** Filtering logic "OR within the group, AND between groups" */
export const filterData = <T>(data: T[], filters: DataFilterOption[]): T[] => {
const len = uniqBy(filters, "key").length;
return data.filter(
(row) =>
filters.filter((filter) => {
const groups = groupBy(filters, "key");
const groupCount = Object.keys(groups).length;

return data.filter((row) => {
const groupsNames = Object.keys(groups);
const groupsMatched = groupsNames.filter((name) => {
const found = groups[name].find((filter) => {
if (filter.function) {
return filter.function(row);
}
return get(row, filter.key as string) === filter.value;
}).length === len
);
});
return Boolean(found);
});

return groupsMatched.length === groupCount;
});
};
Loading

0 comments on commit ab92263

Please sign in to comment.