diff --git a/angular.json b/angular.json
index 129f743..1f4bf67 100644
--- a/angular.json
+++ b/angular.json
@@ -51,7 +51,13 @@
"development": {
"optimization": false,
"extractLicenses": false,
- "sourceMap": true
+ "sourceMap": true,
+ "fileReplacements": [
+ {
+ "replace": "src/environments/environment.ts",
+ "with": "src/environments/environment.development.ts"
+ }
+ ]
}
},
"defaultConfiguration": "production"
diff --git a/src/app/recipe.service.ts b/src/app/recipe.service.ts
index 6a28ff7..3bc997f 100644
--- a/src/app/recipe.service.ts
+++ b/src/app/recipe.service.ts
@@ -1,5 +1,6 @@
import { Injectable } from '@angular/core';
import { Ingredient, Recipe } from '../cookbook/type';
+import { environment } from '../environments/environment';
@Injectable({
providedIn: 'root',
@@ -36,6 +37,23 @@ export class RecipeService {
{ id: 2, name: 'Farine' },
];
+ constructor() {
+ if (environment.API_URL) {
+ fetch(environment.API_URL)
+ .then((response) => response.json())
+ .then((data) => {
+ for (const ingredient of data) {
+ ingredient.id = parseInt(ingredient.id);
+ }
+ this.#ingredients.push(...data);
+ });
+ }
+ const localRecipes = localStorage.getItem('recipes');
+ if (localRecipes) {
+ this.#recipes = JSON.parse(localRecipes);
+ }
+ }
+
getAll(): Recipe[] {
return this.#recipes;
}
@@ -58,6 +76,7 @@ export class RecipeService {
id,
...recipe,
});
+ this.syncRecipes();
}
edit(recipe: Recipe): void {
@@ -66,6 +85,21 @@ export class RecipeService {
this.#recipes[i] = recipe;
}
}
+ this.syncRecipes();
+ }
+
+ delete(recipe: Recipe): boolean {
+ const index = this.#recipes.findIndex((v) => v.id === recipe.id);
+ if (index === -1) {
+ return false;
+ }
+ this.#recipes.splice(index, 1);
+ this.syncRecipes();
+ return true;
+ }
+
+ syncRecipes() {
+ localStorage.setItem('recipes', JSON.stringify(this.#recipes));
}
addIngredient(ingredient: Omit