| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +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: SANDHYA JS (sandhya.j@tataelxsi.co.in) |
| 17 | */ |
| 18 | /** |
| 19 | * @file K8sInfraConfigAddComponent.ts. |
| 20 | */ |
| 21 | import { HttpHeaders } from '@angular/common/http'; |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 22 | import { Component, Injector, Input, OnInit } from '@angular/core'; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 23 | import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
| 24 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| 25 | import { TranslateService } from '@ngx-translate/core'; |
| 26 | import { NotifierService } from 'angular-notifier'; |
| 27 | import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, TYPESECTION, URLPARAMS } from 'CommonModel'; |
| 28 | import { environment } from 'environment'; |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 29 | import { INFRACONFIGPAYLOAD } from 'K8sModel'; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 30 | import { RestService } from 'RestService'; |
| 31 | import { SharedService, isNullOrUndefined } from 'SharedService'; |
| 32 | /** |
| 33 | * Creating Component |
| 34 | * @Component takes K8sInfraConfigAddComponent.html as template url |
| 35 | */ |
| 36 | @Component({ |
| 37 | selector: 'app-infra-config', |
| 38 | templateUrl: './K8sInfraConfigAddComponent.html', |
| 39 | styleUrls: ['./K8sInfraConfigAddComponent.scss'] |
| 40 | }) |
| 41 | /** Exporting a class @exports K8sInfraConfigAddComponent */ |
| 42 | export class K8sInfraConfigAddComponent implements OnInit { |
| 43 | /** To inject services @public */ |
| 44 | public injector: Injector; |
| 45 | |
| 46 | /** FormGroup instance added to the form @ html @public */ |
| 47 | public profileForm: FormGroup; |
| 48 | |
| 49 | /** Instance for active modal service @public */ |
| 50 | public activeModal: NgbActiveModal; |
| 51 | |
| 52 | /** Form submission Add */ |
| 53 | public submitted: boolean = false; |
| 54 | |
| 55 | /** Check the loading results @public */ |
| 56 | public isLoadingResults: boolean = false; |
| 57 | |
| 58 | /** Give the message for the loading @public */ |
| 59 | public message: string = 'PLEASEWAIT'; |
| 60 | |
| 61 | /** profile url @public */ |
| 62 | public profileUrl: string; |
| 63 | |
| 64 | /** Input contains Modal dialog component Instance @public */ |
| 65 | @Input() public profileType: string; |
| 66 | |
| 67 | /** Input contains Modal dialog component Instance @public */ |
| 68 | @Input() public profileID: string; |
| 69 | |
| 70 | /** Input contains Modal dialog component Instance @public */ |
| 71 | @Input() public profileName: string; |
| 72 | |
| 73 | /** Input contains Modal dialog component Instance @public */ |
| 74 | @Input() public profileDescription: string; |
| 75 | |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 76 | /** check Add @public */ |
| 77 | public isAdd = false; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 78 | |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 79 | /** check Edit @public */ |
| 80 | public isEdit = false; |
| 81 | |
| 82 | /** Edit Payload @public */ |
| 83 | public payload: INFRACONFIGPAYLOAD; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 84 | |
| 85 | /** FormBuilder instance added to the formBuilder @private */ |
| 86 | private formBuilder: FormBuilder; |
| 87 | |
| 88 | /** Utilizes rest service for any CRUD operations @private */ |
| 89 | private restService: RestService; |
| 90 | |
| 91 | /** Notifier service to popup notification @private */ |
| 92 | private notifierService: NotifierService; |
| 93 | |
| 94 | /** Contains tranlsate instance @private */ |
| 95 | private translateService: TranslateService; |
| 96 | |
| 97 | /** Controls the header form @private */ |
| 98 | private headers: HttpHeaders; |
| 99 | |
| 100 | /** Contains all methods related to shared @private */ |
| 101 | private sharedService: SharedService; |
| 102 | |
| 103 | constructor(injector: Injector) { |
| 104 | this.injector = injector; |
| 105 | this.restService = this.injector.get(RestService); |
| 106 | this.activeModal = this.injector.get(NgbActiveModal); |
| 107 | this.formBuilder = this.injector.get(FormBuilder); |
| 108 | this.notifierService = this.injector.get(NotifierService); |
| 109 | this.translateService = this.injector.get(TranslateService); |
| 110 | this.sharedService = this.injector.get(SharedService); |
| 111 | } |
| 112 | |
| 113 | public ngOnInit(): void { |
| 114 | /** On Initializing call the methods */ |
| 115 | this.profileFormAction(); |
| 116 | this.headers = new HttpHeaders({ |
| 117 | Accept: 'application/json', |
| 118 | 'Content-Type': 'application/json', |
| 119 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 120 | }); |
| 121 | if (!isNullOrUndefined(this.profileID)) { |
| 122 | this.isEdit = true; |
| 123 | this.profileForm.patchValue({ name: this.profileName, description: this.profileDescription }); |
| 124 | } else { |
| 125 | this.isAdd = true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** On modal initializing forms @public */ |
| 130 | public profileFormAction(): void { |
| 131 | this.profileForm = this.formBuilder.group({ |
| 132 | name: ['', [Validators.required]], |
| 133 | default: [null], |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 134 | description: ['', [Validators.required]], |
| 135 | updatename: [''], |
| 136 | updatedescription: [''] |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 137 | }); |
| 138 | } |
| 139 | |
| 140 | /** convenience getter for easy access to form fields */ |
| 141 | get f(): FormGroup['controls'] { return this.profileForm.controls; } |
| 142 | |
| 143 | /** On modal submit profileSubmit will called @public */ |
| 144 | public profileSubmit(): void { |
| 145 | this.submitted = true; |
| 146 | this.sharedService.cleanForm(this.profileForm); |
| 147 | if (this.profileForm.invalid) { |
| 148 | return; |
| 149 | } |
| 150 | if (!isNullOrUndefined(this.profileID)) { |
| 151 | this.editProfile(this.profileType); |
| 152 | } else { |
| 153 | this.addProfile(); |
| 154 | } |
| 155 | } |
| 156 | /** Add profile @public */ |
| 157 | public addProfile(): void { |
| 158 | const modalData: MODALCLOSERESPONSEDATA = { |
| 159 | message: 'Done' |
| 160 | }; |
| 161 | if (this.profileType === 'infra-config') { |
| 162 | this.profileUrl = environment.K8SINFRACONFIGPROFILE_URL; |
| 163 | } else if (this.profileType === 'infra-controller') { |
| 164 | this.profileUrl = environment.K8SINFRACONTROLLERPROFILE_URL; |
| 165 | } else if (this.profileType === 'app-profile') { |
| 166 | this.profileUrl = environment.K8SAPPPROFILE_URL; |
| 167 | } else if (this.profileType === 'resource-profile') { |
| 168 | this.profileUrl = environment.K8SRESOURCEPROFILE_URL; |
| 169 | } |
| 170 | const apiURLHeader: APIURLHEADER = { |
| 171 | url: this.profileUrl, |
| 172 | httpOptions: { headers: this.headers } |
| 173 | }; |
| 174 | |
| 175 | this.isLoadingResults = true; |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 176 | delete this.profileForm.value.updatename; |
| 177 | delete this.profileForm.value.updatedescription; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 178 | const payload: INFRACONFIGPAYLOAD = { |
| 179 | name: this.profileForm.value.name, |
| 180 | description: this.profileForm.value.description |
| 181 | }; |
| 182 | this.restService.postResource(apiURLHeader, payload).subscribe((result: {}) => { |
| 183 | this.activeModal.close(modalData); |
| 184 | this.isLoadingResults = false; |
| 185 | this.notifierService.notify('success', |
| 186 | this.translateService.instant('PAGE.K8S.PROFILECREATEDSUCCESSFULLY')); |
| 187 | }, (error: ERRORDATA) => { |
| 188 | this.restService.handleError(error, 'post'); |
| 189 | this.isLoadingResults = false; |
| 190 | }); |
| 191 | } |
| 192 | |
| 193 | /** Edit profile @public */ |
| 194 | public editProfile(profileType: string): void { |
| 195 | const modalData: MODALCLOSERESPONSEDATA = { |
| 196 | message: 'Done' |
| 197 | }; |
| 198 | this.isLoadingResults = true; |
| 199 | if (profileType === 'infra-config') { |
| 200 | this.profileUrl = environment.K8SINFRACONFIGPROFILE_URL + '/' + this.profileID; |
| 201 | } else if (profileType === 'infra-controller') { |
| 202 | this.profileUrl = environment.K8SINFRACONTROLLERPROFILE_URL + '/' + this.profileID; |
| 203 | } else if (this.profileType === 'app-profile') { |
| 204 | this.profileUrl = environment.K8SAPPPROFILE_URL + '/' + this.profileID; |
| 205 | } else if (this.profileType === 'resource-profile') { |
| 206 | this.profileUrl = environment.K8SRESOURCEPROFILE_URL + '/' + this.profileID; |
| 207 | } |
| 208 | const apiURLHeader: APIURLHEADER = { |
| 209 | url: this.profileUrl, |
| 210 | httpOptions: { headers: this.headers } |
| 211 | }; |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 212 | delete this.profileForm.value.name; |
| 213 | delete this.profileForm.value.description; |
| 214 | if (this.profileForm.value.updatename === '') { |
| 215 | this.payload = { |
| 216 | description: this.profileForm.value.updatedescription |
| 217 | }; |
| 218 | delete this.profileForm.value.updatename; |
| 219 | } else if (this.profileForm.value.updatedescription === '') { |
| 220 | this.payload = { |
| 221 | name: this.profileForm.value.updatename |
| 222 | }; |
| 223 | delete this.profileForm.value.updatedescription; |
| 224 | } else { |
| 225 | this.payload = { |
| 226 | name: this.profileForm.value.updatenamename, |
| 227 | description: this.profileForm.value.updatedescription |
| 228 | }; |
| 229 | } |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 230 | |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 231 | this.restService.patchResource(apiURLHeader, this.payload).subscribe((result: {}) => { |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 232 | this.activeModal.close(modalData); |
| 233 | this.isLoadingResults = false; |
| 234 | this.notifierService.notify('success', |
| 235 | this.translateService.instant('PAGE.K8S.PROFILEEDITEDSUCCESSFULLY')); |
| 236 | }, (error: ERRORDATA) => { |
| 237 | this.restService.handleError(error, 'post'); |
| 238 | this.isLoadingResults = false; |
| 239 | }); |
| 240 | } |
| 241 | } |