blob: 89affae18c404788c029ffae626cd61587f24caa [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 Roles Deatils component.
20 */
21import { Component, Injector, OnInit } from '@angular/core';
22import { Router } from '@angular/router';
23import { TranslateService } from '@ngx-translate/core';
24import { ERRORDATA } from 'CommonModel';
25import { DataService } from 'DataService';
26import { environment } from 'environment';
27import { LocalDataSource } from 'ng2-smart-table';
28import { RestService } from 'RestService';
29import { RolesActionComponent } from 'RolesAction';
30import { RoleData, RoleDetails } from 'RolesModel';
31import { Subscription } from 'rxjs';
32import { SharedService } from 'SharedService';
Barath Kumar R09cd4ec2020-07-07 16:12:32 +053033import { isNullOrUndefined } from 'util';
kumaran.m3b4814a2020-05-01 19:48:54 +053034
35/**
36 * Creating component
37 * @Component takes RolesComponent.html as template url
38 */
39@Component({
40 selector: 'app-roles-details',
41 templateUrl: './RolesDetailsComponent.html',
42 styleUrls: ['./RolesDetailsComponent.scss']
43})
44export class RolesDetailsComponent implements OnInit {
45 /** To inject services @public */
46 public injector: Injector;
47
48 /** Formation of appropriate Data for LocalDatasource @public */
49 public dataSource: LocalDataSource = new LocalDataSource();
50
51 /** handle translate @public */
52 public translateService: TranslateService;
53
54 /** Columns list of the smart table @public */
55 public columnLists: object = {};
56
57 /** Settings for smarttable to populate the table with columns @public */
58 public settings: object = {};
59
60 /** Check the loading results @public */
61 public isLoadingResults: boolean = true;
62
63 /** Give the message for the loading @public */
64 public message: string = 'PLEASEWAIT';
65
66 /** Instance of the rest service @private */
67 private restService: RestService;
68
69 /** dataService to pass the data from one component to another @private */
70 private dataService: DataService;
71
72 /** Contains role details data @private */
73 private roleData: RoleData[] = [];
74
75 /** Contains all methods related to shared @private */
76 private sharedService: SharedService;
77
78 /** Instance of subscriptions @private */
79 private generateDataSub: Subscription;
80
81 /** Holds the instance of roter service @private */
82 private router: Router;
83
84 constructor(injector: Injector) {
85 this.injector = injector;
86 this.restService = this.injector.get(RestService);
87 this.dataService = this.injector.get(DataService);
88 this.sharedService = this.injector.get(SharedService);
89 this.translateService = this.injector.get(TranslateService);
90 this.router = this.injector.get(Router);
91 }
92 /** Lifecyle Hooks the trigger before component is instantiate @public */
93 public ngOnInit(): void {
94 this.generateColumns();
95 this.generateSettings();
96 this.generateData();
97 this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
98 }
99 /** smart table Header Colums @public */
100 public generateColumns(): void {
101 this.columnLists = {
102 name: { title: this.translateService.instant('NAME'), width: '30%', sortDirection: 'asc' },
103 identifier: { title: this.translateService.instant('IDENTIFIER'), width: '35%' },
104 modified: { title: this.translateService.instant('MODIFIED'), width: '15%' },
105 created: { title: this.translateService.instant('CREATED'), width: '15%' },
106 Actions: {
107 name: 'Actions', width: '5%', filter: false, sort: false, type: 'custom',
108 title: this.translateService.instant('ACTIONS'),
109 valuePrepareFunction: (cell: RoleData, row: RoleData): RoleData => row,
110 renderComponent: RolesActionComponent
111 }
112 };
113 }
114
115 /** smart table Data Settings @public */
116 public generateSettings(): void {
117 this.settings = {
118 edit: {
119 editButtonContent: '<i class="fa fa-edit" title="Edit"></i>',
120 confirmSave: true
121 },
122 delete: {
123 deleteButtonContent: '<i class="far fa-trash-alt" title="delete"></i>',
124 confirmDelete: true
125 },
126 columns: this.columnLists,
127 actions: {
128 add: false,
129 edit: false,
130 delete: false,
131 topology: false,
132 position: 'right'
133 },
134 attr: this.sharedService.tableClassConfig(),
135 pager: this.sharedService.paginationPagerConfig(),
136 noDataMessage: this.translateService.instant('NODATAMSG')
137 };
138 }
139
140 /** smart table listing manipulation @public */
141 public onChange(perPageValue: number): void {
142 this.dataSource.setPaging(1, perPageValue, true);
143 }
144
145 /** convert UserRowSelect Function @private */
146 public onUserRowSelect(event: MessageEvent): void {
147 Object.assign(event.data, { page: 'roles' });
148 this.dataService.changeMessage(event.data);
149 }
150
151 /** Fetching the role data from API and Load it in the smarttable @public */
152 public generateData(): void {
153 this.isLoadingResults = true;
154 this.roleData = [];
155 this.restService.getResource(environment.ROLES_URL).subscribe((roleList: RoleDetails[]) => {
156 roleList.forEach((role: RoleDetails) => {
157 const roleDataObj: RoleData = this.generateRoleData(role);
158 this.roleData.push(roleDataObj);
159 });
160 this.dataSource.load(this.roleData).then((data: boolean) => {
161 this.isLoadingResults = false;
162 }).catch();
163 }, (error: ERRORDATA) => {
164 this.restService.handleError(error, 'get');
165 this.isLoadingResults = false;
166 });
167 }
168
169 /** Generate role data object and return for the datasource @public */
170 public generateRoleData(roleData: RoleDetails): RoleData {
171 return {
172 name: roleData.name,
173 identifier: roleData._id,
Barath Kumar R09cd4ec2020-07-07 16:12:32 +0530174 modified: this.sharedService.convertEpochTime(!isNullOrUndefined(roleData._admin) ? Number(roleData._admin.modified) : null),
175 created: this.sharedService.convertEpochTime(!isNullOrUndefined(roleData._admin) ? Number(roleData._admin.created) : null),
kumaran.m3b4814a2020-05-01 19:48:54 +0530176 permissions: roleData.permissions
177 };
178 }
179
180 /** Create role click handler @public */
181 public createRole(): void {
182 this.router.navigate(['/roles/create']).catch(() => {
183 // Catch Navigation Error
184 });
185 }
186
187 /** Lifecyle hook which get trigger on component destruction @private */
188 public ngOnDestroy(): void {
189 this.generateDataSub.unsubscribe();
190 }
191
192}