Added new componetnts inside page modules
[portal.git] / portal-FE-common / src / app / pages / role / role-functions / role-function-modal / role-function-modal.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 { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
39 import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
40 import { RoleFunction, Role } from 'src/app/shared/model';
41 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
42 import { environment } from 'src/environments/environment';
43 import { HttpClient } from '@angular/common/http';
44 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
45
46 @Component({
47   selector: 'app-role-function-modal',
48   templateUrl: './role-function-modal.component.html',
49   styleUrls: ['./role-function-modal.component.scss']
50 })
51 export class RoleFunctionModalComponent implements OnInit {
52
53   @Input() title: string;
54   @Input() appId: any;
55   @Input() dialogState: number;
56   @Input() currentRoleFunctions: any;
57   @Input() editRoleFunction: RoleFunction;
58   @Output() passBackRoleFunctionPopup: EventEmitter<any> = new EventEmitter();
59   roleFunction: RoleFunction;
60   otherTypeValue: string;
61   typeOptions: string[] = ['menu', 'url', 'other'];
62   api = environment.api;
63   isEditing: any;
64   editDisable: boolean;
65   showSpinner: boolean;
66   selectedType: string;
67   createOrUpdate: string;
68   constructor(public activeModal: NgbActiveModal, public ngbModal: NgbModal, public http: HttpClient) { }
69
70   ngOnInit() {
71     this.createOrUpdate = 'create';
72     this.selectedType = 'menu';
73     this.roleFunction = new RoleFunction(this.selectedType, '', '*', '');
74     this.otherTypeValue = '';
75     if (this.editRoleFunction) {
76       this.createOrUpdate = 'update';
77       this.editDisable = true;
78       this.selectedType = this.editRoleFunction.type;
79       if (this.editRoleFunction.type !== 'menu' && this.editRoleFunction.type !== 'url') {
80         this.selectedType = 'other';
81         this.otherTypeValue = this.editRoleFunction.type;
82       }
83       this.roleFunction = new RoleFunction(this.editRoleFunction.type, this.editRoleFunction.code, this.editRoleFunction.action, this.editRoleFunction.name);
84     }
85   }
86
87   saveRoleFunction() {
88     if (/[^a-zA-Z0-9\-\.\_]/.test(this.roleFunction.type) && this.selectedType === 'other') {
89       this.openConfirmationModal('Confirmation', 'Type can only contain alphanumeric characters, dots(.) and underscores(_)');
90       return;
91     } else {
92       this.roleFunction.type = this.selectedType;
93     }
94     if (this.roleFunction.action !== '*' && /[^a-zA-Z0-9\-\.\_]/.test(this.roleFunction.action)) {
95       this.openConfirmationModal('Confirmation', 'Action can only contain alphanumeric characters, hyphens(-), dots(.) and underscores(_) and single asterisk character(*)');
96       return;
97     }
98     if (/[^a-zA-Z0-9\-\:\_\./*]/.test(this.roleFunction.code)) {
99       this.openConfirmationModal('Confirmation', 'Instance can only contain alphanumeric characters, hyphens(-), dots(.), colons(:), forwardSlash(/) , asterisk(*) and underscores(_)');
100       return;
101     }
102     const modalInfoRef = this.ngbModal.open(InformationModalComponent);
103     modalInfoRef.componentInstance.title = 'Confirmation';
104     modalInfoRef.componentInstance.message = 'You are about to ' + this.createOrUpdate + ' the role function ' + this.roleFunction.name + '. Do you want to continue?';
105     modalInfoRef.result.then((_res) => {
106       if (_res === 'Ok') {
107         this.showSpinner = true;
108         var uuu = this.api.saveRoleFunction.replace(':appId', this.appId);
109         var postData = this.roleFunction;
110         var exists = false, x;
111         for (x in this.currentRoleFunctions) {
112           if (this.currentRoleFunctions[x].type == this.roleFunction.type
113             && this.currentRoleFunctions[x].code == this.roleFunction.code
114             && this.currentRoleFunctions[x].action == this.roleFunction.action
115             && this.currentRoleFunctions[x].name == this.roleFunction.name) {
116             this.openConfirmationModal('Confirmation', "Role Function already exist.");
117             exists = true;
118             this.showSpinner = false;
119             break;
120           }
121           if (!this.editDisable) {
122             if (this.currentRoleFunctions[x].type == this.roleFunction.type
123               && this.currentRoleFunctions[x].code == this.roleFunction.code
124               && this.currentRoleFunctions[x].action == this.roleFunction.action
125             ) {
126               this.openConfirmationModal('Confirmation', "Please make sure code, type and action is unique. Please create a role function with a different code or type or action to proceed.");
127               exists = true;
128               this.showSpinner = false;
129               break;
130             }
131           }
132         }
133         if (this.selectedType === 'other')
134           this.roleFunction.type = this.otherTypeValue;
135         if (!exists && this.roleFunction.name.trim() != '' && this.roleFunction.code.trim() != '') {
136           this.http.post(uuu, JSON.stringify(postData)).toPromise().then((res: any) => {
137             if (res.status == 'OK') {
138               this.showSpinner = false;
139               if (this.editRoleFunction) {
140                 this.editRoleFunction.name = this.roleFunction.name;
141                 this.passBackRoleFunctionPopup.emit(this.editRoleFunction);
142               } else{
143                 this.passBackRoleFunctionPopup.emit(this.roleFunction);
144               }
145               this.openConfirmationModal('Success', res.message);
146             } else {
147               this.showSpinner = false;
148               this.openConfirmationModal('Error', res.message);
149             }
150           });
151
152         }
153       }
154     }, (_dismiss) => {
155
156     })
157   }
158
159
160   openConfirmationModal(_title: string, _message: string) {
161     const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
162     modalInfoRef.componentInstance.title = _title;
163     modalInfoRef.componentInstance.message = _message;
164   }
165 }