blob: 01f963e6d0310e4addb48a770c028004edb1b4df [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 Project Add Modal
20 */
21import { Component, Injector, Input, OnInit } from '@angular/core';
Barath Kumar R09cd4ec2020-07-07 16:12:32 +053022import { AbstractControl, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
kumaran.m3b4814a2020-05-01 19:48:54 +053023import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
24import { TranslateService } from '@ngx-translate/core';
25import { NotifierService } from 'angular-notifier';
Barath Kumar R16070582021-02-08 18:19:35 +053026import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, TYPESECTION } from 'CommonModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053027import { DataService } from 'DataService';
28import { environment } from 'environment';
Barath Kumar R16070582021-02-08 18:19:35 +053029import { ProjectData, ProjectDetails, QUOTAITEM, QUOTA_ITEMS } from 'ProjectModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053030import { ProjectService } from 'ProjectService';
31import { RestService } from 'RestService';
32import { SharedService } from 'SharedService';
33import { isNullOrUndefined } from 'util';
34
35/**
36 * Creating component
37 * @Component takes ProjectCreateUpdateComponent.html as template url
38 */
39@Component({
Barath Kumar R16070582021-02-08 18:19:35 +053040 selector: 'app-project-create-update',
41 templateUrl: './ProjectCreateUpdateComponent.html',
42 styleUrls: ['./ProjectCreateUpdateComponent.scss']
kumaran.m3b4814a2020-05-01 19:48:54 +053043})
44/** Exporting a class @exports ProjectCreateUpdateComponent */
45export class ProjectCreateUpdateComponent implements OnInit {
Barath Kumar R16070582021-02-08 18:19:35 +053046 /** To inject services @public */
47 public injector: Injector;
kumaran.m3b4814a2020-05-01 19:48:54 +053048
Barath Kumar R16070582021-02-08 18:19:35 +053049 /** Instance of the rest service @public */
50 public restService: RestService;
kumaran.m3b4814a2020-05-01 19:48:54 +053051
Barath Kumar R16070582021-02-08 18:19:35 +053052 /** Instance for active modal service @public */
53 public activeModal: NgbActiveModal;
kumaran.m3b4814a2020-05-01 19:48:54 +053054
Barath Kumar R16070582021-02-08 18:19:35 +053055 /** Contains the recently created project details @public */
56 public recentProject: ProjectDetails;
kumaran.m3b4814a2020-05-01 19:48:54 +053057
Barath Kumar R16070582021-02-08 18:19:35 +053058 /** Contains project create or edit @public */
59 public getProjectType: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053060
Barath Kumar R16070582021-02-08 18:19:35 +053061 /** To inject input type services @public */
62 @Input() public projectType: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053063
Barath Kumar R16070582021-02-08 18:19:35 +053064 /** FormGroup user Edit Account added to the form @ html @public */
65 public projectForm: FormGroup;
kumaran.m3b4814a2020-05-01 19:48:54 +053066
Barath Kumar R16070582021-02-08 18:19:35 +053067 /** Form submission Add */
68 public submitted: boolean = false;
kumaran.m3b4814a2020-05-01 19:48:54 +053069
Barath Kumar R16070582021-02-08 18:19:35 +053070 /** Check the loading results for loader status @public */
71 public isLoadingResults: boolean = false;
kumaran.m3b4814a2020-05-01 19:48:54 +053072
Barath Kumar R16070582021-02-08 18:19:35 +053073 /** Give the message for the loading @public */
74 public message: string = 'PLEASEWAIT';
kumaran.m3b4814a2020-05-01 19:48:54 +053075
Barath Kumar R16070582021-02-08 18:19:35 +053076 /** Holds list of domains @public */
77 public domains: TYPESECTION[] = [];
kumaran.m3b4814a2020-05-01 19:48:54 +053078
Barath Kumar R16070582021-02-08 18:19:35 +053079 /** Holds list of quota items @public */
80 public quotaItems: QUOTAITEM[] = QUOTA_ITEMS;
Barath Kumar R09cd4ec2020-07-07 16:12:32 +053081
Barath Kumar R16070582021-02-08 18:19:35 +053082 /** Holds project reference from response @public */
83 public quotaRefs: {} = null;
Barath Kumar R09cd4ec2020-07-07 16:12:32 +053084
Barath Kumar R16070582021-02-08 18:19:35 +053085 /** FormBuilder instance added to the formBuilder @private */
86 private formBuilder: FormBuilder;
kumaran.m3b4814a2020-05-01 19:48:54 +053087
Barath Kumar R16070582021-02-08 18:19:35 +053088 /** DataService to pass the data from one component to another @private */
89 private dataService: DataService;
kumaran.m3b4814a2020-05-01 19:48:54 +053090
Barath Kumar R16070582021-02-08 18:19:35 +053091 /** Contains project name ref @private */
92 private projectRef: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053093
Barath Kumar R16070582021-02-08 18:19:35 +053094 /** Notifier service to popup notification @private */
95 private notifierService: NotifierService;
kumaran.m3b4814a2020-05-01 19:48:54 +053096
Barath Kumar R16070582021-02-08 18:19:35 +053097 /** Contains tranlsate instance @private */
98 private translateService: TranslateService;
kumaran.m3b4814a2020-05-01 19:48:54 +053099
Barath Kumar R16070582021-02-08 18:19:35 +0530100 /** Contains all methods related to shared @private */
101 private sharedService: SharedService;
kumaran.m3b4814a2020-05-01 19:48:54 +0530102
Barath Kumar R16070582021-02-08 18:19:35 +0530103 /** ModalData instance of modal @private */
104 private modalData: MODALCLOSERESPONSEDATA;
kumaran.m3b4814a2020-05-01 19:48:54 +0530105
Barath Kumar R16070582021-02-08 18:19:35 +0530106 /** Holds all project details @private */
107 private projectService: ProjectService;
kumaran.m3b4814a2020-05-01 19:48:54 +0530108
Barath Kumar R16070582021-02-08 18:19:35 +0530109 constructor(injector: Injector) {
110 this.injector = injector;
111 this.formBuilder = this.injector.get(FormBuilder);
112 this.restService = this.injector.get(RestService);
113 this.activeModal = this.injector.get(NgbActiveModal);
114 this.dataService = this.injector.get(DataService);
115 this.notifierService = this.injector.get(NotifierService);
116 this.translateService = this.injector.get(TranslateService);
117 this.sharedService = this.injector.get(SharedService);
118 this.projectService = this.injector.get(ProjectService);
119 /** Initializing Form Action */
120 this.projectForm = this.formBuilder.group({
121 project_name: ['', Validators.required],
122 domain_name: [null],
123 enable_quota: [false, Validators.required]
124 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530125 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530126
Barath Kumar R16070582021-02-08 18:19:35 +0530127 /** convenience getter for easy access to form fields */
128 get f(): FormGroup['controls'] { return this.projectForm.controls; }
kumaran.m3b4814a2020-05-01 19:48:54 +0530129
Barath Kumar R16070582021-02-08 18:19:35 +0530130 /** Lifecyle Hooks the trigger before component is instantiate @public */
131 public ngOnInit(): void {
132 this.getProjectType = this.projectType;
133 if (this.getProjectType === 'Edit') {
134 this.dataService.currentMessage.subscribe((data: ProjectData): void => {
135 if (data.projectName !== undefined || data.projectName !== '' || data.projectName !== null) {
136 this.projectForm.patchValue({ project_name: data.projectName });
137 this.projectRef = data.id;
138 this.quotaRefs = data.quotas;
139 this.patchQuotaInfo(this.quotaRefs);
140 }
141 });
Barath Kumar R09cd4ec2020-07-07 16:12:32 +0530142 } else {
Barath Kumar R16070582021-02-08 18:19:35 +0530143 this.patchQuotaInfo();
144 this.getProjects();
Barath Kumar R09cd4ec2020-07-07 16:12:32 +0530145 }
Barath Kumar R09cd4ec2020-07-07 16:12:32 +0530146 }
Barath Kumar R16070582021-02-08 18:19:35 +0530147
148 /** Get the last project name @public */
149 public getProjects(): void {
150 this.isLoadingResults = true;
151 this.restService.getResource(environment.PROJECTS_URL).subscribe((projects: ProjectDetails[]): void => {
152 this.recentProject = projects.slice(-1).pop();
153 this.getDomainList();
154 }, (error: ERRORDATA): void => {
155 this.restService.handleError(error, 'get');
156 this.isLoadingResults = false;
157 });
158 }
159
160 /** On modal submit users acction will called @public */
161 public projectAction(userType: string): void {
162 this.submitted = true;
163 this.modalData = {
164 message: 'Done'
165 };
166 this.sharedService.cleanForm(this.projectForm);
167 if (!this.projectForm.invalid) {
168 if (userType === 'Add') {
169 this.createProject();
170 } else if (userType === 'Edit') {
171 this.editProject();
172 }
173 }
174 }
175
176 /** Create project @public */
177 public createProject(): void {
178 this.isLoadingResults = true;
179 const apiURLHeader: APIURLHEADER = {
180 url: environment.PROJECTS_URL
181 };
182 const projectPayload: ProjectDetails = {
183 name: this.projectForm.value.project_name,
184 domain_name: !isNullOrUndefined(this.projectForm.value.domain_name) ? this.projectForm.value.domain_name : undefined
185 };
186 this.addQuotaLimit(projectPayload);
187 this.restService.postResource(apiURLHeader, projectPayload).subscribe((): void => {
188 this.activeModal.close(this.modalData);
189 this.isLoadingResults = false;
190 this.notifierService.notify('success', this.translateService.instant('PAGE.PROJECT.CREATEDSUCCESSFULLY'));
191 }, (error: ERRORDATA): void => {
192 this.restService.handleError(error, 'post');
193 this.isLoadingResults = false;
194 });
195 }
196 /** Handle enable quota limit checkbox event @public */
197 public checkQuota(): void {
198 if (this.getFormControl('enable_quota').value) {
199 this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
200 this.projectForm.addControl(quotaItem.value, new FormControl(quotaItem.minValue, Validators.required));
201 });
202 } else {
203 this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
204 this.getFormControl(quotaItem.value).setValue(quotaItem.minValue);
205 });
206 }
207 }
208 /** Edit project @public */
209 public editProject(): void {
210 this.isLoadingResults = true;
211 const apiURLHeader: APIURLHEADER = {
212 url: environment.PROJECTS_URL + '/' + this.projectRef
213 };
214 const projectPayload: ProjectDetails = {
215 name: this.projectForm.value.project_name
216 };
217 this.addQuotaLimit(projectPayload);
218 this.restService.patchResource(apiURLHeader, projectPayload).subscribe((): void => {
219 this.activeModal.close(this.modalData);
220 this.isLoadingResults = false;
221 this.projectService.setHeaderProjects();
222 this.notifierService.notify('success', this.translateService.instant('PAGE.PROJECT.UPDATEDSUCCESSFULLY'));
223 }, (error: ERRORDATA): void => {
224 this.restService.handleError(error, 'patch');
225 this.isLoadingResults = false;
226 });
227 }
228 /** Get domain name list @private */
229 private getDomainList(): void {
230 this.isLoadingResults = true;
231 this.sharedService.getDomainName().subscribe((domainList: TYPESECTION[]): void => {
232 this.domains = domainList;
233 this.isLoadingResults = false;
234 }, (error: ERRORDATA): void => {
235 this.isLoadingResults = false;
236 this.restService.handleError(error, 'get');
237 });
238 }
239
240 /** Used to get the AbstractControl of controlName passed @private */
241 private getFormControl(controlName: string): AbstractControl {
242 return this.projectForm.controls[controlName];
243 }
244
245 /** Add quota information to payload @private */
246 private addQuotaLimit(payload: ProjectDetails): void {
247 if (this.getFormControl('enable_quota').value) {
248 payload.quotas = {};
249 this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
250 payload.quotas[quotaItem.value] = this.getFormControl(quotaItem.value).value;
251 });
252 }
253 }
254
255 /** Set quota information in project form model @private */
256 private patchQuotaInfo(quotaRef?: {}): void {
257 if (quotaRef !== null && this.getProjectType === 'Edit') {
258 this.getFormControl('enable_quota').setValue(true);
259 this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
260 if (!isNullOrUndefined(quotaRef[quotaItem.value])) {
261 this.projectForm.addControl(quotaItem.value, new FormControl(quotaRef[quotaItem.value],
262 [Validators.required, Validators.min(quotaItem.minValue), Validators.max(quotaItem.maxValue)]));
263 } else {
264 this.projectForm.addControl(quotaItem.value, new FormControl(quotaItem.minValue, [Validators.required,
265 Validators.min(quotaItem.minValue), Validators.max(quotaItem.maxValue)]));
266 }
267 });
268 } else {
269 this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
270 this.projectForm.addControl(quotaItem.value, new FormControl(quotaItem.minValue, [Validators.required,
271 Validators.min(quotaItem.minValue), Validators.max(quotaItem.maxValue)]));
272 });
273 }
274 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530275}