modifications par rapport aux consignes

master
Hugo PRADIER 10 months ago
parent 710f0b2c10
commit a0518086cf

@ -3,9 +3,25 @@
<br />
<h2>Commandes en cours</h2>
<ul>
<li *ngFor="let recipe of currentOrders">
{{ recipe.name }}
<li
*ngFor="let recipe of currentOrders; let i = index"
(click)="onSelectRecipe(recipe)"
style="cursor: pointer"
>
{{ i + 1 }}. {{ recipe.name }}
</li>
</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>

@ -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]);
}
}

@ -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;
});
}
}

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

@ -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));
}

Loading…
Cancel
Save