04fd58a5dac678adb2879e5be9d4856ed9dd4ac2
[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 { isNullOrUndefined } from 'util';
22 import { Component, Injector } from '@angular/core';
23 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
24 import { TranslateService } from '@ngx-translate/core';
25 import { AddEditUserComponent } from 'AddEditUserComponent';
26 import { MODALCLOSERESPONSEDATA } from 'CommonModel';
27 import { DeleteComponent } from 'DeleteComponent';
28 import { ProjectRoleComponent } from 'ProjectRoleComponent';
29 import { SharedService } from 'SharedService';
30 import { UserData } from 'UserModel';
31 import { WarningComponent } from 'WarningComponent';
32 /**
33  * Creating component
34  * @Component takes UsersActionComponent.html as template url
35  */
36 @Component({
37     templateUrl: './UsersActionComponent.html',
38     styleUrls: ['./UsersActionComponent.scss']
39 })
40 /** Exporting a class @exports UsersActionComponent */
41 export class UsersActionComponent {
42     /** To inject services @public */
43     public injector: Injector;
44
45     /** To get the value from the Users action via valuePrepareFunction default Property of ng-smarttable @public */
46     public value: UserData;
47
48     /** handle translate @public */
49     public translateService: TranslateService;
50
51     /** Admin Visibility Check @public */
52     public isAdminShow: boolean;
53
54     /** User Visibility Check @public */
55     public isUserShow: boolean;
56
57     /** User Status Check @public */
58     public isUserStatus: string;
59
60     /** Instance of the modal service @private */
61     private modalService: NgbModal;
62
63     /** Contains all methods related to shared @private */
64     private sharedService: SharedService;
65
66     constructor(injector: Injector) {
67         this.injector = injector;
68         this.modalService = this.injector.get(NgbModal);
69         this.sharedService = this.injector.get(SharedService);
70         this.translateService = this.injector.get(TranslateService);
71     }
72
73     /**
74      * Lifecyle Hooks the trigger before component is instantiate
75      */
76     public ngOnInit(): void {
77         this.isAdminShow = localStorage.getItem('admin_show') === 'true' ? true : false;
78         this.isUserShow = localStorage.getItem('user_show') === 'true' ? true : false;
79         if (!isNullOrUndefined(this.value.user_status)) {
80             this.isUserStatus = this.value.user_status;
81         }
82     }
83
84     /** Delete User Account @public */
85     public deleteUser(): void {
86         // eslint-disable-next-line security/detect-non-literal-fs-filename
87         const modalRef: NgbModalRef = this.modalService.open(DeleteComponent, { backdrop: 'static' });
88         modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
89             if (result) {
90                 this.sharedService.callData();
91             }
92         }).catch((): void => {
93             // Catch Navigation Error
94         });
95     }
96
97     /** Edit User Account @public */
98     public editUserModal(editType: string): void {
99         // eslint-disable-next-line security/detect-non-literal-fs-filename
100         const modalRef: NgbModalRef = this.modalService.open(AddEditUserComponent, { backdrop: 'static' });
101         modalRef.componentInstance.userID = this.value.identifier;
102         if (editType === 'editPassword') {
103             modalRef.componentInstance.userTitle = this.translateService.instant('PAGE.USERS.EDITCREDENTIALS');
104         } else {
105             modalRef.componentInstance.userTitle = this.translateService.instant('PAGE.USERS.EDITUSERNAME');
106         }
107         modalRef.componentInstance.userType = editType;
108         modalRef.componentInstance.userName = this.value.username;
109         modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
110             if (result) {
111                 this.sharedService.callData();
112             }
113         }).catch((): void => {
114             // Catch Navigation Error
115         });
116     }
117
118     /** Edit User Account @public */
119     public projectRolesModal(): void {
120         // eslint-disable-next-line security/detect-non-literal-fs-filename
121         const modalRef: NgbModalRef = this.modalService.open(ProjectRoleComponent, { backdrop: 'static' });
122         modalRef.componentInstance.userID = this.value.identifier;
123         modalRef.componentInstance.userTitle = this.translateService.instant('PAGE.USERS.EDITPROJECTROLEMAPPING');
124         modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
125             if (result) {
126                 this.sharedService.callData();
127             }
128         }).catch((): void => {
129             // Catch Navigation Error
130         });
131     }
132
133     /** To Unlock or Renew User @public */
134     public unlockRenewUser(editType: string): void {
135         // eslint-disable-next-line security/detect-non-literal-fs-filename
136         const modalRef: NgbModalRef = this.modalService.open(WarningComponent, { backdrop: 'static' });
137         localStorage.setItem('renew', 'true');
138         const id: string = localStorage.getItem('user_id');
139         if (editType === 'unlock') {
140             modalRef.componentInstance.heading = this.translateService.instant('Unlock User');
141             modalRef.componentInstance.confirmationMessage = this.translateService.instant('Are you sure want to unlock this user');
142             modalRef.componentInstance.submitMessage = this.translateService.instant('Unlock');
143             modalRef.componentInstance.action = Boolean(true);
144             modalRef.componentInstance.editType = editType;
145             modalRef.componentInstance.id = this.value.identifier;
146         } else {
147             modalRef.componentInstance.heading = this.translateService.instant('Renew User');
148             modalRef.componentInstance.confirmationMessage = this.translateService.instant('Are you sure want to renew this user');
149             modalRef.componentInstance.submitMessage = this.translateService.instant('Renew');
150             modalRef.componentInstance.action = Boolean(true);
151             modalRef.componentInstance.editType = editType;
152             modalRef.componentInstance.id = this.value.identifier;
153         }
154         modalRef.result.then((result: MODALCLOSERESPONSEDATA): void => {
155             if (result) {
156                 this.sharedService.callData();
157             }
158         }).catch((): void => {
159             // Catch Navigation Error
160         });
161     }
162 }