7 changed files with 75 additions and 2 deletions
@ -0,0 +1,49 @@ |
|||||
|
|
||||
|
import { Injectable } from '@angular/core'; |
||||
|
import { |
||||
|
HttpRequest, |
||||
|
HttpHandler, |
||||
|
HttpEvent, |
||||
|
HttpInterceptor, |
||||
|
HttpResponse |
||||
|
} from '@angular/common/http'; |
||||
|
import { Observable, throwError } from 'rxjs'; |
||||
|
import { catchError, tap, } from 'rxjs/operators'; |
||||
|
import { MatSnackBar } from '@angular/material/snack-bar'; |
||||
|
|
||||
|
@Injectable() |
||||
|
export class SnackbarInterceptor implements HttpInterceptor { |
||||
|
|
||||
|
constructor(private snackBar: MatSnackBar) { } |
||||
|
|
||||
|
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { |
||||
|
return next.handle(request).pipe( |
||||
|
tap(e => { |
||||
|
if (request.method == "POST" || request.method == "PUT") |
||||
|
if (e instanceof HttpResponse && e.status == 200) { |
||||
|
this.snackBar.open('Saved successfully.', 'close', { duration: 2000, panelClass: 'successSnack' }); |
||||
|
} |
||||
|
}), |
||||
|
catchError(resp => { |
||||
|
console.log(resp); |
||||
|
let message = resp.error.error |
||||
|
switch(resp.error.message) { |
||||
|
case "RaceClosed": { |
||||
|
message = "Upload Failed: Race closed to new uploads."; |
||||
|
break; |
||||
|
} |
||||
|
case "RaceNotOpen": { |
||||
|
message = "Upload Failed: Race not open for uploads yet."; |
||||
|
break; |
||||
|
} |
||||
|
case "EarlyReplay": { |
||||
|
message = "Upload Failed: Replay made before race open."; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
this.snackBar.open(message, 'dismiss', { panelClass: ['errorSnack'], verticalPosition: 'top'}); |
||||
|
return throwError(resp); |
||||
|
}) |
||||
|
); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue