diff --git a/angular.json b/angular.json index f1bf635..83d3f9e 100644 --- a/angular.json +++ b/angular.json @@ -122,5 +122,8 @@ } } } + }, + "cli": { + "analytics": false } } diff --git a/src/app/accueil/accueil.component.ts b/src/app/accueil/accueil.component.ts deleted file mode 100644 index c2d23ec..0000000 --- a/src/app/accueil/accueil.component.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'app-accueil', - standalone: true, - imports: [], - templateUrl: './accueil.component.html', - styleUrl: './accueil.component.scss' -}) -export class AccueilComponent { - -} diff --git a/src/app/app.component.html b/src/app/app.component.html index 0791e0c..883787d 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,4 +1,4 @@ - +
diff --git a/src/app/app.component.ts b/src/app/app.component.ts index afa5c39..4218ec5 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,18 +1,21 @@ import { Component } from '@angular/core'; import { RouterOutlet } from '@angular/router'; -import { HeaderComponent } from './header/header.component'; import { RecipeComponent } from './recipe/recipe.component'; -import { RecipeFormComponent } from './recipe-form/recipe-form.component'; import { RecipeListComponent } from './recipe-list/recipe-list.component'; +import { MenuComponent } from './menu/menu.component'; +import { RecipeAddComponent } from './recipe-add/recipe-add.component'; +import { BrowserModule } from '@angular/platform-browser'; +import { FormsModule } from '@angular/forms'; +import { CommonModule } from '@angular/common'; @Component({ selector: 'app-root', standalone: true, imports: [ RouterOutlet, - HeaderComponent, + MenuComponent, RecipeComponent, - RecipeFormComponent, + RecipeAddComponent, RecipeListComponent, ], templateUrl: './app.component.html', diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index 864ae27..99d5ec9 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -1,20 +1,14 @@ import { RouterModule, Routes } from '@angular/router'; -import { RecipeFormComponent } from './recipe-form/recipe-form.component'; import { RecipeComponent } from './recipe/recipe.component'; import { RecipeListComponent } from './recipe-list/recipe-list.component'; -import { AccueilComponent } from './accueil/accueil.component'; -import { NgModule } from '@angular/core'; +import { HomeComponent } from './home/home.component'; +import { RecipeAddComponent } from './recipe-add/recipe-add.component'; export const routes: Routes = [ - { path: 'form', component: RecipeFormComponent }, + { path: 'add', component: RecipeAddComponent }, { path: 'recipe/:id', component: RecipeComponent }, { path: 'list', component: RecipeListComponent }, - { path: '', component: AccueilComponent }, + { path: '', component: HomeComponent }, { path: '**', redirectTo: '' }, ]; - -@NgModule({ - imports: [RouterModule.forRoot(routes)], - exports: [RouterModule], -}) export class AppRoutingModule {} diff --git a/src/app/accueil/accueil.component.html b/src/app/home/home.component.html similarity index 100% rename from src/app/accueil/accueil.component.html rename to src/app/home/home.component.html diff --git a/src/app/accueil/accueil.component.scss b/src/app/home/home.component.scss similarity index 100% rename from src/app/accueil/accueil.component.scss rename to src/app/home/home.component.scss diff --git a/src/app/home/home.component.ts b/src/app/home/home.component.ts new file mode 100644 index 0000000..deb69c4 --- /dev/null +++ b/src/app/home/home.component.ts @@ -0,0 +1,12 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'app-home', + standalone: true, + imports: [], + templateUrl: './home.component.html', + styleUrl: './home.component.scss' +}) +export class HomeComponent { + +} diff --git a/src/app/header/header.component.html b/src/app/menu/menu.component.html similarity index 100% rename from src/app/header/header.component.html rename to src/app/menu/menu.component.html diff --git a/src/app/header/header.component.scss b/src/app/menu/menu.component.scss similarity index 100% rename from src/app/header/header.component.scss rename to src/app/menu/menu.component.scss diff --git a/src/app/header/header.component.ts b/src/app/menu/menu.component.ts similarity index 79% rename from src/app/header/header.component.ts rename to src/app/menu/menu.component.ts index 3a5177d..ae1fb6b 100644 --- a/src/app/header/header.component.ts +++ b/src/app/menu/menu.component.ts @@ -6,13 +6,13 @@ import { CommonModule } from '@angular/common'; import { RouterLink } from '@angular/router'; @Component({ - selector: 'app-header', + selector: 'app-menu', standalone: true, imports: [MatButtonModule, MatMenuModule, CommonModule, RouterLink], - templateUrl: './header.component.html', - styleUrl: './header.component.scss', + templateUrl: './menu.component.html', + styleUrl: './menu.component.scss', }) -export class HeaderComponent implements OnInit { +export class MenuComponent implements OnInit { menuItems: { label: string; link: string }[] | undefined; constructor(private menuService: MenuService) {} diff --git a/src/app/recipe-add/recipe-add.component.html b/src/app/recipe-add/recipe-add.component.html new file mode 100644 index 0000000..5e2d4b5 --- /dev/null +++ b/src/app/recipe-add/recipe-add.component.html @@ -0,0 +1,37 @@ +
+
+ + +
+
+ + +
+
+ + +
{{ imageError }}
+
+
+ +
+ +
diff --git a/src/app/recipe-form/recipe-form.component.scss b/src/app/recipe-add/recipe-add.component.scss similarity index 100% rename from src/app/recipe-form/recipe-form.component.scss rename to src/app/recipe-add/recipe-add.component.scss diff --git a/src/app/recipe-add/recipe-add.component.ts b/src/app/recipe-add/recipe-add.component.ts new file mode 100644 index 0000000..8e75628 --- /dev/null +++ b/src/app/recipe-add/recipe-add.component.ts @@ -0,0 +1,64 @@ +import { Component, OnInit } from '@angular/core'; +import { FormsModule } from '@angular/forms'; +import { CommonModule } from '@angular/common'; +import { IngredientService } from '../services/ingredient.service'; +import { Ingredient } from '../models/ingredient'; +import { Recipe } from '../models/recipe'; + +@Component({ + selector: 'app-recipe-add', + standalone: true, + imports: [CommonModule, FormsModule], + templateUrl: './recipe-add.component.html', + styleUrls: ['./recipe-add.component.scss'], +}) +export class RecipeAddComponent implements OnInit { + recipe: Recipe = new Recipe(0, '', '', '', []); + ingredients: Ingredient[] = []; + ingredientQuantities: { [key: number]: number } = {}; + imageError: string | null = null; + + constructor(private ingredientService: IngredientService) {} + + ngOnInit(): void { + this.ingredients = this.ingredientService.getIngredients(); + } + + onFileChange(event: any): void { + const file = event.target.files[0]; + if (file) { + const validImageTypes = [ + 'image/jpeg', + 'image/png', + 'image/gif', + 'image/jpg', + ]; + if (!validImageTypes.includes(file.type)) { + this.imageError = 'Invalid file type. Please select an image file.'; + return; + } + + this.imageError = null; + + const reader = new FileReader(); + reader.onload = () => { + this.recipe.image = reader.result as string; + }; + reader.readAsDataURL(file); + } + } + + onSubmit(): void { + this.recipe.ingredients = Object.keys(this.ingredientQuantities).map( + (id) => { + const ingredient = this.ingredientService.getIngredient(Number(id)); + return new Ingredient(Number(id), ingredient.name); + } + ); + + const recipes = JSON.parse(localStorage.getItem('recipes') || '[]'); + this.recipe.id = recipes.length ? recipes[recipes.length - 1].id + 1 : 1; + recipes.push(this.recipe); + localStorage.setItem('recipes', JSON.stringify(recipes)); + } +} diff --git a/src/app/recipe-form/recipe-form.component.html b/src/app/recipe-form/recipe-form.component.html deleted file mode 100644 index 4e4faed..0000000 --- a/src/app/recipe-form/recipe-form.component.html +++ /dev/null @@ -1 +0,0 @@ -

recipe-form works!

diff --git a/src/app/recipe-form/recipe-form.component.ts b/src/app/recipe-form/recipe-form.component.ts deleted file mode 100644 index 471bd4c..0000000 --- a/src/app/recipe-form/recipe-form.component.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'app-recipe-form', - standalone: true, - imports: [], - templateUrl: './recipe-form.component.html', - styleUrl: './recipe-form.component.scss' -}) -export class RecipeFormComponent { - -} diff --git a/src/app/recipe-list/recipe-list.component.html b/src/app/recipe-list/recipe-list.component.html index ac36a67..e1dbeb1 100644 --- a/src/app/recipe-list/recipe-list.component.html +++ b/src/app/recipe-list/recipe-list.component.html @@ -1 +1,11 @@ -

