2 Copyright 2020 TATA ELXSI
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
19 * @file Roles Deatils component.
21 import { Component, Injector, OnInit } from '@angular/core';
22 import { Router } from '@angular/router';
23 import { TranslateService } from '@ngx-translate/core';
24 import { ERRORDATA } from 'CommonModel';
25 import { DataService } from 'DataService';
26 import { environment } from 'environment';
27 import { LocalDataSource } from 'ng2-smart-table';
28 import { RestService } from 'RestService';
29 import { RolesActionComponent } from 'RolesAction';
30 import { RoleData, RoleDetails } from 'RolesModel';
31 import { Subscription } from 'rxjs';
32 import { SharedService } from 'SharedService';
36 * @Component takes RolesComponent.html as template url
39 selector: 'app-roles-details',
40 templateUrl: './RolesDetailsComponent.html',
41 styleUrls: ['./RolesDetailsComponent.scss']
43 export class RolesDetailsComponent implements OnInit {
44 /** To inject services @public */
45 public injector: Injector;
47 /** Formation of appropriate Data for LocalDatasource @public */
48 public dataSource: LocalDataSource = new LocalDataSource();
50 /** handle translate @public */
51 public translateService: TranslateService;
53 /** Columns list of the smart table @public */
54 public columnLists: object = {};
56 /** Settings for smarttable to populate the table with columns @public */
57 public settings: object = {};
59 /** Check the loading results @public */
60 public isLoadingResults: boolean = true;
62 /** Give the message for the loading @public */
63 public message: string = 'PLEASEWAIT';
65 /** Instance of the rest service @private */
66 private restService: RestService;
68 /** dataService to pass the data from one component to another @private */
69 private dataService: DataService;
71 /** Contains role details data @private */
72 private roleData: RoleData[] = [];
74 /** Contains all methods related to shared @private */
75 private sharedService: SharedService;
77 /** Instance of subscriptions @private */
78 private generateDataSub: Subscription;
80 /** Holds the instance of roter service @private */
81 private router: Router;
83 constructor(injector: Injector) {
84 this.injector = injector;
85 this.restService = this.injector.get(RestService);
86 this.dataService = this.injector.get(DataService);
87 this.sharedService = this.injector.get(SharedService);
88 this.translateService = this.injector.get(TranslateService);
89 this.router = this.injector.get(Router);
91 /** Lifecyle Hooks the trigger before component is instantiate @public */
92 public ngOnInit(): void {
93 this.generateColumns();
94 this.generateSettings();
96 this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
98 /** smart table Header Colums @public */
99 public generateColumns(): void {
101 name: { title: this.translateService.instant('NAME'), width: '30%', sortDirection: 'asc' },
102 identifier: { title: this.translateService.instant('IDENTIFIER'), width: '35%' },
103 modified: { title: this.translateService.instant('MODIFIED'), width: '15%' },
104 created: { title: this.translateService.instant('CREATED'), width: '15%' },
106 name: 'Actions', width: '5%', filter: false, sort: false, type: 'custom',
107 title: this.translateService.instant('ACTIONS'),
108 valuePrepareFunction: (cell: RoleData, row: RoleData): RoleData => row,
109 renderComponent: RolesActionComponent
114 /** smart table Data Settings @public */
115 public generateSettings(): void {
118 editButtonContent: '<i class="fa fa-edit" title="Edit"></i>',
122 deleteButtonContent: '<i class="far fa-trash-alt" title="delete"></i>',
125 columns: this.columnLists,
133 attr: this.sharedService.tableClassConfig(),
134 pager: this.sharedService.paginationPagerConfig(),
135 noDataMessage: this.translateService.instant('NODATAMSG')
139 /** smart table listing manipulation @public */
140 public onChange(perPageValue: number): void {
141 this.dataSource.setPaging(1, perPageValue, true);
144 /** convert UserRowSelect Function @private */
145 public onUserRowSelect(event: MessageEvent): void {
146 Object.assign(event.data, { page: 'roles' });
147 this.dataService.changeMessage(event.data);
150 /** Fetching the role data from API and Load it in the smarttable @public */
151 public generateData(): void {
152 this.isLoadingResults = true;
154 this.restService.getResource(environment.ROLES_URL).subscribe((roleList: RoleDetails[]) => {
155 roleList.forEach((role: RoleDetails) => {
156 const roleDataObj: RoleData = this.generateRoleData(role);
157 this.roleData.push(roleDataObj);
159 this.dataSource.load(this.roleData).then((data: boolean) => {
160 this.isLoadingResults = false;
162 }, (error: ERRORDATA) => {
163 this.restService.handleError(error, 'get');
164 this.isLoadingResults = false;
168 /** Generate role data object and return for the datasource @public */
169 public generateRoleData(roleData: RoleDetails): RoleData {
172 identifier: roleData._id,
173 modified: this.sharedService.convertEpochTime(Number(roleData._admin.modified)),
174 created: this.sharedService.convertEpochTime(Number(roleData._admin.created)),
175 permissions: roleData.permissions
179 /** Create role click handler @public */
180 public createRole(): void {
181 this.router.navigate(['/roles/create']).catch(() => {
182 // Catch Navigation Error
186 /** Lifecyle hook which get trigger on component destruction @private */
187 public ngOnDestroy(): void {
188 this.generateDataSub.unsubscribe();