App Onboarding - Delete app not working
[portal.git] / portal-FE-os / src / app / pages / application-onboarding / application-onboarding.component.ts
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38
39 import { Component, OnInit, ViewChild, Input} from '@angular/core';
40 import { MatTableDataSource } from '@angular/material';
41 import { MatSort, MatPaginator } from '@angular/material';
42 import { ApplicationsService } from '../../shared/services/index';
43 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
44 import { IApplications } from 'src/app/shared/model/applications-onboarding/applications';
45 import { environment } from '../../../environments/environment';
46 import { ApplicationDetailsDialogComponent } from './application-details-dialog/application-details-dialog.component';
47 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
48 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
49
50 @Component({
51   selector: 'app-application-onboarding',
52   templateUrl: './application-onboarding.component.html',
53   styleUrls: ['./application-onboarding.component.scss']
54 })
55 export class ApplicationOnboardingComponent implements OnInit {
56
57   api = environment.api;
58   appsList: Array<IApplications> = [];
59   result: any;
60   isEditMode: boolean = false;
61   emptyImgForPreview: string;
62   isUserSuperAdmin: boolean = false;
63   displayedColumns: string[] = ['thumbnail', 'applicationName','active', 
64   'integrationType', 'modeOfIntegration', 'guestAccess', 'landingPage','restURL',
65   'communicationKey', 'applicationNamespace', 'centralAuthAccess'];
66   dataSource = new MatTableDataSource(this.appsList);
67   @ViewChild(MatSort) sort: MatSort;
68   @ViewChild(MatPaginator) paginator: MatPaginator;
69
70   constructor(public applicationsService: ApplicationsService, public ngbModal: NgbModal) { }
71
72   ngOnInit() {
73     this.emptyImgForPreview = '../../../assets/images/default_app_image.gif';
74     this.checkIfUserIsSuperAdmin();
75     this.getOnboardingApps();
76   }
77
78   getOnboardingApps(){
79     //console.log("getOnboardingApps called");
80     this.applicationsService.getOnboardingApps()
81       .subscribe(_data => {
82           this.result = _data;
83           if (this.result == null || this.result == 'undefined') {
84               console.log('WidgetOnboardingService::getOnboardingWidgets Failed: Result or result.data is null');
85           }else {
86             this.appsList = _data;
87             for (var i = 0; i < this.appsList.length; i++) {
88               this.appsList[i].imageLink = '';
89               if (this.appsList[i].imageUrl){
90                 this.appsList[i].imageLink = this.api.appThumbnail.replace(':appId', this.appsList[i].id);
91                 this.appsList[i].imageLink = this.appsList[i].imageLink+'?' + new Date().getTime();
92               }else{
93                 this.appsList[i].imageLink = this.emptyImgForPreview;
94               }                                         
95             }
96             this.populateTableData(this.appsList);
97           }
98       }, error =>{
99         console.log(error);
100         this.openConfirmationModal('Error', error.message);
101     });
102   }
103
104   applyFilter(filterValue: string) {
105     this.dataSource.filter = filterValue.trim().toLowerCase();
106   };
107
108   
109   populateTableData(appsList: Array<IApplications>){
110     this.dataSource = new MatTableDataSource(appsList);
111     this.dataSource.sort = this.sort;
112     this.dataSource.paginator = this.paginator;
113   };
114
115   openAddApplicationModal(rowData: any, action:any) {
116     console.log("Action : ", action);
117     const modalRef = this.ngbModal.open(ApplicationDetailsDialogComponent, { size: 'lg', backdrop: 'static', keyboard: false });
118     modalRef.componentInstance.title = 'Application Details';
119     modalRef.componentInstance.action = action;
120     //console.log("selectedData in parent",rowData);
121     if(rowData != 'undefined' && rowData){
122       modalRef.componentInstance.applicationObj = rowData;
123       this.isEditMode = true;
124     }else{
125       modalRef.componentInstance.applicationObj  = {};
126       this.isEditMode = false;
127     }
128     modalRef.componentInstance.passEntry.subscribe((receivedEntry: any) => {
129       //console.log("receivedEntry >>> ",receivedEntry);
130       if(receivedEntry){
131         this.appsList.push(receivedEntry);
132         //this.populateTableData(this.appsList);
133         this.getOnboardingApps();
134       }
135     });
136   }
137
138   deleteApplication(application: IApplications){
139     let confirmationMsg = 'You are about to delete this App : ' + application.appName+ '. Click OK to continue.';
140     this.openInformationModal("Confirmation",confirmationMsg).result.then((result) => {
141       if (result === 'Ok') {
142         if(!application || application == null){
143           console.log('ApplicationOnboardingCtrl::deleteApplication: No apllication or ID... cannot delete');
144           return;
145         }
146         this.appsList.splice(this.appsList.indexOf(application), 1);
147         this.applicationsService.deleteOnboardingApp(application.id)
148           .subscribe( data => {
149             this.result = data;
150             this.getOnboardingApps();
151           }, error => {
152             console.log(error);
153             if(error && error.status == 405){
154               this.openConfirmationModal('', 'Application : ' + application.appName+ ' can not be deleted as it is associsted with one of the Microservices.');
155             }else{
156               this.openConfirmationModal('Error', error.message);
157             }
158         });
159       }
160     }, (resut) => {
161       return;
162     })
163   }
164
165   checkIfUserIsSuperAdmin(){
166     this.applicationsService.checkIfUserIsSuperAdmin()
167       .subscribe(res => {
168         if(res) {
169           this.isUserSuperAdmin = true;
170           this.displayedColumns = ['thumbnail', 'applicationName','active', 
171           'integrationType', 'modeOfIntegration', 'guestAccess', 'landingPage','restURL',
172           'communicationKey', 'applicationNamespace', 'centralAuthAccess', 'delete'];
173         }  
174         //console.log("isUserSuperAdmin :: ",this.isUserSuperAdmin);
175       }, error =>{
176         console.log(error);
177         this.openConfirmationModal('Error', 'ApplicationsCtrl.checkIfUserIsSuperAdmin:: Failed '+error.message);
178     });
179   }
180
181   openConfirmationModal(_title: string, _message: string) {
182     const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
183     modalInfoRef.componentInstance.title = _title;
184     modalInfoRef.componentInstance.message = _message;
185   }
186
187   openInformationModal(_title: string, _message: string){
188     const modalInfoRef = this.ngbModal.open(InformationModalComponent);
189     modalInfoRef.componentInstance.title = _title;
190     modalInfoRef.componentInstance.message = _message;
191     return modalInfoRef;
192   }
193
194 }