Projekt

Obecné

Profil

Stáhnout (4.05 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Component, OnInit } from '@angular/core';
2
import {Group} from '../../shared/api/endpoints/models/group';
3
import {Drivers} from '../../shared/api/endpoints/models/drivers';
4
import {GeneralInfo} from '../../shared/api/endpoints/models/general-info';
5
import {Lastpos} from '../../shared/api/endpoints/models/lastpos';
6
import {Sensor} from '../../shared/api/endpoints/models/sensor';
7
import {Unit} from '../../shared/api/endpoints/models/unit';
8
import {DataService} from '../../shared/api/endpoints/services/data.service';
9
import {Phenomenon} from '../../shared/api/endpoints/models/phenomenon';
10
import {SensorsService} from '../../shared/api/endpoints/services/sensors.service';
11
import {ConfirmationService, MenuItem, MessageService} from 'primeng/api';
12
import {ManagementService} from '../../shared/api/endpoints/services/management.service';
13
import {InsertUnit} from '../../shared/api/endpoints/models/insert-unit';
14
import {ToastService} from '../../shared/services/toast.service';
15
import {map} from 'rxjs/operators';
16
import {HttpResponse} from '@angular/common/http';
17
import {AuthService} from '../../auth/services/auth.service';
18
import {User} from '../../auth/models/user';
19

    
20
@Component({
21
  selector: 'app-dashboard',
22
  templateUrl: './dashboard.component.html',
23
  styleUrls: ['./dashboard.component.scss']
24
})
25
export class DashboardComponent implements OnInit {
26

    
27
  loggedUser: User;
28
  items: MenuItem[] = [];
29
  position: 'bottom';
30
  groups: Group[];
31
  units: Array<{ drivers?: Drivers; generalInfo?: GeneralInfo; holder?: any; lastpos?: Lastpos; sensors?: Array<Sensor>; unit?: Unit }>;
32
  editedUnit: Unit;
33
  showEditUnitPopup = false;
34
  showInsertSensorPopup = false;
35
  phenomenons: Phenomenon[];
36

    
37
  constructor(
38
    private dataService: DataService,
39
    private sensorService: SensorsService,
40
    private confirmationService: ConfirmationService,
41
    private messageService: MessageService,
42
    private managementService: ManagementService,
43
    private toastService: ToastService,
44
    private authService: AuthService
45
  ) {
46
    this.sensorService.getPhenomenons().subscribe(
47
      response => this.phenomenons = response
48
    );
49
  }
50

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

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

    
64
  getUnits() {
65
    this.dataService.getData().subscribe(data => {
66
      this.units = data;
67
      this.units.forEach(unit => unit.sensors.sort((a, b)  => a.sensorId - b.sensorId));
68
    }, err => this.toastService.showError(err.error.message));
69
  }
70

    
71
  editUnitPopup($event: MouseEvent, unit: Unit) {
72
    $event.stopPropagation();
73
    this.editedUnit = unit;
74
    this.showEditUnitPopup = true;
75
  }
76

    
77
  insertSensorPopup($event: any, unit: Unit) {
78
    $event.stopPropagation();
79
    this.showInsertSensorPopup = true;
80
    this.editedUnit = unit;
81
  }
82

    
83
  deleteUnit($event: any, unit: Unit) {
84
    $event.stopPropagation();
85
    this.confirmationService.confirm({
86
      message: 'Do you want to delete this unit?',
87
      header: 'Delete Unit Confirmation',
88
      icon: 'pi pi-info-circle',
89
      accept: () => {
90
        // TODO this.processUnitDeletion(unit);
91
      },
92
      reject: () => {
93
        this.toastService.operationRejected();
94
      },
95
      key: 'positionDialog'
96
    });
97
  }
98

    
99
  processUnitDeletion(insertUnit: InsertUnit) {
100
    this.managementService.deleteUnit$Response({body: {unit: insertUnit}}).pipe(
101
      map((response: HttpResponse<any>) => {
102
        if (response.status === 200) {
103
          this.toastService.showSuccessMessage(response.body.message);
104
        } else {
105
        }
106
      })
107
    ).toPromise().then().catch(err => this.toastService.showError(err.error.message));
108
  }
109

    
110
  showItems($event: any, unit: Unit) {
111
    $event.stopPropagation();
112
    this.items = [
113
      {label: 'Edit unit', icon: 'pi pi-cog', command: () => {
114
          event.stopPropagation();
115
          this.editUnitPopup($event, unit);
116
        }},
117
      {label: 'Delete unit', icon: 'pi pi-times', command: () => {
118
          event.stopPropagation();
119
          this.deleteUnit($event, unit);
120
        }}
121
    ]
122
  }
123
}
(3-3/3)