1
|
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
|
2
|
import {FormArray, FormBuilder, FormGroup, Validators} from '@angular/forms';
|
3
|
import {InsertUnit} from '../../../shared/api/endpoints/models/insert-unit';
|
4
|
import {InsertSensor} from '../../../shared/api/endpoints/models/insert-sensor';
|
5
|
import {ManagementService} from '../../../shared/api/endpoints/services/management.service';
|
6
|
import {ToastService} from '../../../shared/services/toast.service';
|
7
|
import {HttpResponse} from '@angular/common/http';
|
8
|
import {map} from 'rxjs/operators';
|
9
|
|
10
|
@Component({
|
11
|
selector: 'app-unit-insert-popup',
|
12
|
templateUrl: './unit-insert-popup.component.html',
|
13
|
styleUrls: ['./unit-insert-popup.component.scss']
|
14
|
})
|
15
|
export class UnitInsertPopupComponent implements OnInit {
|
16
|
|
17
|
insertForm: FormGroup;
|
18
|
items: FormArray;
|
19
|
|
20
|
|
21
|
@Input() phenomenons;
|
22
|
@Input() isVisible;
|
23
|
@Output() isVisibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();
|
24
|
|
25
|
constructor(
|
26
|
private formBuilder: FormBuilder,
|
27
|
private managementService: ManagementService,
|
28
|
private toastService: ToastService
|
29
|
) {
|
30
|
this.initForm();
|
31
|
}
|
32
|
|
33
|
ngOnInit(): void {
|
34
|
}
|
35
|
|
36
|
close() {
|
37
|
this.isVisibleChange.emit(false);
|
38
|
}
|
39
|
|
40
|
initForm() {
|
41
|
this.insertForm = this.formBuilder.group({
|
42
|
unitId: ['', [Validators.required]],
|
43
|
unitDescription: ['', Validators.required],
|
44
|
sensors: this.formBuilder.array([this.createSensor()])
|
45
|
});
|
46
|
}
|
47
|
|
48
|
createSensor(): FormGroup {
|
49
|
return this.formBuilder.group({
|
50
|
sensorId: '',
|
51
|
sensorName: '',
|
52
|
sensorType: '',
|
53
|
phenomenons: ['']
|
54
|
});
|
55
|
}
|
56
|
|
57
|
addSensor(): void {
|
58
|
this.items = this.insertForm.get('sensors') as FormArray;
|
59
|
this.items.push(this.createSensor());
|
60
|
}
|
61
|
|
62
|
clearFormArray() {
|
63
|
const frmArray = this.insertForm?.get('sensors') as FormArray;
|
64
|
if (frmArray) {
|
65
|
frmArray.clear();
|
66
|
}
|
67
|
this.addSensor();
|
68
|
this.insertForm.reset();
|
69
|
}
|
70
|
|
71
|
processInsertion() {
|
72
|
if (this.insertForm.valid) {
|
73
|
const unit: InsertUnit = {
|
74
|
unit_id: this.insertForm.controls.unitId.value,
|
75
|
description: this.insertForm.controls.unitDescription.value
|
76
|
};
|
77
|
const sensors: InsertSensor[] = [];
|
78
|
const frmArray = this.insertForm?.get('sensors') as FormArray;
|
79
|
|
80
|
frmArray.controls.forEach(control => {
|
81
|
const sensor: InsertSensor = {
|
82
|
sensor_id: control.get('sensorId').value,
|
83
|
sensor_name: control.get('sensorName').value,
|
84
|
sensor_type: control.get('sensorType').value,
|
85
|
phenomenon: {
|
86
|
phenomenon_id: control.get('phenomenons').value
|
87
|
},
|
88
|
}
|
89
|
sensors.push(sensor);
|
90
|
});
|
91
|
|
92
|
this.managementService.insertUnit$Response({ body: { unit, sensors}}).pipe(
|
93
|
map((response: HttpResponse<any>) => {
|
94
|
if (response.status === 200) {
|
95
|
this.toastService.showSuccessMessage('Unit ' + response.body.unitId + ' inserted!');
|
96
|
this.close()
|
97
|
} else {
|
98
|
}
|
99
|
})
|
100
|
).toPromise().then().catch(err => this.toastService.showError(err.error.message));
|
101
|
}
|
102
|
}
|
103
|
}
|