blob: a4bedd5f28e41728d60738f58d76053f0ffbd2b8 [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +05301/*
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 details Component.
20 */
21import { Component, Injector, OnDestroy, OnInit } from '@angular/core';
22import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
23import { TranslateService } from '@ngx-translate/core';
24import { AddEditUserComponent } from 'AddEditUserComponent';
25import { ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
26import { DataService } from 'DataService';
27import { environment } from 'environment';
28import { LocalDataSource } from 'ng2-smart-table';
29import { ProjectService } from 'ProjectService';
30import { RestService } from 'RestService';
31import { Subscription } from 'rxjs';
32import { SharedService } from 'SharedService';
33import { UserData, UserDetail } from 'UserModel';
34import { UsersActionComponent } from 'UsersActionComponent';
35
36/**
37 * Creating component
38 * @Component takes UserDetailsComponent.html as template url
39 */
40@Component({
41 templateUrl: './UserDetailsComponent.html',
42 styleUrls: ['./UserDetailsComponent.scss']
43})
44/** Exporting a class @exports UserDetailsComponent */
45export class UserDetailsComponent implements OnInit, OnDestroy {
46 /** Data of smarttable populate through LocalDataSource @public */
47 public dataSource: LocalDataSource = new LocalDataSource();
48
49 /** handle translate @public */
50 public translateService: TranslateService;
51
52 /** To inject services @public */
53 public injector: Injector;
54
55 /** Settings for smarttable to populate the table with columns @public */
56 public settings: object = {};
57
58 /** Check the loading results @public */
59 public isLoadingResults: boolean = true;
60
61 /** Give the message for the loading @public */
62 public message: string = 'PLEASEWAIT';
63
64 /** Class for empty and present data @public */
65 public checkDataClass: string;
66
67 /** Instance of the rest service @private */
68 private restService: RestService;
69
70 /** dataService to pass the data from one component to another @private */
71 private dataService: DataService;
72
73 /** Instance of the modal service @private */
74 private modalService: NgbModal;
75
76 /** Formation of appropriate Data for LocalDatasource @private */
77 private userData: {}[] = [];
78
79 /** Contains all methods related to shared @private */
80 private sharedService: SharedService;
81
82 /** Holds all project details */
83 private projectService: ProjectService;
84
85 /** holds the project information @private */
86 private projectList: {}[] = [];
87
88 /** Columns list of the smart table @public */
89 private columnLists: object = {};
90
91 /** Instance of subscriptions @private */
92 private generateDataSub: Subscription;
93
94 constructor(injector: Injector) {
95 this.injector = injector;
96 this.restService = this.injector.get(RestService);
97 this.dataService = this.injector.get(DataService);
98 this.sharedService = this.injector.get(SharedService);
99 this.modalService = this.injector.get(NgbModal);
100 this.projectService = this.injector.get(ProjectService);
101 this.translateService = this.injector.get(TranslateService);
102 }
103
104 /**
105 * Lifecyle Hooks the trigger before component is instantiate
106 */
107 public ngOnInit(): void {
108 this.projectService.getAllProjects().subscribe((projects: {}[]) => {
109 this.projectList = projects;
110 });
111 this.generateColumns();
112 this.generateSettings();
113 this.generateData();
114 this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
115 }
116
117 /** smart table Header Colums @public */
118 public generateColumns(): void {
119 this.columnLists = {
120 username: { title: this.translateService.instant('NAME'), width: '20%', sortDirection: 'asc' },
121 projects: { title: this.translateService.instant('PAGE.DASHBOARD.PROJECTS'), width: '25%' },
122 identifier: { title: this.translateService.instant('IDENTIFIER'), width: '20%' },
123 modified: { title: this.translateService.instant('MODIFIED'), width: '15%' },
124 created: { title: this.translateService.instant('CREATED'), width: '15%' },
125 Actions: {
126 name: 'Action', width: '5%', filter: false, sort: false, title: this.translateService.instant('ACTIONS'), type: 'custom',
127 valuePrepareFunction: (cell: UserData, row: UserData): UserData => row,
128 renderComponent: UsersActionComponent
129 }
130 };
131 }
132
133 /** smart table Data Settings @public */
134 public generateSettings(): void {
135 this.settings = {
136 edit: { editButtonContent: '<i class="fa fa-edit" title="Edit"></i>', confirmSave: true },
137 delete: { deleteButtonContent: '<i class="far fa-trash-alt" title="delete"></i>', confirmDelete: true },
138 columns: this.columnLists,
139 actions: { add: false, edit: false, delete: false, position: 'right' },
140 attr: this.sharedService.tableClassConfig(),
141 pager: this.sharedService.paginationPagerConfig(),
142 noDataMessage: this.translateService.instant('NODATAMSG')
143 };
144 }
145
146 /** on Navigate to Composer Page @public */
147 public composeUser(): void {
148 const modalRef: NgbModalRef = this.modalService.open(AddEditUserComponent, { backdrop: 'static' });
149 modalRef.componentInstance.userTitle = this.translateService.instant('PAGE.USERS.NEWUSER');
150 modalRef.componentInstance.userType = 'add';
151 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
152 if (result) {
153 this.sharedService.callData();
154 }
155 }).catch();
156 }
157
158 /** smart table listing manipulation @private */
159 public onChange(perPageValue: number): void {
160 this.dataSource.setPaging(1, perPageValue, true);
161 }
162
163 /** OnUserRowSelect function @private */
164 public onUserRowSelect(event: MessageEvent): void {
165 Object.assign(event.data, { page: 'users' });
166 this.dataService.changeMessage(event.data);
167 }
168
169 /** Set up user details @public */
170 public setUserDetails(userData: UserDetail): void {
171 const userDataObj: UserData = {
172 username: userData.username,
173 modified: this.sharedService.convertEpochTime(userData._admin.modified),
174 created: this.sharedService.convertEpochTime(userData._admin.created),
175 projects: userData.projectListName,
176 identifier: userData._id
177 };
178 this.userData.push(userDataObj);
179 }
180
181 /**
182 * Lifecyle hook which get trigger on component destruction
183 */
184 public ngOnDestroy(): void {
185 this.generateDataSub.unsubscribe();
186 }
187
188 /** Fetching the data from server to Load in the smarttable @protected */
189 protected generateData(): void {
190 this.isLoadingResults = true;
191 this.restService.getResource(environment.USERS_URL).subscribe((usersData: UserDetail[]) => {
192 this.userData = [];
193 usersData.forEach((userData: UserDetail) => {
194 if (userData.projects.length > 0) {
195 userData.projectListName = userData.projects.join(', ');
196 } else {
197 userData.projectListName = '';
198 }
199 this.setUserDetails(userData);
200 });
201 if (this.userData.length > 0) {
202 this.checkDataClass = 'dataTables_present';
203 } else {
204 this.checkDataClass = 'dataTables_empty';
205 }
206 this.dataSource.load(this.userData).then((data: {}) => {
207 this.isLoadingResults = false;
208 }).catch();
209 }, (error: ERRORDATA) => {
210 this.restService.handleError(error, 'get');
211 this.isLoadingResults = false;
212 });
213 }
214}