Fix bug with groups not being removed when deleting

This commit is contained in:
Mitchell McCaffrey 2021-07-01 11:24:55 +10:00
parent 4bdf147e2b
commit b28896db12
3 changed files with 17 additions and 7 deletions

View File

@ -29,7 +29,16 @@ export function GroupProvider({
const [openGroupItems, setOpenGroupItems] = useState([]);
useEffect(() => {
if (openGroupId) {
setOpenGroupItems(getGroupItems(groupsFromIds([openGroupId], groups)[0]));
const openGroups = groupsFromIds([openGroupId], groups);
if (openGroups.length === 1) {
const openGroup = openGroups[0];
setOpenGroupItems(getGroupItems(openGroup));
} else {
// Close group if we can't find it
// This can happen if it was deleted or all it's items were deleted
setOpenGroupItems([]);
setOpenGroupId();
}
} else {
setOpenGroupItems([]);
}

View File

@ -58,7 +58,6 @@ export function TileDragProvider({
selectedGroupIds,
onGroupsChange,
onGroupSelect,
onGroupClose,
filter,
} = useGroup();
@ -147,10 +146,6 @@ export function TileDragProvider({
onGroupSelect();
// Handle tile ungroup
const newGroups = ungroup(groups, openGroupId, selectedIndices);
// Close group if it was removed
if (!newGroups.find((group) => group.id === openGroupId)) {
onGroupClose();
}
onGroupsChange(newGroups);
} else if (over.id === ADD_TO_MAP_ID) {
onDragAdd &&

View File

@ -31,7 +31,9 @@ export function groupsFromIds(groupIds, groups) {
const groupsByIds = keyBy(groups, "id");
const filteredGroups = [];
for (let groupId of groupIds) {
filteredGroups.push(groupsByIds[groupId]);
if (groupId in groupsByIds) {
filteredGroups.push(groupsByIds[groupId]);
}
}
return filteredGroups;
}
@ -259,6 +261,10 @@ export function removeGroupsItems(groups, itemIds) {
newGroups[i].items.splice(j, 1);
}
}
// Remove group if no items are left
if (newGroups[i].items.length === 0) {
newGroups.splice(i, 1);
}
}
}