Add remove group items for maps and tokens

This commit is contained in:
Mitchell McCaffrey 2021-06-09 09:29:32 +10:00
parent 1ec67c7a0f
commit ee34c599da
5 changed files with 42 additions and 2 deletions

View File

@ -62,8 +62,8 @@ function MapEditBar({ currentMap, disabled, onMapChange, onMapReset, onLoad }) {
setIsMapsRemoveModalOpen(false);
const selectedMaps = getSelectedMaps();
const selectedMapIds = selectedMaps.map((map) => map.id);
await removeMaps(selectedMapIds);
onGroupSelect();
await removeMaps(selectedMapIds);
// Removed the map from the map screen if needed
if (currentMap && selectedMapIds.includes(currentMap.id)) {
onMapChange(null, null);

View File

@ -44,8 +44,8 @@ function TokenEditBar({ disabled, onLoad }) {
setIsTokensRemoveModalOpen(false);
const selectedTokens = getSelectedTokens();
const selectedTokenIds = selectedTokens.map((token) => token.id);
await removeTokens(selectedTokenIds);
onGroupSelect();
await removeTokens(selectedTokenIds);
onLoad(false);
}

View File

@ -5,6 +5,7 @@ import { useAuth } from "./AuthContext";
import { useDatabase } from "./DatabaseContext";
import { applyObservableChange } from "../helpers/dexie";
import { removeGroupsItems } from "../helpers/group";
const MapDataContext = React.createContext();
@ -105,6 +106,11 @@ export function MapDataProvider({ children }) {
}
}
}
const group = await database.table("groups").get("maps");
let items = removeGroupsItems(group.items, ids);
await database.table("groups").update("maps", { items });
await database.table("maps").bulkDelete(ids);
await database.table("states").bulkDelete(ids);
await database.table("assets").bulkDelete(assetIds);

View File

@ -5,6 +5,7 @@ import { useAuth } from "./AuthContext";
import { useDatabase } from "./DatabaseContext";
import { applyObservableChange } from "../helpers/dexie";
import { removeGroupsItems } from "../helpers/group";
const TokenDataContext = React.createContext();
@ -73,6 +74,11 @@ export function TokenDataProvider({ children }) {
assetIds.push(token.thumbnail);
}
}
const group = await database.table("groups").get("tokens");
let items = removeGroupsItems(group.items, ids);
await database.table("groups").update("tokens", { items });
await database.table("tokens").bulkDelete(ids);
await database.table("assets").bulkDelete(assetIds);
},

View File

@ -236,3 +236,31 @@ export function renameGroup(groups, groupId, newName) {
}
return newGroups;
}
/**
* Remove items from groups including sub groups
* @param {Group[]} groups
* @param {string[]} itemIds
*/
export function removeGroupsItems(groups, itemIds) {
let newGroups = cloneDeep(groups);
for (let i = newGroups.length - 1; i >= 0; i--) {
const group = newGroups[i];
if (group.type === "item") {
if (itemIds.includes(group.id)) {
newGroups.splice(i, 1);
}
} else {
const items = group.items;
for (let j = items.length - 1; j >= 0; j--) {
const item = items[j];
if (itemIds.includes(item.id)) {
newGroups[i].items.splice(j, 1);
}
}
}
}
return newGroups;
}