From caf7e7a43477abccb44721d12f39b393dd2a5f11 Mon Sep 17 00:00:00 2001 From: Tiago Melo Date: Tue, 10 Apr 2018 16:01:42 +0100 Subject: [PATCH] mgr/dashboard: Replace toastr calls with notification service Signed-off-by: Tiago Melo --- .../core/auth/login/login.component.spec.ts | 5 +- .../app/core/auth/login/login.component.ts | 7 +-- .../services/auth-interceptor.service.ts | 57 +++++++++++-------- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/login/login.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/login/login.component.spec.ts index b8307b594e8..665a3db0e42 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/login/login.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/login/login.component.spec.ts @@ -3,8 +3,6 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { FormsModule } from '@angular/forms'; import { RouterTestingModule } from '@angular/router/testing'; -import { ToastModule } from 'ng2-toastr'; - import { SharedModule } from '../../../shared/shared.module'; import { LoginComponent } from './login.component'; @@ -18,8 +16,7 @@ describe('LoginComponent', () => { FormsModule, SharedModule, RouterTestingModule, - HttpClientTestingModule, - ToastModule.forRoot() + HttpClientTestingModule ], declarations: [ LoginComponent diff --git a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/login/login.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/login/login.component.ts index f1a1f90f088..4147af81398 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/login/login.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/login/login.component.ts @@ -1,8 +1,6 @@ import { Component, OnInit, ViewContainerRef } from '@angular/core'; import { Router } from '@angular/router'; -import { ToastsManager } from 'ng2-toastr'; - import { AuthService } from '../../../shared/api/auth.service'; import { Credentials } from '../../../shared/models/credentials'; import { AuthStorageService } from '../../../shared/services/auth-storage.service'; @@ -18,10 +16,7 @@ export class LoginComponent implements OnInit { constructor(private authService: AuthService, private authStorageService: AuthStorageService, - private router: Router, - public toastr: ToastsManager, - private vcr: ViewContainerRef) { - this.toastr.setRootViewContainerRef(vcr); + private router: Router) { } ngOnInit() { diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-interceptor.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-interceptor.service.ts index cc7cffe8c2c..ad391208267 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-interceptor.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-interceptor.service.ts @@ -1,43 +1,50 @@ import { - HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest + HttpErrorResponse, + HttpEvent, + HttpHandler, + HttpInterceptor, + HttpRequest } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; -import { ToastsManager } from 'ng2-toastr'; import 'rxjs/add/observable/throw'; import 'rxjs/add/operator/catch'; import { Observable } from 'rxjs/Observable'; +import { NotificationType } from '../enum/notification-type.enum'; import { AuthStorageService } from './auth-storage.service'; +import { NotificationService } from './notification.service'; @Injectable() export class AuthInterceptorService implements HttpInterceptor { - - constructor(private router: Router, - private authStorageService: AuthStorageService, - public toastr: ToastsManager) { - } + constructor( + private router: Router, + private authStorageService: AuthStorageService, + public notificationService: NotificationService + ) {} intercept(request: HttpRequest, next: HttpHandler): Observable> { - return next.handle(request) - .catch((resp) => { - if (resp instanceof HttpErrorResponse) { - switch (resp.status) { - case 404: - this.router.navigate(['/404']); - break; - case 401: - this.authStorageService.remove(); - this.router.navigate(['/login']); - // falls through - default: - this.toastr.error(resp.error.detail || '', - `${resp.status} - ${resp.statusText}`); - } + return next.handle(request).catch(resp => { + if (resp instanceof HttpErrorResponse) { + switch (resp.status) { + case 404: + this.router.navigate(['/404']); + break; + case 401: + this.authStorageService.remove(); + this.router.navigate(['/login']); + // falls through + default: + this.notificationService.show( + NotificationType.error, + resp.error.detail || '', + `${resp.status} - ${resp.statusText}` + ); } - // Return the error to the method that called it. - return Observable.throw(resp); - }); + } + // Return the error to the method that called it. + return Observable.throw(resp); + }); } } -- 2.47.3