Create feature module for Enrichment coordinator
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ei-coordinator / producers-list / producers-list.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 import { Component, OnInit } from '@angular/core';
21 import { FormControl, FormGroup } from '@angular/forms';
22 import { Sort } from '@angular/material/sort';
23 import { MatTableDataSource } from '@angular/material/table';
24 import { EIProducer } from '../../interfaces/ei.types';
25 import { UiService } from '../../services/ui/ui.service';
26 import { EIProducerDataSource } from '../ei-producer.datasource';
27
28 @Component({
29   selector: 'nrcp-producers-list',
30   templateUrl: './producers-list.component.html',
31   styleUrls: ['./producers-list.component.scss']
32 })
33 export class ProducersListComponent implements OnInit {
34   darkMode: boolean;
35   producersDataSource: MatTableDataSource<EIProducer> = new MatTableDataSource<EIProducer>();
36   producerForm: FormGroup;
37
38   constructor(
39     private eiProducersDataSource: EIProducerDataSource,
40     private ui: UiService) {
41
42     this.producerForm = new FormGroup({
43       ei_producer_id: new FormControl(''),
44       ei_producer_types: new FormControl(''),
45       status: new FormControl('')
46     });
47   }
48
49   ngOnInit(): void {
50     this.refresh();
51
52     this.producerForm.valueChanges.subscribe(value => {
53       const filter = { ...value, ei_producer_id: value.ei_producer_id.trim().toLowerCase() } as string;
54       this.producersDataSource.filter = filter;
55     });
56
57     this.producersDataSource.filterPredicate = ((data, filter) => {
58       return this.isDataIncluding(data.ei_producer_id, filter.ei_producer_id)
59         && this.isDataIncluding(data.ei_producer_types.join(','), filter.ei_producer_types)
60         && this.isDataIncluding(data.status, filter.status);
61     }) as (data: EIProducer, filter: any) => boolean;
62
63     this.ui.darkModeState.subscribe((isDark) => {
64       this.darkMode = isDark;
65     });
66   }
67
68   isDataIncluding(data: string, filter: string): boolean {
69     return !filter || data.toLowerCase().includes(filter);
70   }
71
72   sortProducers(sort: Sort) {
73     const data = this.producersDataSource.data
74     data.sort((a: EIProducer, b: EIProducer) => {
75       const isAsc = sort.direction === 'asc';
76       switch (sort.active) {
77         case 'id': return this.compare(a.ei_producer_id, b.ei_producer_id, isAsc);
78         case 'types': return this.compare(a.ei_producer_types, b.ei_producer_types, isAsc);
79         case 'status': return this.compare(a.status, b.status, isAsc);
80         default: return 0;
81       }
82     });
83     this.producersDataSource.data = data;
84   }
85
86   compare(a: any, b: any, isAsc: boolean) {
87     return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
88   }
89
90   stopSort(event: any) {
91     event.stopPropagation();
92   }
93
94   getProducerTypes(eiProducer: EIProducer): string[] {
95     if (eiProducer.ei_producer_types) {
96       return eiProducer.ei_producer_types;
97     }
98     return ['< No types >'];
99   }
100
101   getProducerStatus(eiProducer: EIProducer): string {
102     if (eiProducer.status) {
103       return eiProducer.status;
104     }
105     return '< No status >';
106   }
107
108   refresh() {
109     this.eiProducersDataSource.loadProducers();
110     this.eiProducersDataSource.eiProducersSubject().subscribe((data) => {
111       this.producersDataSource.data = data;
112     });
113   }
114 }