Adding new components with portal-FE-common
[portal.git] / portal-FE-common / src / app / pages / portal-admins / portal-admins.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 import { OnInit, Component, ViewChild, NgModuleRef } from '@angular/core';
39 import { MatSort, MatPaginator } from '@angular/material';
40 import { MatTableDataSource } from '@angular/material';
41 import { PortalAdminsService } from 'src/app/shared/services';
42 import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
43 import { NewPortalAdminComponent } from './new-portal-admin/new-portal-admin.component';
44 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
45 import { PortalAdmin } from 'src/app/shared/model/PortalAdmin';
46 import { HttpErrorResponse } from '@angular/common/http';
47 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
48
49 @Component({
50   selector: 'app-portal-admins',
51   templateUrl: './portal-admins.component.html',
52   styleUrls: ['./portal-admins.component.scss']
53 })
54 export class PortalAdminsComponent implements OnInit {
55   portalAdmins: PortalAdmin[] = [];
56   showSpinner = true;
57   closeResult: string;
58   constructor(private portalAdminsService: PortalAdminsService, private ngbModal: NgbModal) { }
59   displayedColumns: string[] = ['firstName', 'lastName', 'loginId', 'delete'];
60   dataSource = new MatTableDataSource(this.portalAdmins);
61   @ViewChild(MatSort) sort: MatSort;
62   @ViewChild(MatPaginator) paginator: MatPaginator;
63   ngOnInit() {
64     this.getAllPortalAdmins();
65   }
66
67   getAllPortalAdmins() {
68     this.portalAdminsService.getPortalAdmins().subscribe((_data: PortalAdmin[]) => {
69       this.showSpinner = false;
70       // _data is the array of data that you getting from the db.
71       this.portalAdmins = _data;
72       this.dataSource = new MatTableDataSource(this.portalAdmins);
73       this.dataSource.sort = this.sort;
74       this.dataSource.paginator = this.paginator;
75     }, (_err: HttpErrorResponse) =>{
76       const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
77       modalErrorRef.componentInstance.title = "Error";
78       if (_err.status) {    //Conflict
79         modalErrorRef.componentInstance.message = 'Error Status: ' + _err.status + ' There was a unknown problem adding the portal admin.' + 'Please try again later.';
80       }
81     })
82   }
83
84   applyFilter(filterValue: string) {
85     this.dataSource.filter = filterValue.trim().toLowerCase();
86   }
87
88   addPortalAdminEntry() {
89     const modalRef = this.ngbModal.open(NewPortalAdminComponent);
90     modalRef.componentInstance.title = 'Add Portal Admin';
91     modalRef.componentInstance.id = 1;
92     modalRef.componentInstance.passBackNewPortalAdmin.subscribe((_result: any) => {
93       modalRef.close();
94       this.showSpinner = true;
95       this.getAllPortalAdmins();
96     })
97   }
98
99   removePortalAdmin(deletePortalAdmin: any) {
100     const modalRef = this.ngbModal.open(InformationModalComponent);
101     modalRef.componentInstance.title = 'Confirmation';
102     modalRef.componentInstance.message = `Are you sure you want to delete ${deletePortalAdmin.firstName} ${deletePortalAdmin.lastName} ?`;
103     modalRef.result.then((result) => {
104       if (result === 'Ok') {
105         this.portalAdminsService.removePortalAdmin(deletePortalAdmin.userId, deletePortalAdmin.loginId).subscribe(_data => {
106           this.showSpinner = true;
107           this.getAllPortalAdmins();
108         })
109       }
110     }, (reason) => {
111       this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
112     });
113   }
114
115   private getDismissReason(reason: any): string {
116     if (reason === ModalDismissReasons.ESC) {
117       return 'by pressing ESC';
118     } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
119       return 'by clicking on a backdrop';
120     } else {
121       return `with: ${reason}`;
122     }
123   }
124 }