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) {
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);
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';
});
});
- 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);
+ }));
});
/**
* 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) {
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);
+ }
+ }
}