From b2339e34cd8440a8f1e382e40a20f015326b4344 Mon Sep 17 00:00:00 2001 From: Clement FERRERE Date: Tue, 26 Apr 2022 11:13:03 +0200 Subject: [PATCH] Fix des champs non modifies non reconnus pour les businessUnits --- .../businessunit-edit.component.html | 12 ++-- .../businessunit-edit.component.ts | 61 +++++++------------ src/app/services/businessunit.service.ts | 8 +-- 3 files changed, 33 insertions(+), 48 deletions(-) diff --git a/src/app/components/businessunit/businessunit-edit/businessunit-edit.component.html b/src/app/components/businessunit/businessunit-edit/businessunit-edit.component.html index d7bf37e..ec8a47a 100644 --- a/src/app/components/businessunit/businessunit-edit/businessunit-edit.component.html +++ b/src/app/components/businessunit/businessunit-edit/businessunit-edit.component.html @@ -1,14 +1,17 @@
-

{{businessunit.name | uppercase}}

-
id : {{businessunit.id}}
+

{{bu.name | uppercase}}

+
id : {{bu.id}}
-
+
- +
Le nom d'une business unit est obligatoire
@@ -17,7 +20,6 @@
-
diff --git a/src/app/components/businessunit/businessunit-edit/businessunit-edit.component.ts b/src/app/components/businessunit/businessunit-edit/businessunit-edit.component.ts index 64703dc..4dd786b 100644 --- a/src/app/components/businessunit/businessunit-edit/businessunit-edit.component.ts +++ b/src/app/components/businessunit/businessunit-edit/businessunit-edit.component.ts @@ -4,8 +4,8 @@ import {ActivatedRoute} from "@angular/router"; import {BusinessunitService} from "../../../services/businessunit.service"; import {Location} from "@angular/common"; import {FormBuilder, FormGroup, Validators} from "@angular/forms"; -import {Agence} from "../../../interfaces/agence"; -import {AgenceService} from "../../../services/agence.service"; +import {Observable} from "rxjs"; +import {take, tap} from "rxjs/operators"; @Component({ selector: 'app-businessunit-edit', @@ -14,8 +14,9 @@ import {AgenceService} from "../../../services/agence.service"; }) export class BusinessunitEditComponent implements OnInit { - businessunit = {} as Businessunit ; - agences : Agence[] = []; + businessunit! : Observable ; + bu! : Businessunit ; + id: number; registerForm!: FormGroup; @@ -24,56 +25,34 @@ export class BusinessunitEditComponent implements OnInit { constructor( private route: ActivatedRoute, private businessunitService: BusinessunitService, - private agenceService: AgenceService, private location: Location, private formBuilder: FormBuilder - ) { } + ) { + this.id = Number(this.route.snapshot.paramMap.get('id')) + } - ngOnInit(): void { - this.getBusinessunit(); - this.getAgences(); + async ngOnInit(): Promise { this.registerForm = this.formBuilder.group({ name: ['', Validators.required] }); + this.businessunit = this.businessunitService.getBusinessunit(this.id).pipe(tap(businessUnit => this.registerForm.patchValue(businessUnit))); + this.bu = await this.businessunit.pipe(take(1)).toPromise() } - getAgences():void { - this.agenceService.getAgences() - .subscribe(agences => this.agences = agences); - } - - getBusinessunit(): void { - const id = Number(this.route.snapshot.paramMap.get('id')) - this.businessunitService.getBusinessunit(id) - .subscribe(businessunit => this.businessunit = businessunit); - } - - goBack(): void { - this.location.back(); - } - - save():void{ - if (this.businessunit){ - this.businessunitService.updateBusinessunit(this.businessunit) - .subscribe(()=>this.goBack()); + async save() { + if (this.businessunit) { + this.businessunitService.updateBusinessunit(this.bu, this.id) + .subscribe(() => this.goBack()); } } - delete():void { - if (this.businessunit){ - this.businessunitService.deleteBusinessunit(this.businessunit) - .subscribe(()=>this.goBack()); - } - } - - onSubmit() { + async onSubmit() { this.submitted = true; - if (this.registerForm.invalid) { return; } - this.businessunit.name = this.registerForm.value.name - this.save() + this.bu.name = this.registerForm.value.name + await this.save() } get f() { return this.registerForm.controls; } @@ -83,4 +62,8 @@ export class BusinessunitEditComponent implements OnInit { this.registerForm.reset(); } + goBack(): void { + this.location.back(); + } + } diff --git a/src/app/services/businessunit.service.ts b/src/app/services/businessunit.service.ts index 2c96ef2..ff8b7ab 100644 --- a/src/app/services/businessunit.service.ts +++ b/src/app/services/businessunit.service.ts @@ -21,12 +21,12 @@ export class BusinessunitService { } getBusinessunit(id : number):Observable{ - return this.http.get(this.businessunitsUrl+"/"+id); + return this.http.get(this.businessunitsUrl + "/" + id); } - updateBusinessunit(businessunit : Businessunit):Observable{ - let body = JSON.stringify(businessunit); - return this.http.put(this.businessunitsUrl + "/" + businessunit.id, body, this.httpOptions); + updateBusinessunit(businessunit: Businessunit, id:number):Observable{ + let body= JSON.stringify(businessunit); + return this.http.put(this.businessunitsUrl + "/" + id, body, this.httpOptions); } deleteBusinessunit(businessunit: Businessunit) {