Projekt

Obecné

Profil

« Předchozí | Další » 

Revize c3e17dae

Přidáno uživatelem Jakub Hlaváč před téměř 4 roky(ů)

Re #8787 - Přidat unit - úprava formuláře

  • add unit is now in the nav-bar

Zobrazit rozdíly:

src/app/dashboard/components/dashboard.component.html
14 14
          <div class="col-md-8"><h3>{{ unit.unit.description}}</h3></div>
15 15
          <div class="col-md-4 text-right">
16 16
            <p-button label="Sensors graph" icon="pi pi-chart-line" [routerLink]="['/dashboard/unit', unit.unit.unitId]"></p-button>
17
            <p-splitButton label="Add sensor" (onClick)="insertSensorPopup($event, unit.unit)" icon="pi pi-plus-circle" (onDropdownClick)="showItems($event, unit.unit)" [model]="items" styleClass="p-button-warning"></p-splitButton>
17
            <p-splitButton *ngIf="loggedUser?.userInfo?.rightsId == 0 || loggedUser?.userInfo?.rightsId == 1" label="Add sensor" (onClick)="insertSensorPopup($event, unit.unit)" icon="pi pi-plus-circle" (onDropdownClick)="showItems($event, unit.unit)" [model]="items" styleClass="p-button-warning"></p-splitButton>
18 18
          </div>
19 19
        </div>
20 20
      </p-header>
21 21
      <div>
22
        <app-sensors *ngFor="let sensor of unit.sensors" [sensor]="sensor" [unitId]="unit.unit.unitId" [phenomenons]="phenomenons"></app-sensors>
22
        <app-sensors *ngFor="let sensor of unit.sensors" [sensor]="sensor" [unitId]="unit.unit.unitId" [phenomenons]="phenomenons" [loggedUser]="loggedUser"></app-sensors>
23 23
      </div>
24 24
    </p-accordionTab>
