From: Volker Theile Date: Thu, 19 Apr 2018 13:57:11 +0000 (+0200) Subject: mgr/dashboard: Add ability to cancel notifications. X-Git-Tag: v13.1.0~180^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=3416cf2ec4928ecdc67a43938f61a30428a5571c;p=ceph.git mgr/dashboard: Add ability to cancel notifications. Signed-off-by: Volker Theile --- diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/api-interceptor.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/api-interceptor.service.ts index ed5ea8fac946..e2a2cee2a670 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/api-interceptor.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/api-interceptor.service.ts @@ -24,7 +24,7 @@ export class ApiInterceptorService implements HttpInterceptor { public notificationService: NotificationService) {} intercept(request: HttpRequest, next: HttpHandler): Observable> { - 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); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/notification.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/notification.service.spec.ts index e20f89612d46..e7f5e99b68e5 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/notification.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/notification.service.spec.ts @@ -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); + })); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/notification.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/notification.service.ts index 88f83960e81b..5057e9af722e 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/notification.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/notification.service.ts @@ -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); + } + } }