Fix Bug 2294: Incorrect notification in update descriptor
[osm/NG-UI.git] / src / app / projects / project-create-update / ProjectCreateUpdateComponent.ts
index 01f963e..af6388d 100644 (file)
@@ -18,6 +18,7 @@
 /**
  * @file Project Add Modal
  */
+import { isNullOrUndefined } from 'util';
 import { Component, Injector, Input, OnInit } from '@angular/core';
 import { AbstractControl, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
@@ -30,7 +31,6 @@ import { ProjectData, ProjectDetails, QUOTAITEM, QUOTA_ITEMS } from 'ProjectMode
 import { ProjectService } from 'ProjectService';
 import { RestService } from 'RestService';
 import { SharedService } from 'SharedService';
-import { isNullOrUndefined } from 'util';
 
 /**
  * Creating component
@@ -164,6 +164,12 @@ export class ProjectCreateUpdateComponent implements OnInit {
             message: 'Done'
         };
         this.sharedService.cleanForm(this.projectForm);
+        for (const data of this.quotaItems) {
+            if (this.getFormControl(data.value).value > data.maxValue) {
+                this.notifierService.notify('error', this.translateService.instant('PAGE.PROJECT.QUOTALIMIT'));
+                break;
+            }
+        }
         if (!this.projectForm.invalid) {
             if (userType === 'Add') {
                 this.createProject();
@@ -197,16 +203,20 @@ export class ProjectCreateUpdateComponent implements OnInit {
     public checkQuota(): void {
         if (this.getFormControl('enable_quota').value) {
             this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
-                this.projectForm.addControl(quotaItem.value, new FormControl(quotaItem.minValue, Validators.required));
+                this.projectForm.addControl(quotaItem.value, new FormControl(''));
             });
         } else {
             this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
-                this.getFormControl(quotaItem.value).setValue(quotaItem.minValue);
+                this.getFormControl(quotaItem.value).setValue('');
             });
         }
     }
     /** Edit project @public */
     public editProject(): void {
+        if (!this.projectForm.dirty) {
+            this.notifierService.notify('warning', this.translateService.instant('PAGE.TOPOLOGY.DATAEMPTY'));
+            return;
+        }
         this.isLoadingResults = true;
         const apiURLHeader: APIURLHEADER = {
             url: environment.PROJECTS_URL + '/' + this.projectRef
@@ -220,6 +230,7 @@ export class ProjectCreateUpdateComponent implements OnInit {
             this.isLoadingResults = false;
             this.projectService.setHeaderProjects();
             this.notifierService.notify('success', this.translateService.instant('PAGE.PROJECT.UPDATEDSUCCESSFULLY'));
+            this.activeModal.close(this.modalData);
         }, (error: ERRORDATA): void => {
             this.restService.handleError(error, 'patch');
             this.isLoadingResults = false;
@@ -239,6 +250,7 @@ export class ProjectCreateUpdateComponent implements OnInit {
 
     /** Used to get the AbstractControl of controlName passed @private */
     private getFormControl(controlName: string): AbstractControl {
+        // eslint-disable-next-line security/detect-object-injection
         return this.projectForm.controls[controlName];
     }
 
@@ -247,8 +259,13 @@ export class ProjectCreateUpdateComponent implements OnInit {
         if (this.getFormControl('enable_quota').value) {
             payload.quotas = {};
             this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
-                payload.quotas[quotaItem.value] = this.getFormControl(quotaItem.value).value;
+                if (this.getFormControl(quotaItem.value).value !== '') {
+                    payload.quotas[quotaItem.value] = this.getFormControl(quotaItem.value).value;
+                }
             });
+            if (Object.keys(payload.quotas).length === 0) {
+                delete payload.quotas;
+            }
         }
     }
 
@@ -259,16 +276,15 @@ export class ProjectCreateUpdateComponent implements OnInit {
             this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
                 if (!isNullOrUndefined(quotaRef[quotaItem.value])) {
                     this.projectForm.addControl(quotaItem.value, new FormControl(quotaRef[quotaItem.value],
-                        [Validators.required, Validators.min(quotaItem.minValue), Validators.max(quotaItem.maxValue)]));
+                        [Validators.max(quotaItem.maxValue)]));
                 } else {
-                    this.projectForm.addControl(quotaItem.value, new FormControl(quotaItem.minValue, [Validators.required,
-                    Validators.min(quotaItem.minValue), Validators.max(quotaItem.maxValue)]));
+                    this.projectForm.addControl(quotaItem.value, new FormControl('', [Validators.max(quotaItem.maxValue)]));
                 }
             });
         } else {
             this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
-                this.projectForm.addControl(quotaItem.value, new FormControl(quotaItem.minValue, [Validators.required,
-                Validators.min(quotaItem.minValue), Validators.max(quotaItem.maxValue)]));
+                this.projectForm.addControl(quotaItem.value, new FormControl('', [Validators.min(quotaItem.minValue),
+                Validators.max(quotaItem.maxValue)]));
             });
         }
     }