modifications par rapport aux consignes

master
Hugo PRADIER 10 months ago
parent 710f0b2c10
commit a0518086cf

@ -3,9 +3,25 @@
<br /> <br />
<h2>Commandes en cours</h2> <h2>Commandes en cours</h2>
<ul> <ul>
<li *ngFor="let recipe of currentOrders"> <li
{{ recipe.name }} *ngFor="let recipe of currentOrders; let i = index"
(click)="onSelectRecipe(recipe)"
style="cursor: pointer"
>
{{ i + 1 }}. {{ recipe.name }}
</li> </li>
</ul> </ul>
<p>Bienvenue sur Recipe</p> <h3>Bienvenue sur Recipe</h3>
<p>
Nous vous invitons à naviguer sur notre application en utilisant le menu
déroulant en haut à droite.
</p>
<p [ngStyle]="{ color: isLoggedIn ? 'green' : 'red' }">
Vous êtes
{{
isLoggedIn
? "connecté"
: "déconnecté et n'avez donc pas accès à la liste des ingredients"
}}.
</p>
</div> </div>

@ -2,21 +2,32 @@ import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { Recipe } from '../models/recipe'; import { Recipe } from '../models/recipe';
import { RecipeService } from '../services/recipe.service'; import { RecipeService } from '../services/recipe.service';
import { AuthService } from '../services/auth.service';
import { Router } from '@angular/router';
@Component({ @Component({
selector: 'app-home', selector: 'app-home',
standalone: true, standalone: true,
imports: [CommonModule], imports: [CommonModule],
templateUrl: './home.component.html', templateUrl: './home.component.html',
styleUrl: './home.component.scss' styleUrl: './home.component.scss',
}) })
export class HomeComponent implements OnInit { export class HomeComponent implements OnInit {
currentOrders: Recipe[] = []; currentOrders: Recipe[] = [];
isLoggedIn: boolean = false;
constructor(private recipeService: RecipeService) {} constructor(
private recipeService: RecipeService,
private authService: AuthService,
private router: Router
) {}
ngOnInit(): void { ngOnInit(): void {
// Charger les commandes en cours depuis le service
this.currentOrders = this.recipeService.getCurrentOrders(); this.currentOrders = this.recipeService.getCurrentOrders();
this.isLoggedIn = this.authService.isLoggedIn();
}
onSelectRecipe(recipe: Recipe): void {
this.router.navigate(['/recipe', recipe.id]);
} }
} }

@ -19,7 +19,7 @@ import { IngredientRecipe } from '../models/ingredient-recipe';
FormsModule, FormsModule,
MatButtonModule, MatButtonModule,
MatFormFieldModule, MatFormFieldModule,
MatSelectModule MatSelectModule,
], ],
templateUrl: './recipe-add.component.html', templateUrl: './recipe-add.component.html',
styleUrls: ['./recipe-add.component.scss'], styleUrls: ['./recipe-add.component.scss'],
@ -30,8 +30,8 @@ export class RecipeAddComponent implements OnInit {
ingredientQuantities: { [key: number]: number } = {}; ingredientQuantities: { [key: number]: number } = {};
selectedIngredient: number | null = null; selectedIngredient: number | null = null;
imageError: string | null = null; imageError: string | null = null;
maxDescriptionLength: number = 200; // Maximum length for description maxDescriptionLength: number = 200;
isSubmitting: boolean = false; // Flag to prevent multiple submissions isSubmitting: boolean = false;
constructor( constructor(
private ingredientService: IngredientService, private ingredientService: IngredientService,
@ -68,9 +68,12 @@ export class RecipeAddComponent implements OnInit {
} }
addIngredient(): void { addIngredient(): void {
if (this.selectedIngredient !== null && !this.ingredientQuantities[this.selectedIngredient]) { if (
this.ingredientQuantities[this.selectedIngredient] = 1; // Default quantity this.selectedIngredient !== null &&
this.selectedIngredient = null; // Clear the selection !this.ingredientQuantities[this.selectedIngredient]
) {
this.ingredientQuantities[this.selectedIngredient] = 1;
this.selectedIngredient = null;
} }
} }
@ -79,44 +82,46 @@ export class RecipeAddComponent implements OnInit {
} }
getIngredientKeys(): number[] { 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 { 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 : ''; return ingredient ? ingredient.name : '';
} }
onSubmit(): void { 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(); const recipes = this.recipeService.getRecipes();
// Set the new recipe ID
this.recipe.id = recipes.length ? recipes[recipes.length - 1].id + 1 : 1; 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); const ingredient = this.ingredientService.getIngredient(id);
return new Ingredient(id, ingredient.name); return new Ingredient(id, ingredient.name);
}); });
// Save the recipe
this.recipeService.addRecipe(this.recipe); this.recipeService.addRecipe(this.recipe);
// Save the ingredient quantities this.getIngredientKeys().forEach((id) => {
this.getIngredientKeys().forEach(id => {
const quantity = this.ingredientQuantities[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); this.recipeService.addIngredientRecipe(ingredientRecipe);
}); });
// Navigate back to the list
this.router.navigate(['/list']).then(() => { this.router.navigate(['/list']).then(() => {
this.isSubmitting = false; // Reset flag after navigation this.isSubmitting = false;
}); });
} }
} }

@ -55,5 +55,6 @@ export class RecipeListComponent implements OnInit {
onOrderRecipe(recipe: Recipe): void { onOrderRecipe(recipe: Recipe): void {
this.recipeService.orderRecipe(recipe); this.recipeService.orderRecipe(recipe);
this.router.navigate(['/home']);
} }
} }

@ -23,17 +23,20 @@ export class RecipeService {
deleteRecipe(recipeId: number): void { deleteRecipe(recipeId: number): void {
let recipes = this.getRecipes(); let recipes = this.getRecipes();
const deletedRecipe = recipes.find(recipe => recipe.id === recipeId); const deletedRecipe = recipes.find((recipe) => recipe.id === recipeId);
if (deletedRecipe) { 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)); localStorage.setItem(this.localStorageKey, JSON.stringify(recipes));
// Remove associated IngredientRecipe objects
let ingredientRecipes = this.getIngredientRecipes(recipeId); let ingredientRecipes = this.getIngredientRecipes(recipeId);
ingredientRecipes = ingredientRecipes.filter(ir => ir.idRecipe !== recipeId); ingredientRecipes = ingredientRecipes.filter(
localStorage.setItem(this.ingredientRecipeKey, JSON.stringify(ingredientRecipes)); (ir) => ir.idRecipe !== recipeId
);
localStorage.setItem(
this.ingredientRecipeKey,
JSON.stringify(ingredientRecipes)
);
} }
} }
@ -45,12 +48,20 @@ export class RecipeService {
} }
addIngredientRecipe(ingredientRecipe: IngredientRecipe): void { addIngredientRecipe(ingredientRecipe: IngredientRecipe): void {
const ingredientRecipes = this.getIngredientRecipes(ingredientRecipe.idRecipe); const ingredientRecipes = this.getIngredientRecipes(
ingredientRecipe.idRecipe
);
ingredientRecipes.push(ingredientRecipe); 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( const ingredientRecipe = ingredientRecipes.find(
(ir) => ir.idIngredient === ingredientId (ir) => ir.idIngredient === ingredientId
); );
@ -58,7 +69,9 @@ export class RecipeService {
} }
orderRecipe(recipe: Recipe): void { 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); currentOrders.push(recipe);
localStorage.setItem(this.currentOrdersKey, JSON.stringify(currentOrders)); localStorage.setItem(this.currentOrdersKey, JSON.stringify(currentOrders));
} }

Loading…
Cancel
Save