recipe-list works!

+ +
+

{{ recipe.name }}

+

{{ recipe.description }}

+ {{ recipe.name }} + +
diff --git a/src/app/recipe-list/recipe-list.component.ts b/src/app/recipe-list/recipe-list.component.ts index 70df750..d3b2de6 100644 --- a/src/app/recipe-list/recipe-list.component.ts +++ b/src/app/recipe-list/recipe-list.component.ts @@ -1,12 +1,40 @@ -import { Component } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; +import { Recipe } from '../models/recipe'; +import { Router } from '@angular/router'; +import { CommonModule } from '@angular/common'; @Component({ selector: 'app-recipe-list', standalone: true, - imports: [], templateUrl: './recipe-list.component.html', - styleUrl: './recipe-list.component.scss' + styleUrls: ['./recipe-list.component.scss'], + imports: [CommonModule], }) -export class RecipeListComponent { +export class RecipeListComponent implements OnInit { + recipes: Recipe[] = []; + constructor(private router: Router) {} + + ngOnInit(): void { + this.recipes = JSON.parse(localStorage.getItem('recipes') || '[]'); + } + + getQuantity(recipeId: number, ingredientId: number): number { + const recipe = this.recipes.find((r) => r.id === recipeId); + const ingredientRecipe = recipe?.ingredients.find( + (i) => i.id === ingredientId + ); + return ingredientRecipe + ? this.getIngredientQuantity(recipeId, ingredientId) + : 0; + } + + getIngredientQuantity(recipeId: number, ingredientId: number): number { + // Implémenter la logique pour retourner la quantité d'ingrédient + return 0; + } + + navToAddRecipe(): void { + this.router.navigate(['/add']); + } } diff --git a/src/app/services/menu.service.ts b/src/app/services/menu.service.ts index fea7315..44f21c6 100644 --- a/src/app/services/menu.service.ts +++ b/src/app/services/menu.service.ts @@ -10,9 +10,9 @@ interface MenuItem { }) export class MenuService { private menuItems: MenuItem[] = [ - { label: 'Accueil', link: '/accueil' }, - { label: 'List', link: '/list' }, - { label: 'Add', link: '/form' }, + { label: 'Home', link: '/home' }, + { label: 'List recipes', link: '/list' }, + { label: 'Add new recipe', link: '/add' }, ]; getMenuItems(): MenuItem[] { diff --git a/src/app/services/recipe.service.ts b/src/app/services/recipe.service.ts index c55207a..8be5df4 100644 --- a/src/app/services/recipe.service.ts +++ b/src/app/services/recipe.service.ts @@ -1,9 +1,20 @@ import { Injectable } from '@angular/core'; +import { Recipe } from '../models/recipe'; @Injectable({ - providedIn: 'root' + providedIn: 'root', }) export class RecipeService { + private localStorageKey = 'recipes'; - constructor() { } + getRecipes(): Recipe[] { + const recipesJson = localStorage.getItem(this.localStorageKey); + return recipesJson ? JSON.parse(recipesJson) : []; + } + + addRecipe(recipe: Recipe): void { + const recipes = this.getRecipes(); + recipes.push(recipe); + localStorage.setItem(this.localStorageKey, JSON.stringify(recipes)); + } }