Skip to content

Commit

Permalink
mgr/dashboard: Add ability to cancel notifications.
Browse files Browse the repository at this point in the history
Signed-off-by: Volker Theile <vtheile@suse.com>
  • Loading branch information
votdev committed Apr 19, 2018
1 parent 9db2d05 commit 3416cf2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
Expand Up @@ -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';
Expand All @@ -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);
}));
});
Expand Up @@ -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) {
Expand All @@ -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);
}
}
}

0 comments on commit 3416cf2

Please sign in to comment.