datasurvey/src/main/webapp/app/core/interceptor/notification.interceptor.ts

39 lines
1.3 KiB
TypeScript

import { HttpInterceptor, HttpRequest, HttpResponse, HttpHandler, HttpEvent } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { AlertService } from 'app/core/util/alert.service';
@Injectable()
export class NotificationInterceptor implements HttpInterceptor {
constructor(private alertService: AlertService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
tap((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
let alert: string | null = null;
let alertParams: string | null = null;
for (const headerKey of event.headers.keys()) {
if (headerKey.toLowerCase().endsWith('app-alert')) {
alert = event.headers.get(headerKey);
} else if (headerKey.toLowerCase().endsWith('app-params')) {
alertParams = decodeURIComponent(event.headers.get(headerKey)!.replace(/\+/g, ' '));
}
}
if (alert) {
this.alertService.addAlert({
type: 'success',
translationKey: alert,
translationParams: { param: alertParams },
});
}
}
})
);
}
}