Added new componetnts inside page modules
[portal.git] / portal-FE-common / src / app / pages / user-notification-admin / user-notification-admin.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 } from '@angular/core';
40 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
41 import { MatTableDataSource } from '@angular/material';
42 import { MatSort, MatPaginator } from '@angular/material';
43 import { NotificationService } from '../../shared/services/index';
44 import { NewNotificationModalComponent } from './new-notification-modal/new-notification-modal.component';
45 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
46 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
47
48 @Component({
49   selector: 'app-user-notification-admin',
50   templateUrl: './user-notification-admin.component.html',
51   styleUrls: ['./user-notification-admin.component.scss']
52 })
53 export class UserNotificationAdminComponent implements OnInit {
54
55   isEditMode: any;
56   result: any;
57   tableAdminNotifItems: any = [];
58   displayedColumns: string[] = ['messageSource', 'message', 'startDateLocalTime','endDateLocalTime', 'priority', 'createdBy', 'createdTime', 'allUsersRoles', 'viewOrDelete'];
59   notificationsDataSource = new MatTableDataSource(this.tableAdminNotifItems);
60   @ViewChild(MatSort) sort: MatSort;
61   @ViewChild(MatPaginator) paginator: MatPaginator;
62
63   constructor(public notificationService: NotificationService, public ngbModal: NgbModal) { }
64
65   ngOnInit() {
66     this.getAdminNotifications();
67   }
68
69   getAdminNotifications(){
70     this.notificationService.getAdminNotification()
71     .subscribe(_data => {
72         this.result = _data;
73         if (this.result == null || this.result == 'undefined') {
74           console.log('NotificationService::getAdminNotifications Failed:::: Result or result.data is null');
75         }else {
76           this.tableAdminNotifItems = this.result;
77           this.populateTableData(this.tableAdminNotifItems);
78         }
79     }, error =>{
80       console.log(error);
81       this.openConfirmationModal('Error', error);
82       return;
83     });
84   }
85
86   removeUserNotification(selectedAdminNotification: any){ 
87     let confirmationMsg = 'You are about to delete this Notification : ' + selectedAdminNotification.msgHeader+ '. Click OK to continue.';
88     this.openInformationModal("Confirmation",confirmationMsg).result.then((result) => {
89       if (result === 'Ok') {
90         selectedAdminNotification.activeYn = 'N';
91         this.notificationService.updateAdminNotification(selectedAdminNotification)
92         .subscribe(_data => {
93             this.result = _data;
94             this.tableAdminNotifItems = [];
95             this.getAdminNotifications();
96         }, error =>{
97           console.log(error);
98           this.openConfirmationModal('Error', error);
99           return;
100         });
101       }
102     }, (resut) => {
103       this.openConfirmationModal('Error', resut);
104       return;
105     })
106   }
107
108
109   openAddNewNotificationModal(rowData: any){
110     const modalRef = this.ngbModal.open(NewNotificationModalComponent, { windowClass: 'add-notification-modal'});
111     modalRef.componentInstance.title = 'Add a New Notification';
112     if(rowData != 'undefined' && rowData){
113       modalRef.componentInstance.selectedNotification = rowData;
114       this.isEditMode = true;
115     }else{
116       modalRef.componentInstance.notification  = {};
117       this.isEditMode = false;
118     }
119     modalRef.componentInstance.passEntry.subscribe((receivedEntry: any) => {
120       if(receivedEntry){
121         this.tableAdminNotifItems = [];
122         this.getAdminNotifications();
123       }
124     });
125   }
126
127   populateTableData(notificationHistory: Array<Object>){
128     this.notificationsDataSource = new MatTableDataSource(notificationHistory);
129     this.notificationsDataSource.sort = this.sort;
130     this.notificationsDataSource.paginator = this.paginator;
131   }
132
133   applyFilter(filterValue: string) {
134     this.notificationsDataSource.filter = filterValue.trim().toLowerCase();
135   }
136
137   openConfirmationModal(_title: string, _message: string) {
138     const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
139     modalInfoRef.componentInstance.title = _title;
140     modalInfoRef.componentInstance.message = _message;
141   }
142
143   openInformationModal(_title: string, _message: string){
144     const modalInfoRef = this.ngbModal.open(InformationModalComponent);
145     modalInfoRef.componentInstance.title = _title;
146     modalInfoRef.componentInstance.message = _message;
147     return modalInfoRef;
148  }
149 }