Skip to content

Commit

Permalink
Fix: Deleting components does not remove their references 285 (#286)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Potts <[email protected]>
  • Loading branch information
misterpotts authored Feb 20, 2024
1 parent 46c9b86 commit 0ce19cc
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 47 deletions.
2 changes: 1 addition & 1 deletion docs/_config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
title: Fabricate 0.10.20
title: Fabricate 0.10.21
email: [email protected]
description: >-
End user documentation for the Foundry Virtual Tabletop (VTT) Module, "Fabricate".
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fabricate",
"version": "0.10.20",
"version": "0.10.21",
"description": "A system-agnostic, flexible crafting module for FoundryVT",
"main": "index.js",
"type": "module",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,29 @@
import {EssencesStore} from "../stores/EssenceStore";
import EssenceEditorComponent from "./essenceManager/EssenceEditorComponent.svelte";
import {EssenceEditor} from "./essenceManager/EssenceEditor";
import type {LocalizationService} from "../common/LocalizationService";
import type {FabricateAPI} from "../../scripts/api/FabricateAPI";
export let localization;
export let fabricateAPI;
export let localization: LocalizationService;
export let fabricateAPI: FabricateAPI;
const localizationPath = `${Properties.module.id}.CraftingSystemManagerApp`
const craftingSystems = new CraftingSystemsStore({});
const selectedCraftingSystem = new SelectedCraftingSystemStore({ craftingSystems });
const essences = new EssencesStore({ selectedCraftingSystem, fabricateAPI });
const essenceEditor = new EssenceEditor({ fabricateAPI, essences, localization });
const components = new ComponentsStore({ selectedCraftingSystem, fabricateAPI, initialValue: [] });
const componentEditor = new CraftingComponentEditor({ fabricateAPI, components: components, localization });
const recipes = new RecipesStore({ selectedCraftingSystem, fabricateAPI });
const essenceEditor = new EssenceEditor({ fabricateAPI, essences, localization, components, recipes });
const componentEditor = new CraftingComponentEditor({ fabricateAPI, components: components, localization, recipes });
const recipeEditor = new RecipeEditor({ fabricateAPI, recipes, components, localization });
const selectedRecipe = new SelectedRecipeStore({recipes: recipes});
const selectedComponent = new SelectedCraftingComponentStore({craftingComponents: components});
const craftingSystemEditor = new CraftingSystemEditor({fabricateAPI, craftingSystems, localization, components: components });
const craftingSystemEditor = new CraftingSystemEditor({fabricateAPI, craftingSystems, localization, components, recipes });
setContext(key, {
craftingSystems,
Expand Down
20 changes: 14 additions & 6 deletions src/applications/craftingSystemManagerApp/CraftingSystemEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import {LocalizationService} from "../common/LocalizationService";
import {FabricateAPI} from "../../scripts/api/FabricateAPI";
import {FabricateExportModel} from "../../scripts/repository/import/FabricateExportModel";
import {Component} from "../../scripts/crafting/component/Component";
import {ComponentsStore} from "../stores/ComponentsStore";
import {RecipesStore} from "../stores/RecipesStore";

class CraftingSystemEditor {

private readonly _craftingSystems: Writable<CraftingSystem[]>;
private readonly _components: Writable<Component[]>;
private readonly _components: ComponentsStore;
private readonly _recipes: RecipesStore;
private readonly _localization: LocalizationService;
private readonly _fabricateAPI: FabricateAPI;

Expand All @@ -18,16 +21,19 @@ class CraftingSystemEditor {
constructor({
craftingSystems,
components,
recipes,
localization,
fabricateAPI,
}: {
craftingSystems: Writable<CraftingSystem[]>;
components: Writable<Component[]>;
components: ComponentsStore;
recipes: RecipesStore;
localization: LocalizationService;
fabricateAPI: FabricateAPI;
}) {
this._fabricateAPI = fabricateAPI;
this._components = components;
this._recipes = recipes;
this._craftingSystems = craftingSystems;
this._localization = localization;
}
Expand All @@ -50,6 +56,12 @@ class CraftingSystemEditor {
this._craftingSystems.update((craftingSystems) => {
return craftingSystems.filter(craftingSystem => craftingSystem.id !== craftingSystemToDelete.id);
});
this._components.update((components => {
return components.filter(component => component.craftingSystemId !== craftingSystemToDelete.id);
}));
this._recipes.update((recipes => {
return recipes.filter(recipe => recipe.craftingSystemId !== craftingSystemToDelete.id);
}));
}
});
}
Expand Down Expand Up @@ -169,10 +181,6 @@ class CraftingSystemEditor {
return updatedCraftingSystem;
}

async deleteComponent(component: Component): Promise<Component> {
return this._fabricateAPI.components.deleteById(component.id);
}

async saveComponent(craftingComponent: Component): Promise<Component> {
const updatedComponent = await this._fabricateAPI.components.save(craftingComponent);
this._components.update((components) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import type {CraftingSystem} from "../../../scripts/crafting/system/CraftingSystem";
import {CraftingComponentEditor} from "./CraftingComponentEditor";
import type {Essence} from "../../../scripts/crafting/essence/Essence";
import {ComponentsStore} from "../../stores/ComponentsStore";
const localizationPath = `${Properties.module.id}.CraftingSystemManagerApp.tabs.components`;
let selectSalvageTab;
Expand All @@ -33,7 +34,7 @@
}: {
localization: LocalizationService,
selectedComponent: Readable<Component>,
components: Readable<Component[]>,
components: ComponentsStore,
selectedCraftingSystem: Readable<CraftingSystem>,
componentEditor: CraftingComponentEditor,
essences: Readable<Essence[]>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,31 @@ import {LocalizationService} from "../../common/LocalizationService";
import {CraftingSystem} from "../../../scripts/crafting/system/CraftingSystem";
import {FabricateAPI} from "../../../scripts/api/FabricateAPI";
import {ComponentsStore} from "../../stores/ComponentsStore";
import {RecipesStore} from "../../stores/RecipesStore";

class CraftingComponentEditor {

private readonly _fabricateAPI: FabricateAPI;
private readonly _components: ComponentsStore;
private readonly _recipes: RecipesStore;
private readonly _localization: LocalizationService;
private readonly _localizationPath = `${Properties.module.id}.CraftingSystemManagerApp.tabs.components`;

constructor({
localization,
fabricateAPI,
components
components,
recipes
}: {
localization: LocalizationService;
fabricateAPI: FabricateAPI;
components: ComponentsStore;
recipes: RecipesStore;
}) {
this._localization = localization;
this._fabricateAPI = fabricateAPI;
this._components = components;
this._recipes = recipes;
}

public async importComponent(event: any, selectedSystem: CraftingSystem) {
Expand Down Expand Up @@ -110,7 +115,17 @@ class CraftingComponentEditor {
return undefined;
}
const deletedComponent = await this._fabricateAPI.components.deleteById(component.id);
this._components.remove(deletedComponent);
const modifiedComponents = await this._fabricateAPI.components.removeSalvageReferences(component.id, component.craftingSystemId);
const modifiedComponentsById = new Map(modifiedComponents.map(component => [component.id, component]));
const modifiedRecipes = await this._fabricateAPI.recipes.removeComponentReferences(component.id, component.craftingSystemId);
const modifiedRecipesById = new Map(modifiedRecipes.map(recipe => [recipe.id, recipe]));
this._components.update((components) => {
return components.filter(component => component.id !== deletedComponent.id)
.map(component => modifiedComponentsById.get(component.id) || component);
});
this._recipes.update((recipes) => {
return recipes.map(recipe => modifiedRecipesById.get(recipe.id) || recipe);
});
}

public async saveComponent(craftingComponent: Component): Promise<Component> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,35 @@ import {DefaultDocumentManager} from "../../../scripts/foundry/DocumentManager";
import Properties from "../../../scripts/Properties";
import {FabricateAPI} from "../../../scripts/api/FabricateAPI";
import {EssencesStore} from "../../stores/EssenceStore";
import {ComponentsStore} from "../../stores/ComponentsStore";
import {RecipesStore} from "../../stores/RecipesStore";

class EssenceEditor {

private readonly _essences: EssencesStore;
private readonly _components: ComponentsStore;
private readonly _recipes: RecipesStore;
private readonly _fabricateAPI: FabricateAPI;
private readonly _localization: LocalizationService;

private readonly _localizationPath = `${Properties.module.id}.CraftingSystemManagerApp.tabs.essences`;

constructor({
essences,
components,
recipes,
fabricateAPI,
localization,
}: {
essences: EssencesStore;
components: ComponentsStore;
recipes: RecipesStore;
fabricateAPI: FabricateAPI;
localization: LocalizationService;
}) {
this._essences = essences;
this._components = components;
this._recipes = recipes;
this._fabricateAPI = fabricateAPI;
this._localization = localization;
}
Expand Down Expand Up @@ -60,18 +70,35 @@ class EssenceEditor {
if (!doDelete) {
return;
}
await this._fabricateAPI.essences.deleteById(essence.id);
this._essences.remove(essence);
const deletedEssence = await this._fabricateAPI.essences.deleteById(essence.id);
const modifiedComponents = await this._fabricateAPI.components.removeEssenceReferences(essence.id, essence.craftingSystemId);
const modifiedComponentsById = new Map(modifiedComponents.map(component => [component.id, component]));
const modifiedRecipes = await this._fabricateAPI.recipes.removeEssenceReferences(essence.id, essence.craftingSystemId);
const modifiedRecipesById = new Map(modifiedRecipes.map(recipe => [recipe.id, recipe]));
this._essences.remove(deletedEssence);
this._components.update((components) => {
return components.map(component => modifiedComponentsById.get(component.id) || component);
});
this._recipes.update((recipes) => {
return recipes.map(recipe => modifiedRecipesById.get(recipe.id) || recipe);
});
}

public async setActiveEffectSource(event: any, essence: Essence) {
const dropEventParser = new DropEventParser({
localizationService: this._localization,
documentManager: new DefaultDocumentManager(),
partType: this._localization.localize(`${Properties.module.id}.typeNames.activeEffectSource.singular`)
documentManager: new DefaultDocumentManager()
})
const dropData = await dropEventParser.parse(event);
essence.activeEffectSource = dropData.itemData;
const dropEvent = await dropEventParser.parse(event);
if (dropEvent.type === "Unknown") {
return;
}
if (dropEvent.type === "Compendium") {
return;
}
if (dropEvent.type === "Item") {
essence.activeEffectSource = dropEvent.data.item;
}
await this._fabricateAPI.essences.save(essence);
this._essences.insert(essence);
return essence;
Expand Down
Loading

0 comments on commit 0ce19cc

Please sign in to comment.