diff --git a/src/app/services/recipe.service.ts b/src/app/services/recipe.service.ts index 25de6d0..5f164b9 100644 --- a/src/app/services/recipe.service.ts +++ b/src/app/services/recipe.service.ts @@ -22,8 +22,18 @@ export class RecipeService { deleteRecipe(recipeId: number): void { let recipes = this.getRecipes(); - recipes = recipes.filter(recipe => recipe.id !== recipeId); - localStorage.setItem(this.localStorageKey, JSON.stringify(recipes)); + const deletedRecipe = recipes.find(recipe => recipe.id === recipeId); + + if (deletedRecipe) { + // Remove the recipe from recipes array + recipes = recipes.filter(recipe => recipe.id !== recipeId); + localStorage.setItem(this.localStorageKey, JSON.stringify(recipes)); + + // Remove associated IngredientRecipe objects + let ingredientRecipes = this.getIngredientRecipes(recipeId); + ingredientRecipes = ingredientRecipes.filter(ir => ir.idRecipe !== recipeId); + localStorage.setItem(this.ingredientRecipeKey, JSON.stringify(ingredientRecipes)); + } } getIngredientRecipes(recipeId: number): IngredientRecipe[] {