1
|
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
|
2
|
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
|
3
|
import {ManagementService} from '../../../shared/api/endpoints/services/management.service';
|
4
|
import {ToastService} from '../../../shared/services/toast.service';
|
5
|
|
6
|
@Component({
|
7
|
selector: 'app-unit-popup',
|
8
|
templateUrl: './unit-popup.component.html',
|
9
|
styleUrls: ['./unit-popup.component.scss']
|
10
|
})
|
11
|
export class UnitPopupComponent implements OnInit {
|
12
|
|
13
|
insertForm: FormGroup;
|
14
|
|
15
|
@Input() isVisible;
|
16
|
@Input() unit;
|
17
|
@Output() isVisibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();
|
18
|
|
19
|
constructor(
|
20
|
private formBuilder: FormBuilder,
|
21
|
private managementService: ManagementService,
|
22
|
private toastService: ToastService
|
23
|
) {
|
24
|
this.initForm();
|
25
|
}
|
26
|
|
27
|
initForm() {
|
28
|
this.insertForm = this.formBuilder.group({
|
29
|
unitDescription: ['', Validators.required]
|
30
|
});
|
31
|
setTimeout(() => {
|
32
|
this.insertForm.controls.unitDescription.setValue(this.unit.description);
|
33
|
}, 0);
|
34
|
}
|
35
|
|
36
|
ngOnInit(): void {
|
37
|
}
|
38
|
|
39
|
|
40
|
saveUnit() {
|
41
|
if (this.insertForm.controls.unitDescription.value && this.insertForm.controls.unitDescription.value !== this.unit.description) {
|
42
|
this.unit.description = this.insertForm.controls.unitDescription.value;
|
43
|
this.managementService.updateUnit$Response({ body: {
|
44
|
unit: {
|
45
|
unit_id: this.unit.unitId,
|
46
|
description: this.unit.description
|
47
|
}}
|
48
|
}).toPromise().then( response => {
|
49
|
if (response.status === 200) {
|
50
|
this.toastService.showSuccess();
|
51
|
this.close();
|
52
|
} else {
|
53
|
this.toastService.showError(response.body);
|
54
|
}
|
55
|
}).catch(err => this.toastService.showError(err.body.message));
|
56
|
}
|
57
|
}
|
58
|
|
59
|
close() {
|
60
|
this.insertForm.reset();
|
61
|
this.isVisibleChange.emit(false);
|
62
|
}
|
63
|
}
|