blob: 927e54e8f2d4fce7a0399dc7aa0f594568dc7373 [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 WIM Account 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 { CONFIGCONSTANT, ERRORDATA, MODALCLOSERESPONSEDATA, WIM_TYPES } from 'CommonModel';
25import { DataService } from 'DataService';
26import { environment } from 'environment';
27import { NewWIMAccountComponent } from 'NewWIMAccount';
28import { LocalDataSource } from 'ng2-smart-table';
29import { RestService } from 'RestService';
30import { Subscription } from 'rxjs';
31import { SharedService } from 'SharedService';
32import { WIMAccountData, WIMAccountModel } from 'WIMAccountModel';
33import { WIMAccountsActionComponent } from 'WIMAccountsAction';
34/**
35 * Creating component
36 * @Component takes WIMAccountDetailsComponent.html as template url
37 */
38@Component({
39 templateUrl: './WIMAccountDetailsComponent.html',
40 styleUrls: ['./WIMAccountDetailsComponent.scss']
41})
42/** Exporting a class @exports WIMAccountDetailsComponent */
43export class WIMAccountDetailsComponent implements OnInit, OnDestroy {
44 /** To inject services @public */
45 public injector: Injector;
46
47 /** handle translate @public */
48 public translateService: TranslateService;
49
50 /** Data of smarttable populate through LocalDataSource @public */
51 public dataSource: LocalDataSource = new LocalDataSource();
52
53 /** Columns list of the smart table @public */
54 public columnLists: object = {};
55
56 /** Settings for smarttable to populate the table with columns @public */
57 public settings: object = {};
58
59 /** Check the loading results @public */
60 public isLoadingResults: boolean = true;
61
62 /** Give the message for the loading @public */
63 public message: string = 'PLEASEWAIT';
64
65 /** Class for empty and present data @public */
66 public checkDataClass: string;
67
68 /** operational State init data @public */
69 public operationalStateFirstStep: string = CONFIGCONSTANT.wimOperationalStateFirstStep;
70
71 /** operational State running data @public */
72 public operationalStateSecondStep: string = CONFIGCONSTANT.wimOperationalStateStateSecondStep;
73
74 /** operational State failed data @public */
75 public operationalStateThirdStep: string = CONFIGCONSTANT.wimOperationalStateThirdStep;
76
77 /** Instance of the rest service @private */
78 private restService: RestService;
79
80 /** dataService to pass the data from one component to another @private */
81 private dataService: DataService;
82
83 /** Formation of appropriate Data for LocalDatasource @private */
84 private wimData: {}[] = [];
85
86 /** Contains all methods related to shared @private */
87 private sharedService: SharedService;
88
89 /** Instance of the modal service @private */
90 private modalService: NgbModal;
91
92 /** Instance of subscriptions @private */
93 private generateDataSub: Subscription;
94
95 constructor(injector: Injector) {
96 this.injector = injector;
97 this.restService = this.injector.get(RestService);
98 this.dataService = this.injector.get(DataService);
99 this.sharedService = this.injector.get(SharedService);
100 this.translateService = this.injector.get(TranslateService);
101 this.modalService = this.injector.get(NgbModal);
102 }
103 /** Lifecyle Hooks the trigger before component is instantiate @public */
104 public ngOnInit(): void {
105 this.generateColumns();
106 this.generateSettings();
107 this.generateData();
108 this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
109 }
110
111 /** smart table Header Colums @public */
112 public generateColumns(): void {
113 this.columnLists = {
114 name: { title: this.translateService.instant('NAME'), width: '20%', sortDirection: 'asc' },
115 identifier: { title: this.translateService.instant('IDENTIFIER'), width: '20%' },
116 type: {
117 title: this.translateService.instant('TYPE'), width: '15%',
118 filter: {
119 type: 'list',
120 config: {
121 selectText: 'Select',
122 list: WIM_TYPES
123 }
124 }
125 },
126 operationalState: {
127 title: this.translateService.instant('OPERATIONALSTATUS'), width: '15%', type: 'html',
128 filter: {
129 type: 'list',
130 config: {
131 selectText: 'Select',
132 list: [
133 { value: this.operationalStateFirstStep, title: this.operationalStateFirstStep },
134 { value: this.operationalStateSecondStep, title: this.operationalStateSecondStep },
135 { value: this.operationalStateThirdStep, title: this.operationalStateThirdStep }
136 ]
137 }
138 },
139 valuePrepareFunction: (cell: WIMAccountData, row: WIMAccountData): string => {
140 if (row.operationalState === this.operationalStateFirstStep) {
141 return `<span class="icon-label" title="${row.operationalState}">
142 <i class="fas fa-clock text-warning"></i>
143 </span>`;
144 } else if (row.operationalState === this.operationalStateSecondStep) {
145 return `<span class="icon-label" title="${row.operationalState}">
146 <i class="fas fa-check-circle text-success"></i>
147 </span>`;
148 } else if (row.operationalState === this.operationalStateThirdStep) {
149 return `<span class="icon-label" title="${row.operationalState}">
150 <i class="fas fa-times-circle text-danger"></i>
151 </span>`;
152 } else {
153 return `<span>${row.operationalState}</span>`;
154 }
155 }
156 },
157 description: { title: this.translateService.instant('DESCRIPTION'), width: '25%' },
158 Actions: {
159 name: 'Action', width: '5%', filter: false, sort: false, title: this.translateService.instant('ACTIONS'), type: 'custom',
160 valuePrepareFunction: (cell: WIMAccountData, row: WIMAccountData): WIMAccountData => row,
161 renderComponent: WIMAccountsActionComponent
162 }
163 };
164 }
165
166 /** smart table Data Settings @public */
167 public generateSettings(): void {
168 this.settings = {
169 edit: {
170 editButtonContent: '<i class="fa fa-edit" title="Edit"></i>', confirmSave: true
171 },
172 delete: {
173 deleteButtonContent: '<i class="far fa-trash-alt" title="delete"></i>', confirmDelete: true
174 },
175 columns: this.columnLists,
176 actions: {
177 add: false, edit: false, delete: false, position: 'right'
178 },
179 attr: this.sharedService.tableClassConfig(),
180 pager: this.sharedService.paginationPagerConfig(),
181 noDataMessage: this.translateService.instant('NODATAMSG')
182 };
183 }
184
185 /** smart table listing manipulation @public */
186 public onChange(perPageValue: number): void {
187 this.dataSource.setPaging(1, perPageValue, true);
188 }
189
190 /** smart table listing manipulation @public */
191 public onUserRowSelect(event: MessageEvent): void {
192 Object.assign(event.data, { page: 'wim-account' });
193 this.dataService.changeMessage(event.data);
194 }
195
196 /** Compose new WIM Accounts @public */
197 public composeWIM(): void {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530198 // eslint-disable-next-line security/detect-non-literal-fs-filename
kumaran.m3b4814a2020-05-01 19:48:54 +0530199 const modalRef: NgbModalRef = this.modalService.open(NewWIMAccountComponent, { backdrop: 'static' });
200 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
201 if (result) {
202 this.sharedService.callData();
203 }
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530204 }).catch((): void => {
205 // Catch Navigation Error
206 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530207 }
208
209 /** Generate generateWIMData object from loop and return for the datasource @public */
210 public generateWIMData(wimAccountData: WIMAccountModel): WIMAccountData {
211 return {
212 name: wimAccountData.name,
213 identifier: wimAccountData._id,
214 type: wimAccountData.wim_type,
215 operationalState: wimAccountData._admin.operationalState,
216 description: wimAccountData.description
217 };
218 }
219
220 /**
221 * Lifecyle hook which get trigger on component destruction
222 */
223 public ngOnDestroy(): void {
224 this.generateDataSub.unsubscribe();
225 }
226
227 /** Fetching the data from server to Load in the smarttable @protected */
228 protected generateData(): void {
229 this.isLoadingResults = true;
230 this.wimData = [];
231 this.restService.getResource(environment.WIMACCOUNTS_URL).subscribe((wimAccountsDetails: {}[]) => {
232 wimAccountsDetails.forEach((wimAccountsData: WIMAccountModel) => {
233 const wimDataObj: WIMAccountData = this.generateWIMData(wimAccountsData);
234 this.wimData.push(wimDataObj);
235 });
236 if (this.wimData.length > 0) {
237 this.checkDataClass = 'dataTables_present';
238 } else {
239 this.checkDataClass = 'dataTables_empty';
240 }
241 this.dataSource.load(this.wimData).then((data: {}) => {
242 this.isLoadingResults = false;
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530243 }).catch((): void => {
244 // Catch Navigation Error
245 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530246 }, (error: ERRORDATA) => {
247 this.restService.handleError(error, 'get');
248 this.isLoadingResults = false;
249 });
250 }
251}