Angular upgrade
[osm/NG-UI.git] / src / app / projects / ProjectsComponent.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 Project details Component.
20  */
21 import { Component, Injector, OnDestroy, OnInit } from '@angular/core';
22 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
23 import { TranslateService } from '@ngx-translate/core';
24 import { ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
25 import { DataService } from 'DataService';
26 import { environment } from 'environment';
27 import { LocalDataSource } from 'ng2-smart-table';
28 import { ProjectCreateUpdateComponent } from 'ProjectCreateUpdate';
29 import { ProjectLinkComponent } from 'ProjectLinkComponent';
30 import { ProjectData, ProjectDetails } from 'ProjectModel';
31 import { ProjectsActionComponent } from 'ProjectsAction';
32 import { RestService } from 'RestService';
33 import { Subscription } from 'rxjs';
34 import { SharedService, isNullOrUndefined } from 'SharedService';
35
36 /**
37  * Creating component
38  * @Component takes ProjectsComponent.html as template url
39  */
40 @Component({
41     selector: 'app-projects',
42     templateUrl: './ProjectsComponent.html',
43     styleUrls: ['./ProjectsComponent.scss']
44 })
45 /** Exporting a class @exports ProjectsComponent */
46 export class ProjectsComponent implements OnInit, OnDestroy {
47     /** To inject services @public */
48     public injector: Injector;
49
50     /** handle translate @public */
51     public translateService: TranslateService;
52
53     /** Data of smarttable populate through LocalDataSource @public */
54     public dataSource: LocalDataSource = new LocalDataSource();
55
56     /** Columns list of the smart table @public */
57     public columnLists: object = {};
58
59     /** Settings for smarttable to populate the table with columns @public */
60     public settings: object = {};
61
62     /** Check the loading results @public */
63     public isLoadingResults: boolean = true;
64
65     /** Give the message for the loading @public */
66     public message: string = 'PLEASEWAIT';
67
68     /** Instance of the rest service @private */
69     private restService: RestService;
70
71     /** dataService to pass the data from one component to another @private */
72     private dataService: DataService;
73
74     /** Instance of the modal service @private */
75     private modalService: NgbModal;
76
77     /** Formation of appropriate Data for LocalDatasource @private */
78     private projectData: ProjectData[] = [];
79
80     /** Contains all methods related to shared @private */
81     private sharedService: SharedService;
82
83     /** Instance of subscriptions @private */
84     private generateDataSub: Subscription;
85
86     constructor(injector: Injector) {
87         this.injector = injector;
88         this.restService = this.injector.get(RestService);
89         this.dataService = this.injector.get(DataService);
90         this.sharedService = this.injector.get(SharedService);
91         this.modalService = this.injector.get(NgbModal);
92         this.translateService = this.injector.get(TranslateService);
93     }
94
95     /** Lifecyle Hooks the trigger before component is instantiate @public */
96     public ngOnInit(): void {
97         this.generateColumns();
98         this.generateSettings();
99         this.generateData();
100         this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
101     }
102
103     /** smart table Header Colums @public */
104     public generateColumns(): void {
105         this.columnLists = {
106             projectName: {
107                 title: this.translateService.instant('NAME'), width: '55%', sortDirection: 'asc', type: 'custom',
108                 valuePrepareFunction: (cell: ProjectData, row: ProjectData): ProjectData => row,
109                 renderComponent: ProjectLinkComponent
110             },
111             modificationDate: { title: this.translateService.instant('MODIFIED'), width: '20%' },
112             creationDate: { title: this.translateService.instant('CREATED'), width: '20%' },
113             Actions: {
114                 name: 'Action', width: '5%', filter: false, sort: false, type: 'custom',
115                 title: this.translateService.instant('ACTIONS'),
116                 valuePrepareFunction: (cell: ProjectData, row: ProjectData): ProjectData => row,
117                 renderComponent: ProjectsActionComponent
118             }
119         };
120     }
121
122     /** smart table Data Settings @public */
123     public generateSettings(): void {
124         this.settings = {
125             edit: {
126                 editButtonContent: '<i class="fa fa-edit" title="Edit"></i>',
127                 confirmSave: true
128             },
129             delete: {
130                 deleteButtonContent: '<i class="far fa-trash-alt" title="Delete"></i>',
131                 confirmDelete: true
132             },
133             columns: this.columnLists,
134             actions: {
135                 add: false,
136                 edit: false,
137                 delete: false,
138                 position: 'right'
139             },
140             attr: this.sharedService.tableClassConfig(),
141             pager: this.sharedService.paginationPagerConfig(),
142             noDataMessage: this.translateService.instant('NODATAMSG')
143         };
144     }
145
146     /** Modal service to initiate the project add @private */
147     public projectAdd(): void {
148         // eslint-disable-next-line security/detect-non-literal-fs-filename
149         const modalRef: NgbModalRef = this.modalService.open(ProjectCreateUpdateComponent, { size: 'lg', backdrop: 'static' });
150         modalRef.componentInstance.projectType = 'Add';
151         modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
152             if (result) {
153                 this.generateData();
154             }
155         }).catch((): void => {
156             // Catch Navigation Error
157         });
158     }
159
160     /** smart table listing manipulation @private */
161     public onChange(perPageValue: number): void {
162         this.dataSource.setPaging(1, perPageValue, true);
163     }
164
165     /** convert UserRowSelect Function @private */
166     public onUserRowSelect(event: MessageEvent): void {
167         Object.assign(event.data, { page: 'projects' });
168         this.dataService.changeMessage(event.data);
169     }
170
171     /** Generate Projects object from loop and return for the datasource @public */
172     public generateProjectData(projectData: ProjectDetails): ProjectData {
173         return {
174             projectName: projectData.name,
175             modificationDate: this.sharedService.convertEpochTime(!isNullOrUndefined(projectData._admin)
176                 ? projectData._admin.modified : null),
177             creationDate: this.sharedService.convertEpochTime(!isNullOrUndefined(projectData._admin) ? projectData._admin.created : null),
178             id: projectData._id,
179             project: projectData._id,
180             quotas: !isNullOrUndefined(projectData.quotas) && Object.keys(projectData.quotas).length !== 0 ? projectData.quotas : null
181         };
182     }
183
184     /**
185      * Lifecyle hook which get trigger on component destruction
186      */
187     public ngOnDestroy(): void {
188         this.generateDataSub.unsubscribe();
189     }
190
191     /** Fetching the data from server to Load in the smarttable @protected */
192     protected generateData(): void {
193         this.isLoadingResults = true;
194         this.restService.getResource(environment.PROJECTS_URL).subscribe((projectsData: ProjectDetails[]) => {
195             this.projectData = [];
196             projectsData.forEach((projectData: ProjectDetails) => {
197                 const projectDataObj: ProjectData = this.generateProjectData(projectData);
198                 this.projectData.push(projectDataObj);
199             });
200             this.dataSource.load(this.projectData).then((data: boolean) => {
201                 this.isLoadingResults = false;
202             }).catch((): void => {
203                 // Catch Navigation Error
204             });
205         }, (error: ERRORDATA) => {
206             this.restService.handleError(error, 'get');
207             this.isLoadingResults = false;
208         });
209     }
210 }