Skip to content

Commit

Permalink
defect: Can't rename Options #118 (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
misterpotts authored Mar 23, 2023
1 parent 1b7e3f5 commit 3049178
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 11 deletions.
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.8.6",
"version": "0.8.7",
"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 @@ -104,8 +104,26 @@ class CraftingComponentManager {
}

public async saveComponent(craftingComponent: CraftingComponent, selectedSystem: CraftingSystem) {
selectedSystem.editComponent(craftingComponent);
await this._craftingSystemEditor.saveCraftingSystem(selectedSystem);
if (this.validateOptionNames(craftingComponent)) {
selectedSystem.editComponent(craftingComponent);
await this._craftingSystemEditor.saveCraftingSystem(selectedSystem);
return;
}
const message = this._localization.format(`${this._localizationPath}.component.errors.optionNotUnique`, { componentName: craftingComponent.name });
ui.notifications.error(message);
}

private validateOptionNames(component: CraftingComponent) {
let valid = true;
component.salvageOptions
.map(salvageOption => salvageOption.name)
.forEach((value, index, array) => {
if (array.indexOf(value) !== index) {
valid = false;
console.error(`The salvage option name ${value} is not unique.`);
}
});
return valid;
}

public async duplicateComponent(craftingComponent: CraftingComponent, selectedSystem: CraftingSystem): Promise<CraftingComponent> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,13 @@
{#if $selectedRecipe.hasIngredients}
<Tabs bind:selectPreviousTab={selectPreviousTab}>
<TabList>
{#each sortByName($selectedRecipe.ingredientOptions) as ingredientOption}
{#each $selectedRecipe.ingredientOptions as ingredientOption}
<Tab>{ingredientOption.name}</Tab>
{/each}
<Tab><i class="fa-regular fa-square-plus"></i> {localization.localize(`${localizationPath}.recipe.labels.newIngredientOption`)}</Tab>
</TabList>

{#each sortByName($selectedRecipe.ingredientOptions) as ingredientOption}
{#each $selectedRecipe.ingredientOptions as ingredientOption}
<TabPanel class="fab-columns">
<div class="fab-column">
<div class="fab-option-controls fab-row">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,34 @@ class RecipeManager {
}

public async saveRecipe(recipe: Recipe, selectedSystem: CraftingSystem) {
selectedSystem.editRecipe(recipe);
await this._craftingSystemEditor.saveCraftingSystem(selectedSystem);
if (this.validateOptionNames(recipe)) {
selectedSystem.editRecipe(recipe);
await this._craftingSystemEditor.saveCraftingSystem(selectedSystem);
return;
}
const message = this._localization.format(`${this._localizationPath}.recipe.errors.optionNotUnique`, { recipeName: recipe.name });
ui.notifications.error(message);
}

private validateOptionNames(recipe: Recipe) {
let valid = true;
recipe.ingredientOptions
.map(ingredientOption => ingredientOption.name)
.forEach((value, index, array) => {
if (array.indexOf(value) !== index) {
valid = false;
console.error(`The ingredient option name ${value} is not unique.`);
}
});
recipe.resultOptions
.map(ingredientOption => ingredientOption.name)
.forEach((value, index, array) => {
if (array.indexOf(value) !== index) {
valid = false;
console.error(`The result option name ${value} is not unique.`);
}
});
return valid;
}

public async duplicateRecipe(recipe: Recipe, selectedSystem: CraftingSystem): Promise<Recipe> {
Expand Down
6 changes: 4 additions & 2 deletions src/public/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@
},
"errors": {
"itemNotFound": "The item does not exist or has been deleted.",
"itemNotSet": "No item configured. You can set one in the component editor."
"itemNotSet": "No item configured. You can set one in the component editor.",
"optionNotUnique": "The component {componentName} has options that are not uniquely named."
},
"labels": {
"salvageName": "Option name",
Expand Down Expand Up @@ -218,7 +219,8 @@
},
"errors": {
"itemNotFound": "The item does not exist or has been deleted.",
"itemNotSet": "No item configured. You can set one in the recipe editor."
"itemNotSet": "No item configured. You can set one in the recipe editor.",
"optionNotUnique": "The recipe {recipeName} has options that are not uniquely named."
},
"imported": "Imported recipe {recipeName} into {systemName}",
"deleted": "Removed recipe {recipeName} from {systemName}",
Expand Down
2 changes: 1 addition & 1 deletion src/public/module.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "fabricate",
"title": "Fabricate",
"version": "0.8.6",
"version": "0.8.7",
"description": "A system-agnostic, flexible crafting module for FoundryVTT",
"authors": [
{
Expand Down
6 changes: 5 additions & 1 deletion src/scripts/common/Recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class IngredientOption implements Identifiable, Serializable<IngredientOptionJso

private _catalysts: Combination<CraftingComponent>;
private _ingredients: Combination<CraftingComponent>;
private readonly _name: string;
private _name: string;

constructor({
name,
Expand Down Expand Up @@ -68,6 +68,10 @@ class IngredientOption implements Identifiable, Serializable<IngredientOptionJso
return this._name;
}

set name(value: string) {
this._name = value;
}

get id(): string {
return this._name;
}
Expand Down
3 changes: 3 additions & 0 deletions src/scripts/common/SelectableOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class SelectableOptions<J, T extends Identifiable & Serializable<J>> implements
toJson(): Record<string, J> {
return Array.from(this._options.values())
.reduce((previousValue, currentValue) => {
if (previousValue[currentValue.id]) {
throw new Error("Two options cannot have the same identity. ");
}
previousValue[currentValue.id] = currentValue.toJson();
return previousValue;
}, <Record<string, J>>{});
Expand Down

0 comments on commit 3049178

Please sign in to comment.