25 25
</p-accordion>
26 26
</div>
27 27

  
28 28
<app-unit-popup *ngIf="editedUnit" [(isVisible)]="showEditUnitPopup" [unit]="editedUnit"></app-unit-popup>
29
<app-unit-insert-popup [(isVisible)]="showInsertUnitPopup" [phenomenons]="phenomenons"></app-unit-insert-popup>
30 29
<app-sensor-insert-popup *ngIf="editedUnit" [unit]="editedUnit" [(isVisible)]="showInsertSensorPopup" [phenomenons]="phenomenons"></app-sensor-insert-popup>
31 30

  
32 31

  
src/app/dashboard/components/dashboard.component.ts
14 14
import {ToastService} from '../../shared/services/toast.service';
15 15
import {map} from 'rxjs/operators';
16 16
import {HttpResponse} from '@angular/common/http';
17
import {AuthService} from '../../auth/services/auth.service';
18
import {User} from '../../auth/models/user';
17 19

  
18 20
@Component({
19 21
  selector: 'app-dashboard',
......
22 24
})
23 25
export class DashboardComponent implements OnInit {
24 26

  
27
  loggedUser: User;
25 28
  items: MenuItem[] = [];
26 29
  position: 'bottom';
27 30
  groups: Group[];
28 31
  units: Array<{ drivers?: Drivers; generalInfo?: GeneralInfo; holder?: any; lastpos?: Lastpos; sensors?: Array<Sensor>; unit?: Unit }>;
29
  showInsertUnitPopup = false;
30 32
  editedUnit: Unit;
31 33
  showEditUnitPopup = false;
32 34
  showInsertSensorPopup = false;
......
38 40
    private confirmationService: ConfirmationService,
39 41
    private messageService: MessageService,
40 42
    private managementService: ManagementService,
41
    private toastService: ToastService
43
    private toastService: ToastService,
44
    private authService: AuthService
42 45
  ) {
43 46
    this.sensorService.getPhenomenons().subscribe(
44 47
      response => this.phenomenons = response
......
46 49
  }
47 50

  
48 51
  ngOnInit(): void {
52
    this.setUser();
49 53
    this.getUnits();
50 54
  }
51 55

  
56
  setUser(){
57
    this.authService.getUserState().subscribe(res => {
58
      if(res){
59
        this.loggedUser = res;
60
      }
61
    });
62
  }
63

  
52 64
  getUnits() {
53 65
    this.dataService.getData().subscribe(data => {
54 66
      this.units = data;
......
62 74
    this.showEditUnitPopup = true;
63 75
  }
64 76

  
65
  insertUnitPopup() {
66
    this.showInsertUnitPopup = true;
67
  }
68

  
69 77
  insertSensorPopup($event: any, unit: Unit) {
70 78
    $event.stopPropagation();
71 79
    this.showInsertSensorPopup = true;
src/app/dashboard/components/sensors/sensors.component.html
6 6
  </div>
7 7
  <div class="col-md-7 text-right">
8 8
    <button pButton type="button" label="View graph" [routerLink]="['/dashboard/unit', unitId, 'sensor', sensor.sensorId]" title="Sensor" icon="pi pi-chart-line"></button>
9
    <div class="dashboard-button-separator"></div>
10
    <button pButton type="button" label="Edit sensor" icon="pi pi-cog" (click)="editSensor($event, sensor)" [className]="'p-button-warning'"></button>
11
    <button pButton type="button" label="Delete sensor" icon="pi pi-times" (click)="deleteSensor($event, sensor)" [className]="'p-button-danger'"></button>
9
    <ng-container *ngIf="loggedUser?.userInfo?.rightsId == 0 || loggedUser?.userInfo?.rightsId == 1">
10
      <div class="dashboard-button-separator"></div>
11
      <button pButton type="button" label="Edit sensor" icon="pi pi-cog" (click)="editSensor($event, sensor)" [className]="'p-button-warning'"></button>
12
      <button pButton type="button" label="Delete sensor" icon="pi pi-times" (click)="deleteSensor($event, sensor)" [className]="'p-button-danger'"></button>
13
    </ng-container>
12 14
  </div>
13 15
</div>
14 16

  
src/app/dashboard/components/sensors/sensors.component.ts
5 5
import {map} from 'rxjs/operators';
6 6
import {HttpResponse} from '@angular/common/http';
7 7
import {ToastService} from '../../../shared/services/toast.service';
8
import {User} from '../../../auth/models/user';
8 9

  
9 10
@Component({
10 11
  selector: 'app-sensors',
......
16 17
  @Input() sensor: Sensor;
17 18
  @Input() unitId: number;
18 19
  @Input() phenomenons;
20
  @Input() loggedUser: User;
19 21
  showSensorPopup = false;
20 22
  editedSensor: Sensor;
21 23
  constructor(
......
43 45
        // TODO this.processSensorDeletion(sensor);
44 46
      },
45 47
      reject: () => {
46
        console.log('REJECTED');
48
        this.toastService.operationRejected();
47 49
      },
48 50
      key: 'positionDialog'
49 51
    });
src/app/dashboard/components/unit-insert-popup/unit-insert-popup.component.html
1
<p-dialog [visible]="isVisible" [modal]="true" [closable]="false" [draggable]="false" header="Add unit!" [style]="{width: '70vw'}"
2
          [baseZIndex]="10000" (onShow)="clearFormArray()">
3

  
4
  <form [formGroup]="insertForm">
5
    <div class="input-group form-group">
6
      <div class="input-group-prepend">
7
        <span class="input-group-text">Unit ID</span>
8
      </div>
9
      <input type="text" formControlName="unitId" class="form-control" id="unitId" placeholder="unitId"/>
10
    </div>
11
    <div class="input-group form-group">
12
      <div class="input-group-prepend">
13
        <span class="input-group-text">Description</span>
14
      </div>
15
      <input type="text" formControlName="unitDescription" class="form-control" id="unitDescription"
16
             placeholder="unitDescription"/>
17
    </div>
18
    <hr>
19
    <div class="input-group form-group">
20
      <div formArrayName="sensors" *ngFor="let item of insertForm.get('sensors')['controls']; let i = index;">
21
        <div [formGroupName]="i">
22
          <div class="input-group form-group">
23
            <div class="input-group-prepend">
24
              <span class="input-group-text">Sensor ID</span>
25
            </div>
26
            <input type="text" formControlName="sensorId" class="form-control" id="sensorId" placeholder="sensorId"/>
27
          </div>
28
          <div class="input-group form-group">
29
            <div class="input-group-prepend">
30
              <span class="input-group-text">Sensor name</span>
31
            </div>
32
            <input type="text" formControlName="sensorName" class="form-control" id="sensorName"
33
                   placeholder="sensorName"/>
34
          </div>
35
          <div class="input-group form-group">
36
            <div class="input-group-prepend">
37
              <span class="input-group-text">Sensor type</span>
38
            </div>
39
            <input type="text" formControlName="sensorType" class="form-control" id="sensorType"
40
                   placeholder="sensorType"/>
41
          </div>
42
          <div class="input-group form-group">
43
            <div class="input-group-prepend">
44
              <span class="input-group-text">Sensor name</span>
45
            </div>
46
            <select formControlName="phenomenons" id="phenomenons">
47
              <option *ngFor="let phenomenon of phenomenons; let i = index" [value]="phenomenons[i].phenomenonId">
48
                {{phenomenons[i].phenomenonName}} ( {{ phenomenons[i].unit}})
49
              </option>
50
            </select>
51
          </div>
52
        </div>
53
        <br>
54
      </div>
55
    </div>
56
  </form>
57

  
58

  
59
  <p-button type="button" (click)="addSensor()" class="pr-2">Add Sensor</p-button>
60

  
61

  
62
  <p-footer>
63
    <div class="row justify-content-end align-items-center">
64
      <div>
65
        <p-button icon="pi pi-check" (click)="processInsertion()" type="submit" label="Uložit" class="pr-2"></p-button>
66
        <p-button icon="pi pi-times" (click)="close()" label="Zavřít" class="pr-2"></p-button>
67
      </div>
68
    </div>
69
  </p-footer>
70
</p-dialog>
src/app/dashboard/components/unit-insert-popup/unit-insert-popup.component.ts
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
}
src/app/dashboard/dashboard.module.ts
9 9
import { UnitPopupComponent } from './components/unit-popup/unit-popup.component';
10 10
import {DialogModule} from 'primeng/dialog';
11 11
import { SensorPopupComponent } from './components/sensor-popup/sensor-popup.component';
12
import { UnitInsertPopupComponent } from './components/unit-insert-popup/unit-insert-popup.component';
13 12
import {ReactiveFormsModule} from '@angular/forms';
14 13
import { SensorInsertPopupComponent } from './components/sensor-insert-popup/sensor-insert-popup.component';
15 14
import {ConfirmDialogModule} from 'primeng/confirmdialog';
......
18 17

  
19 18

  
20 19
@NgModule({
21
  declarations: [DashboardComponent, SensorsComponent, UnitPopupComponent, SensorPopupComponent, UnitInsertPopupComponent,
22
    SensorInsertPopupComponent],
20
  declarations: [DashboardComponent, SensorsComponent, UnitPopupComponent, SensorPopupComponent, SensorInsertPopupComponent],
23 21
  imports: [
24 22
    CommonModule,
25 23
    NavBarModule,
src/app/shared/nav-bar/components/nav-bar.component.html
13 13
            <li *ngIf="loggedUser?.userInfo?.rightsId == 0" class="nav-item">
14 14
              <a class="nav-link" (click)="addUser()"><h2><i class="fas fa-user-plus"></i>&nbsp;Add user</h2></a>
15 15
            </li>
16
            <li class="nav-item">
17
              <a class="nav-link" [routerLink]="['/add-unit']"><h2><i class="fas fa-folder-plus"></i>&nbsp;Add unit</h2></a>
16
            <li *ngIf="loggedUser?.userInfo?.rightsId == 0 || loggedUser?.userInfo?.rightsId == 1" class="nav-item">
17
              <a class="nav-link" (click)="insertUnitPopup()"><h2><i class="fas fa-folder-plus"></i>&nbsp;Add unit</h2></a>
18 18
            </li>
19 19
          </ul>
20 20
          <a class="navbar-brand" href="/">
......
36 36
</div>
37 37

  
38 38
<app-user-insert-popup *ngIf="showAddUserPopup" [(isVisible)]="showAddUserPopup"></app-user-insert-popup>
39
<app-unit-insert-popup [(isVisible)]="showInsertUnitPopup" [phenomenons]="phenomenons"></app-unit-insert-popup>
src/app/shared/nav-bar/components/nav-bar.component.ts
3 3
import {User} from '../../../auth/models/user';
4 4
import {Subscription} from 'rxjs';
5 5
import {Right} from '../../api/endpoints/models/right';
6
import {Phenomenon} from '../../api/endpoints/models/phenomenon';
7
import {SensorsService} from '../../api/endpoints/services/sensors.service';
6 8

  
7 9
@Component({
8 10
  selector: 'app-nav-bar',
......
15 17
  subscription: Subscription[] = [];
16 18
  showAddUserPopup = false;
17 19
  rights: Right[];
20
  showInsertUnitPopup = false;
21
  phenomenons: Phenomenon[];
18 22
  constructor(
19
    private authService: AuthService
23
    private authService: AuthService,
24
    private sensorService: SensorsService
20 25
  ) {
21 26
  }
22 27

  
......
32 37
    });
33 38
  }
34 39

  
40
  insertUnitPopup() {
41
    this.sensorService.getPhenomenons().subscribe(
42
      response => this.phenomenons = response
43
    );
44
    this.showInsertUnitPopup = true;
45
  }
46

  
35 47
  logOut(): void {
36 48
    this.authService.doLogout();
37 49
  }
src/app/shared/nav-bar/components/unit-insert-popup/unit-insert-popup.component.html
1
<p-dialog [visible]="isVisible" [modal]="true" [closable]="false" [draggable]="false" header="Add unit!" [style]="{width: '70vw'}"
2
          [baseZIndex]="10000" (onShow)="clearFormArray()">
3

  
4
  <form [formGroup]="insertForm">
5
    <div class="input-group form-group">
6
      <div class="input-group-prepend">
7
        <span class="input-group-text">Unit ID</span>
8
      </div>
9
      <input type="text" formControlName="unitId" class="form-control" id="unitId" placeholder="unitId"/>
10
    </div>
11
    <div class="input-group form-group">
12
      <div class="input-group-prepend">
13
        <span class="input-group-text">Description</span>
14
      </div>
15
      <input type="text" formControlName="unitDescription" class="form-control" id="unitDescription"
16
             placeholder="unitDescription"/>
17
    </div>
18
    <hr>
19
    <div class="input-group form-group">
20
      <div formArrayName="sensors" *ngFor="let item of insertForm.get('sensors')['controls']; let i = index;">
21
        <div [formGroupName]="i">
22
          <div class="input-group form-group">
23
            <div class="input-group-prepend">
24
              <span class="input-group-text">Sensor ID</span>
25
            </div>
26
            <input type="text" formControlName="sensorId" class="form-control" id="sensorId" placeholder="sensorId"/>
27
          </div>
28
          <div class="input-group form-group">
29
            <div class="input-group-prepend">
30
              <span class="input-group-text">Sensor name</span>
31
            </div>
32
            <input type="text" formControlName="sensorName" class="form-control" id="sensorName"
33
                   placeholder="sensorName"/>
34
          </div>
35
          <div class="input-group form-group">
36
            <div class="input-group-prepend">
37
              <span class="input-group-text">Sensor type</span>
38
            </div>
39
            <input type="text" formControlName="sensorType" class="form-control" id="sensorType"
40
                   placeholder="sensorType"/>
41
          </div>
42
          <div class="input-group form-group">
43
            <div class="input-group-prepend">
44
              <span class="input-group-text">Sensor name</span>
45
            </div>
46
            <select formControlName="phenomenons" id="phenomenons">
47
              <option *ngFor="let phenomenon of phenomenons; let i = index" [value]="phenomenons[i].phenomenonId">
48
                {{phenomenons[i].phenomenonName}} ( {{ phenomenons[i].unit}})
49
              </option>
50
            </select>
51
          </div>
52
        </div>
53
        <br>
54
      </div>
55
    </div>
56
  </form>
57

  
58

  
59
  <p-button type="button" (click)="addSensor()" class="pr-2">Add Sensor</p-button>
60

  
61

  
62
  <p-footer>
63
    <div class="row justify-content-end align-items-center">
64
      <div>
65
        <p-button icon="pi pi-check" (click)="processInsertion()" type="submit" label="Uložit" class="pr-2"></p-button>
66
        <p-button icon="pi pi-times" (click)="close()" label="Zavřít" class="pr-2"></p-button>
67
      </div>
68
    </div>
69
  </p-footer>
70
</p-dialog>
src/app/shared/nav-bar/components/unit-insert-popup/unit-insert-popup.component.ts
1
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
2
import {FormArray, FormBuilder, FormGroup, Validators} from '@angular/forms';
3
import {HttpResponse} from '@angular/common/http';
4
import {map} from 'rxjs/operators';
5
import {ManagementService} from '../../../api/endpoints/services/management.service';
6
import {ToastService} from '../../../services/toast.service';
7
import {InsertUnit} from '../../../api/endpoints/models/insert-unit';
8
import {InsertSensor} from '../../../api/endpoints/models/insert-sensor';
9
import {Phenomenon} from '../../../api/endpoints/models/phenomenon';
10
import {SensorsService} from '../../../api/endpoints/services/sensors.service';
11

  
12
@Component({
13
  selector: 'app-unit-insert-popup',
14
  templateUrl: './unit-insert-popup.component.html',
15
  styleUrls: ['./unit-insert-popup.component.scss']
16
})
17
export class UnitInsertPopupComponent implements OnInit {
18

  
19
  insertForm: FormGroup;
20
  items: FormArray;
21

  
22
  @Input()phenomenons: Phenomenon[];
23
  @Input() isVisible;
24
  @Output() isVisibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();
25

  
26
  constructor(
27
    private formBuilder: FormBuilder,
28
    private managementService: ManagementService,
29
    private toastService: ToastService,
30
    private sensorService: SensorsService
31
  ) {
32
    this.initForm();
33
  }
34

  
35
  ngOnInit(): void {
36
  }
37

  
38
  close() {
39
    this.isVisibleChange.emit(false);
40
  }
41

  
42
  initForm() {
43
    this.insertForm = this.formBuilder.group({
44
      unitId: ['', [Validators.required]],
45
      unitDescription: ['', Validators.required],
46
      sensors: this.formBuilder.array([this.createSensor()])
47
    });
48
  }
49

  
50
  createSensor(): FormGroup {
51
    return this.formBuilder.group({
52
      sensorId: '',
53
      sensorName: '',
54
      sensorType: '',
55
      phenomenons: ['']
56
    });
57
  }
58

  
59
  addSensor(): void {
60
    this.items = this.insertForm.get('sensors') as FormArray;
61
    this.items.push(this.createSensor());
62
  }
63

  
64
  clearFormArray() {
65
    const frmArray = this.insertForm?.get('sensors') as FormArray;
66
    if (frmArray) {
67
      frmArray.clear();
68
    }
69
    this.addSensor();
70
    this.insertForm.reset();
71
  }
72

  
73
  processInsertion() {
74
    if (this.insertForm.valid) {
75
      const unit: InsertUnit = {
76
        unit_id: this.insertForm.controls.unitId.value,
77
        description: this.insertForm.controls.unitDescription.value
78
      };
79
      const sensors: InsertSensor[] = [];
80
      const frmArray = this.insertForm?.get('sensors') as FormArray;
81

  
82
      frmArray.controls.forEach(control => {
83
        const sensor: InsertSensor = {
84
          sensor_id: control.get('sensorId').value,
85
          sensor_name: control.get('sensorName').value,
86
          sensor_type: control.get('sensorType').value,
87
          phenomenon: {
88
            phenomenon_id: control.get('phenomenons').value
89
          },
90
        }
91
        sensors.push(sensor);
92
      });
93

  
94
      this.managementService.insertUnit$Response({ body: { unit, sensors}}).pipe(
95
        map((response: HttpResponse<any>) => {
96
          if (response.status === 200) {
97
            this.toastService.showSuccessMessage('Unit ' + response.body.unitId + ' inserted!');
98
            this.close()
99
          } else {
100
          }
101
        })
102
      ).toPromise().then().catch(err => this.toastService.showError(err.error.message));
103
    }
104
  }
105
}
src/app/shared/nav-bar/nav-bar.module.ts
6 6
import { UserInsertPopupComponent } from './components/user-insert-popup/user-insert-popup.component';
7 7
import {DialogModule} from 'primeng/dialog';
8 8
import {ReactiveFormsModule} from '@angular/forms';
9
import {UnitInsertPopupComponent} from './components/unit-insert-popup/unit-insert-popup.component';
9 10

  
10 11

  
11 12
@NgModule({
12
  declarations: [NavBarComponent, UserInsertPopupComponent],
13
  declarations: [NavBarComponent, UserInsertPopupComponent, UnitInsertPopupComponent],
13 14
  exports: [
14 15
    NavBarComponent
15 16
  ],

Také k dispozici: Unified diff