Added support for recursive groups in helper function

This commit is contained in:
Mitchell McCaffrey 2021-05-20 14:01:17 +10:00
parent 19a2668644
commit d34cd98fa9
2 changed files with 16 additions and 14 deletions

View File

@ -46,7 +46,6 @@ function MapTiles({
const selectedMapStates = itemsFromGroups(
selectedGroups,
mapStates,
true,
"mapId"
);

View File

@ -144,23 +144,26 @@ export function groupsFromIds(groupIds, groups) {
return filteredGroups;
}
export function itemsFromGroups(
groups,
allItems,
includeGroupedItems = true,
itemKey = "id"
) {
function getGroupItems(group) {
if (group.type === "group") {
let groups = [];
for (let item of group.items) {
groups.push(...getGroupItems(item));
}
return groups;
} else {
return [group];
}
}
export function itemsFromGroups(groups, allItems, itemKey = "id") {
const allItemsById = keyBy(allItems, itemKey);
const groupedItems = [];
for (let group of groups) {
if (group.type === "item") {
groupedItems.push(allItemsById[group.id]);
} else if (group.type === "group" && includeGroupedItems) {
for (let item of group.items) {
groupedItems.push(allItemsById[item.id]);
}
}
const groupItems = getGroupItems(group);
const items = groupItems.map((item) => allItemsById[item.id]);
groupedItems.push(...items);
}
return groupedItems;