diff --git a/src/app/home/home.component.html b/src/app/home/home.component.html
index 4a852a8..44d57c1 100644
--- a/src/app/home/home.component.html
+++ b/src/app/home/home.component.html
@@ -3,9 +3,25 @@
Bienvenue sur Recipe
++ Nous vous invitons à naviguer sur notre application en utilisant le menu + déroulant en haut à droite. +
++ Vous êtes + {{ + isLoggedIn + ? "connecté" + : "déconnecté et n'avez donc pas accès à la liste des ingredients" + }}. +
diff --git a/src/app/home/home.component.ts b/src/app/home/home.component.ts index afcc3ff..5880e5f 100644 --- a/src/app/home/home.component.ts +++ b/src/app/home/home.component.ts @@ -2,21 +2,32 @@ import { Component, OnInit } from '@angular/core'; import { CommonModule } from '@angular/common'; import { Recipe } from '../models/recipe'; import { RecipeService } from '../services/recipe.service'; +import { AuthService } from '../services/auth.service'; +import { Router } from '@angular/router'; @Component({ selector: 'app-home', standalone: true, imports: [CommonModule], templateUrl: './home.component.html', - styleUrl: './home.component.scss' + styleUrl: './home.component.scss', }) export class HomeComponent implements OnInit { currentOrders: Recipe[] = []; + isLoggedIn: boolean = false; - constructor(private recipeService: RecipeService) {} + constructor( + private recipeService: RecipeService, + private authService: AuthService, + private router: Router + ) {} ngOnInit(): void { - // Charger les commandes en cours depuis le service this.currentOrders = this.recipeService.getCurrentOrders(); + this.isLoggedIn = this.authService.isLoggedIn(); + } + + onSelectRecipe(recipe: Recipe): void { + this.router.navigate(['/recipe', recipe.id]); } } diff --git a/src/app/recipe-add/recipe-add.component.ts b/src/app/recipe-add/recipe-add.component.ts index c536e5e..c738934 100644 --- a/src/app/recipe-add/recipe-add.component.ts +++ b/src/app/recipe-add/recipe-add.component.ts @@ -19,7 +19,7 @@ import { IngredientRecipe } from '../models/ingredient-recipe'; FormsModule, MatButtonModule, MatFormFieldModule, - MatSelectModule + MatSelectModule, ], templateUrl: './recipe-add.component.html', styleUrls: ['./recipe-add.component.scss'], @@ -30,8 +30,8 @@ export class RecipeAddComponent implements OnInit { ingredientQuantities: { [key: number]: number } = {}; selectedIngredient: number | null = null; imageError: string | null = null; - maxDescriptionLength: number = 200; // Maximum length for description - isSubmitting: boolean = false; // Flag to prevent multiple submissions + maxDescriptionLength: number = 200; + isSubmitting: boolean = false; constructor( private ingredientService: IngredientService, @@ -68,9 +68,12 @@ export class RecipeAddComponent implements OnInit { } addIngredient(): void { - if (this.selectedIngredient !== null && !this.ingredientQuantities[this.selectedIngredient]) { - this.ingredientQuantities[this.selectedIngredient] = 1; // Default quantity - this.selectedIngredient = null; // Clear the selection + if ( + this.selectedIngredient !== null && + !this.ingredientQuantities[this.selectedIngredient] + ) { + this.ingredientQuantities[this.selectedIngredient] = 1; + this.selectedIngredient = null; } } @@ -79,44 +82,46 @@ export class RecipeAddComponent implements OnInit { } getIngredientKeys(): number[] { - return Object.keys(this.ingredientQuantities).map(key => parseInt(key, 10)); + return Object.keys(this.ingredientQuantities).map((key) => + parseInt(key, 10) + ); } findIngredientName(id: number): string { - const ingredient = this.ingredients.find(ingredient => ingredient.id === id); + const ingredient = this.ingredients.find( + (ingredient) => ingredient.id === id + ); return ingredient ? ingredient.name : ''; } onSubmit(): void { - if (this.isSubmitting) return; // Prevent multiple submissions + if (this.isSubmitting) return; - this.isSubmitting = true; // Set flag to prevent multiple submissions + this.isSubmitting = true; - // Get the current recipes const recipes = this.recipeService.getRecipes(); - // Set the new recipe ID this.recipe.id = recipes.length ? recipes[recipes.length - 1].id + 1 : 1; - // Add the ingredients with quantities - this.recipe.ingredients = this.getIngredientKeys().map(id => { + this.recipe.ingredients = this.getIngredientKeys().map((id) => { const ingredient = this.ingredientService.getIngredient(id); return new Ingredient(id, ingredient.name); }); - // Save the recipe this.recipeService.addRecipe(this.recipe); - // Save the ingredient quantities - this.getIngredientKeys().forEach(id => { + this.getIngredientKeys().forEach((id) => { const quantity = this.ingredientQuantities[id]; - const ingredientRecipe = new IngredientRecipe(id, this.recipe.id, quantity); + const ingredientRecipe = new IngredientRecipe( + id, + this.recipe.id, + quantity + ); this.recipeService.addIngredientRecipe(ingredientRecipe); }); - // Navigate back to the list this.router.navigate(['/list']).then(() => { - this.isSubmitting = false; // Reset flag after navigation + this.isSubmitting = false; }); } } diff --git a/src/app/recipe-list/recipe-list.component.ts b/src/app/recipe-list/recipe-list.component.ts index 08e4da0..a1eef9a 100644 --- a/src/app/recipe-list/recipe-list.component.ts +++ b/src/app/recipe-list/recipe-list.component.ts @@ -55,5 +55,6 @@ export class RecipeListComponent implements OnInit { onOrderRecipe(recipe: Recipe): void { this.recipeService.orderRecipe(recipe); + this.router.navigate(['/home']); } } diff --git a/src/app/services/recipe.service.ts b/src/app/services/recipe.service.ts index 55c562e..6a2a474 100644 --- a/src/app/services/recipe.service.ts +++ b/src/app/services/recipe.service.ts @@ -23,17 +23,20 @@ export class RecipeService { deleteRecipe(recipeId: number): void { let recipes = this.getRecipes(); - const deletedRecipe = recipes.find(recipe => recipe.id === recipeId); + const deletedRecipe = recipes.find((recipe) => recipe.id === recipeId); if (deletedRecipe) { - // Remove the recipe from recipes array - recipes = recipes.filter(recipe => recipe.id !== recipeId); + 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)); + ingredientRecipes = ingredientRecipes.filter( + (ir) => ir.idRecipe !== recipeId + ); + localStorage.setItem( + this.ingredientRecipeKey, + JSON.stringify(ingredientRecipes) + ); } } @@ -45,12 +48,20 @@ export class RecipeService { } addIngredientRecipe(ingredientRecipe: IngredientRecipe): void { - const ingredientRecipes = this.getIngredientRecipes(ingredientRecipe.idRecipe); + const ingredientRecipes = this.getIngredientRecipes( + ingredientRecipe.idRecipe + ); ingredientRecipes.push(ingredientRecipe); - localStorage.setItem(this.ingredientRecipeKey, JSON.stringify(ingredientRecipes)); + localStorage.setItem( + this.ingredientRecipeKey, + JSON.stringify(ingredientRecipes) + ); } - getIngredientQuantity(ingredientId: number, ingredientRecipes: IngredientRecipe[]): number { + getIngredientQuantity( + ingredientId: number, + ingredientRecipes: IngredientRecipe[] + ): number { const ingredientRecipe = ingredientRecipes.find( (ir) => ir.idIngredient === ingredientId ); @@ -58,7 +69,9 @@ export class RecipeService { } orderRecipe(recipe: Recipe): void { - let currentOrders: Recipe[] = JSON.parse(localStorage.getItem(this.currentOrdersKey) || '[]'); + let currentOrders: Recipe[] = JSON.parse( + localStorage.getItem(this.currentOrdersKey) || '[]' + ); currentOrders.push(recipe); localStorage.setItem(this.currentOrdersKey, JSON.stringify(currentOrders)); }