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