]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: Add ability to cancel notifications. 21386/head
authorVolker Theile <vtheile@suse.com>
Thu, 19 Apr 2018 13:57:11 +0000 (15:57 +0200)
committerVolker Theile <vtheile@suse.com>
Thu, 19 Apr 2018 14:39:29 +0000 (16:39 +0200)
Signed-off-by: Volker Theile <vtheile@suse.com>
src/pybind/mgr/dashboard/frontend/src/app/shared/services/api-interceptor.service.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/services/notification.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/services/notification.service.ts

index ed5ea8fac94660c7658189d214e78ac4a56909bd..e2a2cee2a6700cb2019f6f7779ba901e12ff3b1b 100644 (file)
@@ -24,7 +24,7 @@ export class ApiInterceptorService implements HttpInterceptor {
               public notificationService: NotificationService) {}
 
   intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
-    return next.handle(request).catch(resp => {
+    return next.handle(request).catch((resp) => {
       if (resp instanceof HttpErrorResponse) {
         let showNotification = true;
         switch (resp.status) {
@@ -39,11 +39,32 @@ export class ApiInterceptorService implements HttpInterceptor {
             showNotification = false;
             break;
         }
+
+        let timeoutId;
         if (showNotification) {
-          this.notificationService.show(NotificationType.error,
+          timeoutId = this.notificationService.show(NotificationType.error,
             resp.error.detail || '',
             `${resp.status} - ${resp.statusText}`);
         }
+
+        /**
+         * Decorated preventDefault method (in case error previously had
+         * preventDefault method defined). If called, it will prevent a
+         * notification to be shown.
+         */
+        resp['preventDefault'] = () => {
+          this.notificationService.cancel(timeoutId);
+        };
+
+        /**
+         * If called, it will prevent a notification for the specific status code.
+         * @param {number} status The status code to be ignored.
+         */
+        resp['ignoreStatusCode'] = function(status: number) {
+          if (this.status === status) {
+            this.preventDefault();
+          }
+        };
       }
       // Return the error to the method that called it.
       return Observable.throw(resp);
index e20f89612d468db53a847edaa9cbc49b17afc499..e7f5e99b68e53882c14bb51cb91c63793528ac17 100644 (file)
@@ -2,6 +2,7 @@ import { inject, TestBed } from '@angular/core/testing';
 
 import { ToastOptions, ToastsManager } from 'ng2-toastr';
 
+import { NotificationType } from '../enum/notification-type.enum';
 import { NotificationService } from './notification.service';
 import { TaskManagerMessageService } from './task-manager-message.service';
 import { TaskManagerService } from './task-manager.service';
@@ -19,7 +20,17 @@ describe('NotificationService', () => {
     });
   });
 
-  it('should be created', inject([NotificationService], (service: NotificationService) => {
-    expect(service).toBeTruthy();
-  }));
+  it('should be created',
+    inject([NotificationService], (service: NotificationService) => {
+      expect(service).toBeTruthy();
+    }));
+
+  it('should not create a notification',
+    inject([NotificationService], (service: NotificationService) => {
+      expect(service).toBeTruthy();
+      service.removeAll();
+      const timeoutId = service.show(NotificationType.error, 'Simple test');
+      service.cancel(timeoutId);
+      expect(service['dataSource'].getValue().length).toBe(0);
+    }));
 });
index 88f83960e81bad734a63c7cf9981a8f7c5c9f21f..5057e9af722ea27b4e9582980ec2029fa2d002b1 100644 (file)
@@ -63,27 +63,28 @@ export class NotificationService {
 
   /**
    * Method for showing a notification.
-   *
    * @param {NotificationType} type toastr type
    * @param {string} message
    * @param {string} [title]
    * @param {*} [options] toastr compatible options, used when creating a toastr
    * @memberof NotificationService
+   * @returns The timeout ID that is set to be able to cancel the notification.
    */
   show(type: NotificationType, message: string, title?: string, options?: any) {
-    this.save(type, message, title);
-
-    switch (type) {
-      case NotificationType.error:
-        this.toastr.error(message, title, options);
-        break;
-      case NotificationType.info:
-        this.toastr.info(message, title, options);
-        break;
-      case NotificationType.success:
-        this.toastr.success(message, title, options);
-        break;
-    }
+    return setTimeout(() => {
+      this.save(type, message, title);
+      switch (type) {
+        case NotificationType.error:
+          this.toastr.error(message, title, options);
+          break;
+        case NotificationType.info:
+          this.toastr.info(message, title, options);
+          break;
+        case NotificationType.success:
+          this.toastr.success(message, title, options);
+          break;
+      }
+    }, 10);
   }
 
   notifyTask(finishedTask: FinishedTask, success: boolean = true) {
@@ -96,4 +97,14 @@ export class NotificationService {
         this.taskManagerMessageService.getDescription(finishedTask));
     }
   }
+
+  /**
+   * Prevent the notification from being shown.
+   * @param {number} timeoutId A number representing the ID of the timeout to be canceled.
+   */
+  cancel(timeoutId) {
+    if (timeoutId) {
+      clearTimeout(timeoutId);
+    }
+  }
 }