| 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 Add Modal |
| 20 | */ |
| 21 | import { Component, Injector, Input, OnInit } from '@angular/core'; |
| 22 | import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
| 23 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| 24 | import { TranslateService } from '@ngx-translate/core'; |
| 25 | import { NotifierService } from 'angular-notifier'; |
| 26 | import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel'; |
| 27 | import { DataService } from 'DataService'; |
| 28 | import { environment } from 'environment'; |
| 29 | import { ProjectData, ProjectDetails } from 'ProjectModel'; |
| 30 | import { ProjectService } from 'ProjectService'; |
| 31 | import { RestService } from 'RestService'; |
| 32 | import { SharedService } from 'SharedService'; |
| 33 | import { isNullOrUndefined } from 'util'; |
| 34 | |
| 35 | /** |
| 36 | * Creating component |
| 37 | * @Component takes ProjectCreateUpdateComponent.html as template url |
| 38 | */ |
| 39 | @Component({ |
| 40 | selector: 'app-project-create-update', |
| 41 | templateUrl: './ProjectCreateUpdateComponent.html', |
| 42 | styleUrls: ['./ProjectCreateUpdateComponent.scss'] |
| 43 | }) |
| 44 | /** Exporting a class @exports ProjectCreateUpdateComponent */ |
| 45 | export class ProjectCreateUpdateComponent implements OnInit { |
| 46 | /** To inject services @public */ |
| 47 | public injector: Injector; |
| 48 | |
| 49 | /** Instance of the rest service @public */ |
| 50 | public restService: RestService; |
| 51 | |
| 52 | /** Instance for active modal service @public */ |
| 53 | public activeModal: NgbActiveModal; |
| 54 | |
| 55 | /** Contains the recently created project details @public */ |
| 56 | public recentProject: ProjectDetails; |
| 57 | |
| 58 | /** Contains project name @public */ |
| 59 | public projectName: string; |
| 60 | |
| 61 | /** Contains project create or edit @public */ |
| 62 | public getProjectType: string; |
| 63 | |
| 64 | /** To inject input type services @public */ |
| 65 | @Input() public projectType: string; |
| 66 | |
| 67 | /** FormGroup user Edit Account added to the form @ html @public */ |
| 68 | public projectForm: FormGroup; |
| 69 | |
| 70 | /** Form submission Add */ |
| 71 | public submitted: boolean = false; |
| 72 | |
| 73 | /** Check the loading results for loader status @public */ |
| 74 | public isLoadingResults: boolean = false; |
| 75 | |
| 76 | /** Give the message for the loading @public */ |
| 77 | public message: string = 'PLEASEWAIT'; |
| 78 | |
| 79 | /** Holds list of domains @public */ |
| 80 | public domains: {}[] = []; |
| 81 | |
| 82 | /** FormBuilder instance added to the formBuilder @private */ |
| 83 | private formBuilder: FormBuilder; |
| 84 | |
| 85 | /** DataService to pass the data from one component to another @private */ |
| 86 | private dataService: DataService; |
| 87 | |
| 88 | /** Contains project name ref @private */ |
| 89 | private projectRef: string; |
| 90 | |
| 91 | /** Notifier service to popup notification @private */ |
| 92 | private notifierService: NotifierService; |
| 93 | |
| 94 | /** Contains tranlsate instance @private */ |
| 95 | private translateService: TranslateService; |
| 96 | |
| 97 | /** Contains all methods related to shared @private */ |
| 98 | private sharedService: SharedService; |
| 99 | |
| 100 | /** ModalData instance of modal @private */ |
| 101 | private modalData: MODALCLOSERESPONSEDATA; |
| 102 | |
| 103 | /** Holds all project details @private */ |
| 104 | private projectService: ProjectService; |
| 105 | |
| 106 | constructor(injector: Injector) { |
| 107 | this.injector = injector; |
| 108 | this.formBuilder = this.injector.get(FormBuilder); |
| 109 | this.restService = this.injector.get(RestService); |
| 110 | this.activeModal = this.injector.get(NgbActiveModal); |
| 111 | this.dataService = this.injector.get(DataService); |
| 112 | this.notifierService = this.injector.get(NotifierService); |
| 113 | this.translateService = this.injector.get(TranslateService); |
| 114 | this.sharedService = this.injector.get(SharedService); |
| 115 | this.projectService = this.injector.get(ProjectService); |
| 116 | /** Initializing Form Action */ |
| 117 | this.projectForm = this.formBuilder.group({ |
| 118 | project_name: ['', Validators.required], |
| 119 | domain_name: [null] |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | /** convenience getter for easy access to form fields */ |
| 124 | get f(): FormGroup['controls'] { return this.projectForm.controls; } |
| 125 | |
| 126 | /** Lifecyle Hooks the trigger before component is instantiate @public */ |
| 127 | public ngOnInit(): void { |
| 128 | this.getProjectType = this.projectType; |
| 129 | if (this.getProjectType === 'Edit') { |
| 130 | this.dataService.currentMessage.subscribe((data: ProjectData) => { |
| 131 | if (data.projectName !== undefined || data.projectName !== '' || data.projectName !== null) { |
| 132 | this.projectName = data.projectName; |
| 133 | this.projectRef = data.id; |
| 134 | } |
| 135 | }); |
| 136 | } else { |
| 137 | this.getProjects(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** Get the last project name @public */ |
| 142 | public getProjects(): void { |
| 143 | this.isLoadingResults = true; |
| 144 | this.restService.getResource(environment.PROJECTS_URL).subscribe((projects: ProjectDetails[]) => { |
| 145 | this.recentProject = projects.slice(-1).pop(); |
| 146 | this.getDomainName(); |
| 147 | }, (error: ERRORDATA) => { |
| 148 | this.restService.handleError(error, 'get'); |
| 149 | this.isLoadingResults = false; |
| 150 | }); |
| 151 | } |
| 152 | |
| 153 | /** On modal submit users acction will called @public */ |
| 154 | public projectAction(userType: string): void { |
| 155 | this.submitted = true; |
| 156 | this.modalData = { |
| 157 | message: 'Done' |
| 158 | }; |
| 159 | this.sharedService.cleanForm(this.projectForm); |
| 160 | if (!this.projectForm.invalid) { |
| 161 | if (userType === 'Add') { |
| 162 | this.createProject(); |
| 163 | } else if (userType === 'Edit') { |
| 164 | this.editProject(); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** Create project @public */ |
| 170 | public createProject(): void { |
| 171 | this.isLoadingResults = true; |
| 172 | const apiURLHeader: APIURLHEADER = { |
| 173 | url: environment.PROJECTS_URL |
| 174 | }; |
| 175 | const projectPayload: {} = { |
| 176 | name: this.projectForm.value.project_name, |
| 177 | domain_name: !isNullOrUndefined(this.projectForm.value.domain_name) ? this.projectForm.value.domain_name : undefined |
| 178 | }; |
| 179 | this.restService.postResource(apiURLHeader, projectPayload).subscribe(() => { |
| 180 | this.activeModal.close(this.modalData); |
| 181 | this.isLoadingResults = false; |
| 182 | this.notifierService.notify('success', this.translateService.instant('PAGE.PROJECT.CREATEDSUCCESSFULLY')); |
| 183 | }, (error: ERRORDATA) => { |
| 184 | this.restService.handleError(error, 'post'); |
| 185 | this.isLoadingResults = false; |
| 186 | }); |
| 187 | } |
| 188 | /** Edit project @public */ |
| 189 | public editProject(): void { |
| 190 | this.isLoadingResults = true; |
| 191 | const apiURLHeader: APIURLHEADER = { |
| 192 | url: environment.PROJECTS_URL + '/' + this.projectRef |
| 193 | }; |
| 194 | this.restService.patchResource(apiURLHeader, { name: this.projectForm.value.project_name }).subscribe(() => { |
| 195 | this.activeModal.close(this.modalData); |
| 196 | this.isLoadingResults = false; |
| 197 | this.projectService.setHeaderProjects(); |
| 198 | this.notifierService.notify('success', this.translateService.instant('PAGE.PROJECT.UPDATEDSUCCESSFULLY')); |
| 199 | }, (error: ERRORDATA) => { |
| 200 | this.restService.handleError(error, 'patch'); |
| 201 | this.isLoadingResults = false; |
| 202 | }); |
| 203 | } |
| 204 | /** Get domain name @private */ |
| 205 | private getDomainName(): void { |
| 206 | this.restService.getResource(environment.DOMAIN_URL).subscribe((domains: { project_domain_name: string, user_domain_name: string }) => { |
| 207 | let domainNames: string[] = []; |
| 208 | if (!isNullOrUndefined(domains.project_domain_name)) { |
| 209 | domainNames = domainNames.concat(domains.project_domain_name.split(',')); |
| 210 | } |
| 211 | if (!isNullOrUndefined(domains.user_domain_name)) { |
| 212 | domainNames = domainNames.concat(domains.user_domain_name.split(',')); |
| 213 | } |
| 214 | domainNames = Array.from(new Set(domainNames)); |
| 215 | this.checkDomainNames(domainNames); |
| 216 | this.isLoadingResults = false; |
| 217 | }, (error: ERRORDATA) => { |
| 218 | this.restService.handleError(error, 'get'); |
| 219 | this.isLoadingResults = false; |
| 220 | }); |
| 221 | } |
| 222 | |
| 223 | /** Check the domain names and create modal for domain select @private */ |
| 224 | private checkDomainNames(domainNames: string[]): void { |
| 225 | if (domainNames.length > 0) { |
| 226 | domainNames.forEach((domainName: string) => { |
| 227 | if (!domainName.endsWith(':ro')) { |
| 228 | this.domains.push({ id: domainName, text: domainName }); |
| 229 | } |
| 230 | }); |
| 231 | } |
| 232 | } |
| 233 | } |