Reorganize dashboard into subfolders
[portal/ric-dashboard.git] / dashboard / webapp-frontend / src / app / catalog / catalog.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 import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
21 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
22 import { MatDialog } from '@angular/material/dialog';
23 import { MatSort } from '@angular/material/sort';
24 import { Subscription } from 'rxjs';
25 import { finalize } from 'rxjs/operators';
26 import { RicInstance } from '../interfaces/dashboard.types';
27 import { XMDeployableApp } from '../interfaces/app-mgr.types';
28 import { AppMgrService } from '../services/app-mgr/app-mgr.service';
29 import { InstanceSelectorService } from '../services/instance-selector/instance-selector.service';
30 import { LoadingDialogService } from '../services/ui/loading-dialog.service';
31 import { UiService } from '../services/ui/ui.service';
32 import { AppConfigurationComponent } from './../app-configuration/app-configuration.component';
33 import { ConfirmDialogService } from '../services/ui/confirm-dialog.service';
34 import { NotificationService } from '../services/ui/notification.service';
35 import { CatalogDataSource } from './catalog.datasource';
36
37 @Component({
38   selector: 'rd-app-catalog',
39   templateUrl: './catalog.component.html',
40   styleUrls: ['./catalog.component.scss'],
41 })
42 export class CatalogComponent implements OnInit, OnDestroy {
43
44   darkMode: boolean;
45   panelClass: string;
46   displayedColumns: string[] = ['name', 'version', 'action'];
47   dataSource: CatalogDataSource;
48   private instanceChange: Subscription;
49   private instanceKey: string;
50
51   @ViewChild(MatSort, { static: true }) sort: MatSort;
52
53   constructor(
54     private appMgrService: AppMgrService,
55     private confirmDialogService: ConfirmDialogService,
56     private dialog: MatDialog,
57     private loadingDialogService: LoadingDialogService,
58     private notificationService: NotificationService,
59     public instanceSelectorService: InstanceSelectorService,
60     public ui: UiService) { }
61
62   ngOnInit() {
63     this.dataSource = new CatalogDataSource(this.appMgrService, this.sort, this.notificationService);
64     this.ui.darkModeState.subscribe((isDark) => {
65       this.darkMode = isDark;
66     });
67
68     this.instanceChange = this.instanceSelectorService.getSelectedInstance().subscribe((instance: RicInstance) => {
69       if (instance.key) {
70         this.instanceKey = instance.key;
71         this.dataSource.loadTable(instance.key);
72       }
73     });
74   }
75
76   ngOnDestroy() {
77     this.instanceChange.unsubscribe();
78   }
79
80   onConfigureApp(xapp: XMDeployableApp): void {
81     if (this.darkMode) {
82       this.panelClass = 'dark-theme';
83     } else {
84       this.panelClass = '';
85     }
86     const dialogRef = this.dialog.open(AppConfigurationComponent, {
87       panelClass: this.panelClass,
88       width: '40%',
89       maxHeight: '500px',
90       position: {
91         top: '10%'
92       },
93       data: {
94         xapp: xapp,
95         instanceKey: this.instanceKey
96       }
97
98     });
99   }
100
101   onDeployApp(app: XMDeployableApp): void {
102     this.confirmDialogService.openConfirmDialog('Deploy application ' + app.name + '?')
103       .afterClosed().subscribe((res: boolean) => {
104         if (res) {
105           this.loadingDialogService.startLoading('Deploying ' + app.name);
106           this.appMgrService.deployXapp(this.instanceKey, app.name)
107             .pipe(
108               finalize(() => this.loadingDialogService.stopLoading())
109             )
110             .subscribe(
111               (response: HttpResponse<Object>) => {
112                 this.notificationService.success('App deploy succeeded!');
113               },
114               ((her: HttpErrorResponse) => {
115                 // the error field should have an ErrorTransport object
116                 let msg = her.message;
117                 if (her.error && her.error.message) {
118                   msg = her.error.message;
119                 }
120                 this.notificationService.warn('App deploy failed: ' + msg);
121               })
122             );
123         }
124       }
125       );
126   }
127
128 }