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