4f45f8186d3c299c2ecd81bbdae7740652f5d870
[osm/NG-UI.git] / src / app / utilities / users-action / UsersActionComponent.ts
1 /*
2  Copyright 2020 TATA ELXSI
3
4  Licensed under the Apache License, Version 2.0 (the 'License');
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15
16  Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17 */
18 /**
19  * @file Users Action Component
20  */
21 import { Component, Injector } from '@angular/core';
22 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
23 import { TranslateService } from '@ngx-translate/core';
24 import { AddEditUserComponent } from 'AddEditUserComponent';
25 import { MODALCLOSERESPONSEDATA } from 'CommonModel';
26 import { DeleteComponent } from 'DeleteComponent';
27 import { ProjectRoleComponent } from 'ProjectRoleComponent';
28 import { SharedService, isNullOrUndefined } from 'SharedService';
29 import { UserData } from 'UserModel';
30 import { WarningComponent } from 'WarningComponent';
31 /**
32  * Creating component
33  * @Component takes UsersActionComponent.html as template url
34  */
35 @Component({
36     templateUrl: './UsersActionComponent.html',
37     styleUrls: ['./UsersActionComponent.scss']
38 })
39 /** Exporting a class @exports UsersActionComponent */
40 export class UsersActionComponent {
41     /** To inject services @public */
42     public injector: Injector;
43
44     /** To get the value from the Users action via valuePrepareFunction default Property of ng-smarttable @public */
45     public value: UserData;
46
47     /** handle translate @public */
48     public translateService: TranslateService;
49
50     /** Admin Visibility Check @public */
51     public isAdminShow: boolean;
52
53     /** User Visibility Check @public */
54     public isUserShow: boolean;
55
56     /** User Status Check @public */
57     public isUserStatus: string;
58
59     /** Instance of the modal service @private */
60     private modalService: NgbModal;
61
62     /** Contains all methods related to shared @private */
63     private sharedService: SharedService;
64
65     constructor(injector: Injector) {
66         this.injector = injector;
67         this.modalService = this.injector.get(NgbModal);
68         this.sharedService = this.injector.get(SharedService);
69         this.translateService = this.injector.get(TranslateService);
70     }
71
72     /**
73      * Lifecyle Hooks the trigger before component is instantiate
74      */
75     public ngOnInit(): void {
76         this.isAdminShow = sessionStorage.getItem('admin_show') === 'true' ? true : false;
77         this.isUserShow = sessionStorage.getItem('user_show') === 'true' ? true : false;
78         if (!isNullOrUndefined(this.value.user_status)) {
79             this.isUserStatus = this.value.user_status;
80         }
81     }
82
83     /** Delete User Account @public */
84     public deleteUser(): void {
85         // eslint-disable-next-line security/detect-non-literal-fs-filename
86         const modalRef: NgbModalRef = this.modalService.open(DeleteComponent, { backdrop: 'static' });
87         modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
88             if (result) {
89                 this.sharedService.callData();
90             }
91         }).catch((): void => {
92             // Catch Navigation Error
93         });
94     }
95
96     /** Edit User Account @public */
97     public editUserModal(editType: string): void {
98         // eslint-disable-next-line security/detect-non-literal-fs-filename
99         const modalRef: NgbModalRef = this.modalService.open(AddEditUserComponent, { backdrop: 'static' });
100         modalRef.componentInstance.userID = this.value.identifier;
101         if (editType === 'editPassword') {
102             modalRef.componentInstance.userTitle = this.translateService.instant('PAGE.USERS.EDITCREDENTIALS');
103         } else if (editType === 'editmail') {
104             modalRef.componentInstance.userTitle = this.translateService.instant('PAGE.USERS.CHANGEMAIL');
105         } else {
106             modalRef.componentInstance.userTitle = this.translateService.instant('PAGE.USERS.EDITUSERNAME');
107         }
108         modalRef.componentInstance.userType = editType;
109         modalRef.componentInstance.userName = this.value.username;
110         modalRef.componentInstance.email = this.value.email_id;
111         modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
112             if (result) {
113                 this.sharedService.callData();
114             }
115         }).catch((): void => {
116             // Catch Navigation Error
117         });
118     }
119
120     /** Edit User Account @public */
121     public projectRolesModal(): void {
122         // eslint-disable-next-line security/detect-non-literal-fs-filename
123         const modalRef: NgbModalRef = this.modalService.open(ProjectRoleComponent, { backdrop: 'static' });
124         modalRef.componentInstance.userID = this.value.identifier;
125         modalRef.componentInstance.userTitle = this.translateService.instant('PAGE.USERS.EDITPROJECTROLEMAPPING');
126         modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
127             if (result) {
128                 this.sharedService.callData();
129             }
130         }).catch((): void => {
131             // Catch Navigation Error
132         });
133     }
134
135     /** To Unlock or Renew User @public */
136     public unlockRenewUser(editType: string): void {
137         // eslint-disable-next-line security/detect-non-literal-fs-filename
138         const modalRef: NgbModalRef = this.modalService.open(WarningComponent, { backdrop: 'static' });
139         sessionStorage.setItem('renew', 'true');
140         const id: string = sessionStorage.getItem('user_id');
141         if (editType === 'unlock') {
142             modalRef.componentInstance.heading = this.translateService.instant('Unlock User');
143             modalRef.componentInstance.confirmationMessage = this.translateService.instant('Are you sure want to unlock this user');
144             modalRef.componentInstance.submitMessage = this.translateService.instant('Unlock');
145             modalRef.componentInstance.action = Boolean(true);
146             modalRef.componentInstance.editType = editType;
147             modalRef.componentInstance.id = this.value.identifier;
148         } else {
149             modalRef.componentInstance.heading = this.translateService.instant('Renew User');
150             modalRef.componentInstance.confirmationMessage = this.translateService.instant('Are you sure want to renew this user');
151             modalRef.componentInstance.submitMessage = this.translateService.instant('Renew');
152             modalRef.componentInstance.action = Boolean(true);
153             modalRef.componentInstance.editType = editType;
154             modalRef.componentInstance.id = this.value.identifier;
155         }
156         modalRef.result.then((result: MODALCLOSERESPONSEDATA): void => {
157             if (result) {
158                 this.sharedService.callData();
159             }
160         }).catch((): void => {
161             // Catch Navigation Error
162         });
163     }
164 }