|
|
@ -4,8 +4,8 @@ import {ActivatedRoute} from "@angular/router"; |
|
|
|
import {BusinessunitService} from "../../../services/businessunit.service"; |
|
|
|
import {BusinessunitService} from "../../../services/businessunit.service"; |
|
|
|
import {Location} from "@angular/common"; |
|
|
|
import {Location} from "@angular/common"; |
|
|
|
import {FormBuilder, FormGroup, Validators} from "@angular/forms"; |
|
|
|
import {FormBuilder, FormGroup, Validators} from "@angular/forms"; |
|
|
|
import {Agence} from "../../../interfaces/agence"; |
|
|
|
import {Observable} from "rxjs"; |
|
|
|
import {AgenceService} from "../../../services/agence.service"; |
|
|
|
import {take, tap} from "rxjs/operators"; |
|
|
|
|
|
|
|
|
|
|
|
@Component({ |
|
|
|
@Component({ |
|
|
|
selector: 'app-businessunit-edit', |
|
|
|
selector: 'app-businessunit-edit', |
|
|
@ -14,8 +14,9 @@ import {AgenceService} from "../../../services/agence.service"; |
|
|
|
}) |
|
|
|
}) |
|
|
|
export class BusinessunitEditComponent implements OnInit { |
|
|
|
export class BusinessunitEditComponent implements OnInit { |
|
|
|
|
|
|
|
|
|
|
|
businessunit = {} as Businessunit ; |
|
|
|
businessunit! : Observable<Businessunit> ; |
|
|
|
agences : Agence[] = []; |
|
|
|
bu! : Businessunit ; |
|
|
|
|
|
|
|
id: number; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
registerForm!: FormGroup; |
|
|
|
registerForm!: FormGroup; |
|
|
@ -24,56 +25,34 @@ export class BusinessunitEditComponent implements OnInit { |
|
|
|
constructor( |
|
|
|
constructor( |
|
|
|
private route: ActivatedRoute, |
|
|
|
private route: ActivatedRoute, |
|
|
|
private businessunitService: BusinessunitService, |
|
|
|
private businessunitService: BusinessunitService, |
|
|
|
private agenceService: AgenceService, |
|
|
|
|
|
|
|
private location: Location, |
|
|
|
private location: Location, |
|
|
|
private formBuilder: FormBuilder |
|
|
|
private formBuilder: FormBuilder |
|
|
|
) { } |
|
|
|
) { |
|
|
|
|
|
|
|
this.id = Number(this.route.snapshot.paramMap.get('id')) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void { |
|
|
|
async ngOnInit(): Promise<void> { |
|
|
|
this.getBusinessunit(); |
|
|
|
|
|
|
|
this.getAgences(); |
|
|
|
|
|
|
|
this.registerForm = this.formBuilder.group({ |
|
|
|
this.registerForm = this.formBuilder.group({ |
|
|
|
name: ['', Validators.required] |
|
|
|
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 { |
|
|
|
async save() { |
|
|
|
this.agenceService.getAgences() |
|
|
|
if (this.businessunit) { |
|
|
|
.subscribe(agences => this.agences = agences); |
|
|
|
this.businessunitService.updateBusinessunit(this.bu, this.id) |
|
|
|
} |
|
|
|
.subscribe(() => this.goBack()); |
|
|
|
|
|
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
delete():void { |
|
|
|
async onSubmit() { |
|
|
|
if (this.businessunit){ |
|
|
|
|
|
|
|
this.businessunitService.deleteBusinessunit(this.businessunit) |
|
|
|
|
|
|
|
.subscribe(()=>this.goBack()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onSubmit() { |
|
|
|
|
|
|
|
this.submitted = true; |
|
|
|
this.submitted = true; |
|
|
|
|
|
|
|
|
|
|
|
if (this.registerForm.invalid) { |
|
|
|
if (this.registerForm.invalid) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
this.businessunit.name = this.registerForm.value.name |
|
|
|
this.bu.name = this.registerForm.value.name |
|
|
|
this.save() |
|
|
|
await this.save() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
get f() { return this.registerForm.controls; } |
|
|
|
get f() { return this.registerForm.controls; } |
|
|
@ -83,4 +62,8 @@ export class BusinessunitEditComponent implements OnInit { |
|
|
|
this.registerForm.reset(); |
|
|
|
this.registerForm.reset(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
goBack(): void { |
|
|
|
|
|
|
|
this.location.back(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|