Project

General

Profile

1
import { Component, OnInit } from '@angular/core';
2
import { DashboardSection } from './../../shared/models/dashboard-section.interface';
3
import { DocumentClassification } from './../../shared/models/document-classification.interface';
4
import { IPowerClient } from './../../shared/models/ipower-client.interface';
5
import { AuthService } from './../../shared/services/auth.service';
6
import { DashboardService } from './../../shared/services/dashboard/dashboard.service';
7
import { IpowerClientsService } from './../../shared/services/administration/ipower-clients.service';
8
import { DocumentClassificationService } from './../../shared/services/document-classification.service';
9
import { ErrorHandlingService } from 'src/app/shared/services/error-handling/error-handling.service';
10
import { PieConfiguration } from './dashboard.configuration';
11
import { environment } from './../../../environments/environment';
12
 
13
@Component({
14
  selector: 'app-dashboard',
15
  templateUrl: './dashboard.component.html',
16
  styleUrls: ['./dashboard.component.scss']
17
})
18
export class DashboardComponent implements OnInit {
19

    
20
  iPowerClients: IPowerClient[] = [];
21
  selectedIPowerClients: string[] = [];
22
  dashboardSections: DashboardSection[] = [];
23
  documentClassifications: DocumentClassification[];
24
  pieConfiguration = new PieConfiguration();
25
  
26
  constructor(public authService: AuthService,
27
              private dashboardService: DashboardService,
28
              private ipowerClientsService: IpowerClientsService,
29
              private errorHandlingService: ErrorHandlingService,
30
              private documentClassificationService: DocumentClassificationService) { }
31

    
32
  ngOnInit(): void {
33
    this.getClientsByUser();
34

    
35
    this.documentClassificationService.getAll().subscribe(docClassifications => {
36
      this.documentClassifications = docClassifications;
37
    });
38
  }
39

    
40
  enrichData(sections) {
41
    for (let section of sections) {
42
      const noneOngoing = (section.totalProcesses > 0) ? 0 :
43
                          (section.totalExceptions > 0) ? 0 :
44
                          (section.totalUnhandled > 0) ? 0 : 1; 
45
      section.pie = {
46
        labels: this.pieConfiguration.sections.find(x => x.type === section.type)?.options?.labels,
47
        datasets: [
48
          {
49
            data: [section.totalProcesses, section.totalExceptions, section.totalUnhandled, noneOngoing],
50
            backgroundColor: this.pieConfiguration.sections.find(x => x.type === section.type)?.options?.backgroundColor,
51
            hoverBackgroundColor: this.pieConfiguration.sections.find(x => x.type === section.type)?.options?.hoverBackgroundColor
52
          }
53
        ]
54
      };
55
      section.selectedElements = 3;
56
      section.selectedPage = 0;
57
      section.resultsLoading = false;
58
    } 
59
  }
60

    
61
  initializeData(sections) {
62
    this.enrichData(sections);
63
    this.dashboardSections = sections;
64
  }
65

    
66
  patchData(sections, sectionPage, sectionOffset) {
67
    let newSecs = this.dashboardSections.filter(section => section.type === sections[0].type);
68

    
69
    newSecs[0].type = sections[0].type;
70
    newSecs[0].totalPages = sections[0].totalPages;
71
    newSecs[0].totalElements = sections[0].totalElements;
72
    newSecs[0].aggregatedResults = sections[0].aggregatedResults;
73
    newSecs[0].selectedElements = sectionOffset;
74
    newSecs[0].selectedPage = sectionPage;
75
    newSecs[0].resultsLoading = false;
76

    
77
    this.dashboardSections = this.dashboardSections?.map(obj =>
78
      newSecs?.find(section =>
79
        section.type === obj.type
80
      ) || obj
81
    );
82
  }
83

    
84
  evaluateLoading(sectionType, loading) {
85
    for (let section of this.dashboardSections) {
86
      if (section.type === sectionType) {
87
        section.resultsLoading = loading;
88
      }
89
      else {
90
        section.resultsLoading = false;
91
      }
92
    }
93
  }
94

    
95
  setIPowerClientsInStore() {
96
    const now = new Date()
97
    const item = {
98
      value: this.selectedIPowerClients,
99
      expiry: now.getTime() + environment.dashboardSelectedIpowerClientsStorageTime
100
    }
101
    localStorage.setItem('selectedIpowerClients', JSON.stringify(item))
102
  }
103

    
104
  getStoredIPowerClients() {
105
    const selectedIpowerClients = localStorage.getItem('selectedIpowerClients')
106

    
107
    if (!selectedIpowerClients) {
108
      return [];
109
    }
110

    
111
    const item = JSON.parse(selectedIpowerClients)
112
	  const now = new Date()
113
	  if (now.getTime() > item.expiry) {
114
      localStorage.removeItem(selectedIpowerClients)
115
      return [];
116
    }
117

    
118
    return item.value;
119
  }
120

    
121
  getClientsByUser() {
122
    this.ipowerClientsService.getClientsByUser()
123
      .subscribe(clients => {
124
        this.iPowerClients = clients;
125
        const storedIPowerClients = this.getStoredIPowerClients();
126
        let filteredIPowerClients: IPowerClient[] = [];
127
        let selectedIPowerClients: string[] = [];
128

    
129
        filteredIPowerClients = this.iPowerClients.filter(function (item) {
130
          return storedIPowerClients.indexOf(item.clientCode) !== -1;
131
        });
132

    
133
        if (filteredIPowerClients.length > 0) {
134
          filteredIPowerClients.forEach(function (item) {
135
            selectedIPowerClients.push(item.clientCode);
136
          });
137
          this.selectedIPowerClients = selectedIPowerClients;
138
        }
139
        else {
140
          this.selectedIPowerClients = [];
141
        }
142
      }, err => {
143
        this.errorHandlingService.showHttpResponseError(err);
144
      }, () => {
145
        this.getData('', 0, 3);
146
      });
147
  }
148

    
149
  getData(sectionType, sectionPage, sectionOffset) {
150
    this.evaluateLoading(sectionType, true);
151
    this.dashboardService.getDashboardSections(sectionType, sectionPage, sectionOffset, this.selectedIPowerClients)
152
      .subscribe(sections => {
153
        this.setIPowerClientsInStore();
154
        if (sectionType !== '') {
155
          this.patchData(sections, sectionPage, sectionOffset);
156
        }
157
        else {
158
          this.initializeData(sections);
159
        }
160
      }, err => {
161
        this.errorHandlingService.showHttpResponseError(err);
162
      });
163
  }
164

    
165
  totalElementsChanged(sectionOffset, section) {
166
    this.getData(section.type, 0, sectionOffset);
167
    console.log(section);
168
  }
169

    
170
  paginationEvent(paginationEvent) {
171
    this.getData(paginationEvent.sectionType, paginationEvent.sectionPage, paginationEvent.sectionOffset);
172
  }
173

    
174
}
(4-4/5)