From 388804ae06c59ecf29975c6ea58007d0387b87d2 Mon Sep 17 00:00:00 2001 From: Blandine Bajard <83599148+BlandineBajard@users.noreply.github.com> Date: Fri, 18 Feb 2022 17:22:17 +0100 Subject: [PATCH] menus --- src/app/app.module.ts | 4 +- .../card-menu/card-menu.component.html | 130 ++++++++++++++---- .../card-menu/card-menu.component.ts | 67 +++++++-- src/app/models/menu.ts | 4 +- src/app/services/menus.service.ts | 18 ++- 5 files changed, 171 insertions(+), 52 deletions(-) diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 8de0573..d1960bf 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -39,7 +39,6 @@ import { PageHumeurComponent } from './pages/page-humeur/page-humeur.component'; import { FooterComponent } from './components/footer/footer.component'; import { PageDeleteAccountComponent } from './pages/page-delete-account/page-delete-account.component'; import { PageUpdateAccountComponent } from './pages/page-update-account/page-update-account.component'; -import { EmoticonComponent } from './emoticon/emoticon.component'; @@ -80,7 +79,6 @@ import { EmoticonComponent } from './emoticon/emoticon.component'; FooterComponent, PageDeleteAccountComponent, PageUpdateAccountComponent, - EmoticonComponent, ], imports: [ BrowserModule, @@ -88,7 +86,7 @@ import { EmoticonComponent } from './emoticon/emoticon.component'; ReactiveFormsModule, HttpClientModule, FormsModule, - // AutofocusFixModule.forRoot(), + AutofocusFixModule.forRoot(), ], providers: [ diff --git a/src/app/components/card-menu/card-menu.component.html b/src/app/components/card-menu/card-menu.component.html index a6a566b..44b472f 100644 --- a/src/app/components/card-menu/card-menu.component.html +++ b/src/app/components/card-menu/card-menu.component.html @@ -1,46 +1,124 @@ - - - - - - diff --git a/src/app/components/card-menu/card-menu.component.ts b/src/app/components/card-menu/card-menu.component.ts index 1cbbff0..002fd82 100644 --- a/src/app/components/card-menu/card-menu.component.ts +++ b/src/app/components/card-menu/card-menu.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, Input, OnInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { Menu } from 'src/app/models/menu'; import { MenusService } from 'src/app/services/menus.service'; @@ -22,6 +22,8 @@ closeResult = ''; public listMenus:any[]; public menuId : any; public menuForm : FormGroup; +public upMenuForm : FormGroup; + constructor( private menusService: MenusService, @@ -31,10 +33,49 @@ public menuForm : FormGroup; private fb: FormBuilder ) { this.listMenus=[]; - this.menuId=0; this.menuForm = new FormGroup({}); + this.upMenuForm = this.initForm(); } + //Méthode qui initialise les champs du formulaire avec les infos de la BDD + private initForm(menu?: Menu): FormGroup { + return this.fb.group({ + dateMenu: [menu ? menu.dateMenu : ''], + libelle: [menu ? menu.libelle : ''], + repas: [menu ? menu.repas : ''], + + }); + } + + //delete d'un menu + deleteMenu(id_menu : number): void { + window.alert("Le menu a bien été supprimé!") +this.menusService.deleteMenu(id_menu)?.subscribe((resp) => { + this.router.navigate(['menu']); +}); + + } + +//updateMenu +updateMenu(id_menu : number): void { + const dateValue = this.menuForm.value['dateMenuFc']; + const libelleValue = this.menuForm.value['libelleFc']; + const repasValue = this.menuForm.value['repasFc']; + + const menu: Menu = { + dateMenu: dateValue, + libelle: libelleValue, + repas: repasValue, + id: id_menu + }; + + console.log(id_menu); + + this.menusService.updateMenu(menu, id_menu)?.subscribe((resp) => { + console.log("ok"); + this.router.navigate(['menu']); + }); + } //ajout d'un menu @@ -42,21 +83,24 @@ saveMenu(): void { const dateValue = this.menuForm.value['dateMenuFc']; const libelleValue = this.menuForm.value['libelleFc']; + const repasValue = this.menuForm.value['repasFc']; const menu: Menu = { dateMenu: dateValue, libelle: libelleValue, + repas: repasValue, + id: 0 }; - // if (menu.dateMenu != undefined && menu.libelle != '') { + if (menu.dateMenu !=='' && menu.libelle !== '') { - console.log(dateValue); + console.log(menu.dateMenu); this.menusService.addMenu(menu)?.subscribe((resp) => { this.router.navigate(['menu']); }); - // } else { - // this.router.navigate(['menu']); - // } + } else { + this.router.navigate(['accueil']); + } } @@ -72,6 +116,7 @@ this.menusService.getMenu()?.subscribe((listMenus: any[])=>{ { dateMenuFc: new FormControl('',[Validators.required]), libelleFc: new FormControl('',[Validators.required]), + repasFc: new FormControl('',[Validators.required]), } ); } @@ -99,11 +144,3 @@ open(content: any) { -//modif d'un menu -// this.menuId=this.route.snapshot.paramMap.get('id'); -// this.menusService.getMenuById(this.menuId) -// .subscribe((this.listMenus:any)=>) - -//delete d'un menu - - diff --git a/src/app/models/menu.ts b/src/app/models/menu.ts index 9b08b81..81b7415 100644 --- a/src/app/models/menu.ts +++ b/src/app/models/menu.ts @@ -3,5 +3,7 @@ import { Team } from './team'; export interface Menu { libelle: string; - dateMenu: Date; + dateMenu: string; + repas: string; + id: number; } diff --git a/src/app/services/menus.service.ts b/src/app/services/menus.service.ts index 9eebafb..019495f 100644 --- a/src/app/services/menus.service.ts +++ b/src/app/services/menus.service.ts @@ -44,13 +44,17 @@ apiUrl: string; } - deleteMenu(id: any): Observable { - return this.http.delete(`${this.apiUrl}/menus/delete/`+ id); + deleteMenu(id:number): Observable { + return this.http.delete(`${this.apiUrl}/menus/delete/${id}`); } - updateMenu(menu: Menu): Observable { - return this.http.put(`${this.apiUrl}/contacts/update/`, menu); - } + + updateMenu(menu: Menu, id:number): Observable | void { + const teamId = this.tokenService.getCurrentTeamId(); + if (teamId){ + return this.http.put(`${this.apiUrl}/menus/update/${teamId}/${id}`, menu); + }else { + this.router.navigate(['accueil']); + } +} } - -