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) { diff --git a/src/app/recipe/recipe.component.html b/src/app/recipe/recipe.component.html index e00b1ee..b2931db 100644 --- a/src/app/recipe/recipe.component.html +++ b/src/app/recipe/recipe.component.html @@ -34,6 +34,10 @@

+ +
+ +
diff --git a/src/app/recipe/recipe.component.ts b/src/app/recipe/recipe.component.ts index 3496ff0..06b1e34 100644 --- a/src/app/recipe/recipe.component.ts +++ b/src/app/recipe/recipe.component.ts @@ -2,7 +2,7 @@ import { CommonModule } from '@angular/common'; import { Component, Input } from '@angular/core'; import { MatButtonModule } from '@angular/material/button'; import { MatIconModule } from '@angular/material/icon'; -import { RouterLink } from '@angular/router'; +import { Router, RouterLink } from '@angular/router'; import { Recipe } from '../../cookbook/type'; import { RecipeService } from '../recipe.service'; @@ -20,6 +20,11 @@ export class RecipeComponent { this.recipe = this.recipes.get(parseInt(id))!; } - constructor(protected recipes: RecipeService) { + constructor(protected recipes: RecipeService, private router: Router) { + } + + delete(): void { + this.recipes.delete(this.recipe); + this.router.navigateByUrl('/recipes'); } } diff --git a/src/environments/environment.development.ts b/src/environments/environment.development.ts new file mode 100644 index 0000000..7523e5d --- /dev/null +++ b/src/environments/environment.development.ts @@ -0,0 +1,3 @@ +export const environment = { + API_URL: 'https://664ba07f35bbda10987d9f99.mockapi.io/api/ingredients', +}; diff --git a/src/environments/environment.ts b/src/environments/environment.ts new file mode 100644 index 0000000..7523e5d --- /dev/null +++ b/src/environments/environment.ts @@ -0,0 +1,3 @@ +export const environment = { + API_URL: 'https://664ba07f35bbda10987d9f99.mockapi.io/api/ingredients', +};