improve the dark theme
[portal/ric-dashboard.git] / webapp-frontend / src / app / anr-xapp / anr-xapp.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
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
21 import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
22 import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
23 import { MatSort } from '@angular/material/sort';
24 import { MatDialog } from '@angular/material/dialog';
25 import { fromEvent } from 'rxjs/observable/fromEvent';
26 import { debounceTime, distinctUntilChanged, finalize, tap } from 'rxjs/operators';
27 import { ANRNeighborCellRelation } from '../interfaces/anr-xapp.types';
28 import { ANRXappService } from '../services/anr-xapp/anr-xapp.service';
29 import { ErrorDialogService } from '../services/ui/error-dialog.service';
30 import { LoadingDialogService } from '../services/ui/loading-dialog.service';
31 import { ConfirmDialogService } from './../services/ui/confirm-dialog.service';
32 import { NotificationService } from './../services/ui/notification.service';
33 import { AnrEditNcrDialogComponent } from './anr-edit-ncr-dialog.component';
34 import { ANRXappDataSource } from './anr-xapp.datasource';
35 import { UiService } from '../services/ui/ui.service';
36
37 @Component({
38   selector: 'rd-anr',
39   templateUrl: './anr-xapp.component.html',
40   styleUrls: ['./anr-xapp.component.scss']
41 })
42 export class AnrXappComponent implements AfterViewInit, OnInit {
43
44   darkMode: boolean;
45   panelClass: string = "";
46   dataSource: ANRXappDataSource;
47   gNodeBIds: string[];
48   @ViewChild('ggNodeB', { static: true }) ggNodeB: ElementRef;
49   @ViewChild('servingCellNrcgi', { static: true }) servingCellNrcgi: ElementRef;
50   @ViewChild('neighborCellNrpci', { static: true }) neighborCellNrpci: ElementRef;
51   @ViewChild(MatSort, { static: true }) sort: MatSort;
52
53   displayedColumns = ['cellIdentifierNrcgi', 'neighborCellNrpci', 'neighborCellNrcgi',
54     'flagNoHo', 'flagNoXn', 'flagNoRemove', 'action'];
55
56   constructor(
57     private anrXappService: ANRXappService,
58     private dialog: MatDialog,
59     private confirmDialogService: ConfirmDialogService,
60     private errorDialogService: ErrorDialogService,
61     private loadingDialogService: LoadingDialogService,
62     private notificationService: NotificationService,
63     public ui: UiService) { }
64
65   ngOnInit() {
66     this.dataSource = new ANRXappDataSource(this.anrXappService, this.sort, this.notificationService);
67     this.dataSource.loadTable();
68     // Empty string occurs first in the array of gNodeBIds
69     this.anrXappService.getgNodeBs().subscribe((res: string[]) => this.gNodeBIds = res);
70     this.ui.darkModeState.subscribe((isDark) => {
71       this.darkMode = isDark;
72     });
73   }
74
75   ngAfterViewInit() {
76     // the selector event calls loadNcrtPage() directly.
77     fromEvent(this.servingCellNrcgi.nativeElement, 'keyup')
78       .pipe(
79         debounceTime(150),
80         distinctUntilChanged(),
81         tap(() => {
82           this.loadNcrtPage();
83         })
84       )
85       .subscribe();
86     fromEvent(this.neighborCellNrpci.nativeElement, 'keyup')
87       .pipe(
88         debounceTime(150),
89         distinctUntilChanged(),
90         tap(() => {
91           this.loadNcrtPage();
92         })
93       )
94       .subscribe();
95   }
96
97   loadNcrtPage() {
98     this.dataSource.loadTable(
99       this.ggNodeB.nativeElement.value,
100       this.servingCellNrcgi.nativeElement.value,
101       this.neighborCellNrpci.nativeElement.value);
102   }
103
104   modifyNcr(ncr: ANRNeighborCellRelation): void {
105     if (this.darkMode) {
106       this.panelClass = "dark-theme";
107     } else {
108       this.panelClass = "";
109     }
110     const dialogRef = this.dialog.open(AnrEditNcrDialogComponent, {
111       panelClass: this.panelClass,
112       width: '300px',
113       data: ncr
114     });
115     dialogRef.afterClosed().subscribe(
116       (result: any) => {
117         this.loadNcrtPage();
118       }
119     );
120   }
121
122   deleteNcr(ncr: ANRNeighborCellRelation): void {
123     this.confirmDialogService
124       .openConfirmDialog('Are you sure you want to delete this relation?')
125       .afterClosed().subscribe(
126         (res: any) => {
127           if (res) {
128             this.loadingDialogService.startLoading("Deleting");
129             this.anrXappService.deleteNcr(ncr.servingCellNrcgi, ncr.neighborCellNrpci)
130               .pipe(
131                 finalize(() => this.loadingDialogService.stopLoading())
132               )
133               .subscribe(
134                 (response: HttpResponse<Object>) => {
135                   switch (response.status) {
136                     case 200:
137                       this.notificationService.success('Delete succeeded!');
138                       this.loadNcrtPage();
139                       break;
140                     default:
141                       this.notificationService.warn('Delete failed.');
142                   }
143                 },
144                 (error: HttpErrorResponse) => {
145                   this.errorDialogService.displayError(error.message);
146                 });
147           }
148         });
149   }
150
151 }