Procházet zdrojové kódy

fix frontend validation for creating new folder and import dashboard (#10737)

Fixes #10731
Marcus Efraimsson před 8 roky
rodič
revize
4b64c1d0d4

+ 1 - 1
public/app/features/dashboard/create_folder_ctrl.ts

@@ -27,7 +27,7 @@ export class CreateFolderCtrl {
     this.titleTouched = true;
     this.titleTouched = true;
 
 
     this.validationSrv
     this.validationSrv
-      .validateNewDashboardOrFolderName(this.title)
+      .validateNewFolderName(this.title)
       .then(() => {
       .then(() => {
         this.hasValidationError = false;
         this.hasValidationError = false;
       })
       })

+ 1 - 1
public/app/features/dashboard/dashboard_import_ctrl.ts

@@ -93,7 +93,7 @@ export class DashboardImportCtrl {
     this.nameExists = false;
     this.nameExists = false;
 
 
     this.validationSrv
     this.validationSrv
-      .validateNewDashboardOrFolderName(this.dash.title)
+      .validateNewDashboardName(0, this.dash.title)
       .then(() => {
       .then(() => {
         this.hasNameValidationError = false;
         this.hasNameValidationError = false;
       })
       })

+ 1 - 1
public/app/features/dashboard/folder_picker/folder_picker.ts

@@ -67,7 +67,7 @@ export class FolderPickerCtrl {
     this.newFolderNameTouched = true;
     this.newFolderNameTouched = true;
 
 
     this.validationSrv
     this.validationSrv
-      .validateNewDashboardOrFolderName(this.newFolderName)
+      .validateNewFolderName(this.newFolderName)
       .then(() => {
       .then(() => {
         this.hasValidationError = false;
         this.hasValidationError = false;
       })
       })

+ 1 - 1
public/app/features/dashboard/specs/dashboard_import_ctrl.jest.ts

@@ -19,7 +19,7 @@ describe('DashboardImportCtrl', function() {
     };
     };
 
 
     validationSrv = {
     validationSrv = {
-      validateNewDashboardOrFolderName: jest.fn().mockReturnValue(Promise.resolve()),
+      validateNewDashboardName: jest.fn().mockReturnValue(Promise.resolve()),
     };
     };
 
 
     ctx.ctrl = new DashboardImportCtrl(backendSrv, validationSrv, navModelSrv, {}, {}, {});
     ctx.ctrl = new DashboardImportCtrl(backendSrv, validationSrv, navModelSrv, {}, {}, {});

+ 34 - 6
public/app/features/dashboard/validation_srv.ts

@@ -1,13 +1,27 @@
 import coreModule from 'app/core/core_module';
 import coreModule from 'app/core/core_module';
 
 
+const hitTypes = {
+  FOLDER: 'dash-folder',
+  DASHBOARD: 'dash-db',
+};
+
 export class ValidationSrv {
 export class ValidationSrv {
   rootName = 'general';
   rootName = 'general';
 
 
   /** @ngInject */
   /** @ngInject */
   constructor(private $q, private backendSrv) {}
   constructor(private $q, private backendSrv) {}
 
 
-  validateNewDashboardOrFolderName(name) {
+  validateNewDashboardName(folderId, name) {
+    return this.validate(folderId, name, 'A dashboard in this folder with the same name already exists');
+  }
+
+  validateNewFolderName(name) {
+    return this.validate(0, name, 'A folder or dashboard in the general folder with the same name already exists');
+  }
+
+  private validate(folderId, name, existingErrorMessage) {
     name = (name || '').trim();
     name = (name || '').trim();
+    const nameLowerCased = name.toLowerCase();
 
 
     if (name.length === 0) {
     if (name.length === 0) {
       return this.$q.reject({
       return this.$q.reject({
@@ -16,7 +30,7 @@ export class ValidationSrv {
       });
       });
     }
     }
 
 
-    if (name.toLowerCase() === this.rootName) {
+    if (folderId === 0 && nameLowerCased === this.rootName) {
       return this.$q.reject({
       return this.$q.reject({
         type: 'EXISTING',
         type: 'EXISTING',
         message: 'This is a reserved name and cannot be used for a folder.',
         message: 'This is a reserved name and cannot be used for a folder.',
@@ -25,12 +39,26 @@ export class ValidationSrv {
 
 
     let deferred = this.$q.defer();
     let deferred = this.$q.defer();
 
 
-    this.backendSrv.search({ query: name }).then(res => {
-      for (let hit of res) {
-        if (name.toLowerCase() === hit.title.toLowerCase()) {
+    const promises = [];
+    promises.push(this.backendSrv.search({ type: hitTypes.FOLDER, folderIds: [folderId], query: name }));
+    promises.push(this.backendSrv.search({ type: hitTypes.DASHBOARD, folderIds: [folderId], query: name }));
+
+    this.$q.all(promises).then(res => {
+      let hits = [];
+
+      if (res.length > 0 && res[0].length > 0) {
+        hits = res[0];
+      }
+
+      if (res.length > 1 && res[1].length > 0) {
+        hits = hits.concat(res[1]);
+      }
+
+      for (let hit of hits) {
+        if (nameLowerCased === hit.title.toLowerCase()) {
           deferred.reject({
           deferred.reject({
             type: 'EXISTING',
             type: 'EXISTING',
-            message: 'A folder or dashboard with the same name already exists',
+            message: existingErrorMessage,
           });
           });
           break;
           break;
         }
         }