Initial commit for the Angular part. We are nowhere yet.
This commit is contained in:
37
bookie/src/app/core/core.module.ts
Normal file
37
bookie/src/app/core/core.module.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import {NgModule} from '@angular/core';
|
||||
import {CommonModule} from '@angular/common';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatMenuModule } from '@angular/material/menu';
|
||||
import { MatToolbarModule } from '@angular/material/toolbar';
|
||||
import {RouterModule} from '@angular/router';
|
||||
import {HTTP_INTERCEPTORS} from '@angular/common/http';
|
||||
import {HttpAuthInterceptor} from './http-auth-interceptor';
|
||||
import {LoadingBarHttpClientModule} from '@ngx-loading-bar/http-client';
|
||||
import {LoadingBarRouterModule} from '@ngx-loading-bar/router';
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
CommonModule,
|
||||
LoadingBarHttpClientModule,
|
||||
LoadingBarRouterModule,
|
||||
MatButtonModule,
|
||||
MatIconModule,
|
||||
MatMenuModule,
|
||||
MatToolbarModule,
|
||||
RouterModule
|
||||
],
|
||||
declarations: [
|
||||
],
|
||||
exports: [
|
||||
LoadingBarHttpClientModule,
|
||||
LoadingBarRouterModule
|
||||
],
|
||||
providers: [[{
|
||||
provide: HTTP_INTERCEPTORS,
|
||||
useClass: HttpAuthInterceptor,
|
||||
multi: true
|
||||
}]]
|
||||
})
|
||||
export class CoreModule {
|
||||
}
|
||||
36
bookie/src/app/core/error-logger.service.ts
Normal file
36
bookie/src/app/core/error-logger.service.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Observable} from 'rxjs/internal/Observable';
|
||||
import {throwError} from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ErrorLoggerService {
|
||||
constructor() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle Http operation that failed.
|
||||
* Let the app continue.
|
||||
* @param operation - name of the operation that failed
|
||||
* @param result - optional value to return as the observable result
|
||||
*/
|
||||
public handleError<T>(serviceName = 'error-logger', operation = 'operation', result?: T) {
|
||||
return (error: any): Observable<T> => {
|
||||
|
||||
// TODO: send the error to remote logging infrastructure
|
||||
console.error(error); // log to console instead
|
||||
|
||||
// TODO: better job of transforming error for user consumption
|
||||
this.log(serviceName, `${operation} failed: ${error.message}`);
|
||||
|
||||
// // Let the app keep running by returning an empty result.
|
||||
// return of(result as T);
|
||||
return throwError(error);
|
||||
};
|
||||
}
|
||||
|
||||
public log(serviceName = 'error-logger', message: string) {
|
||||
console.log(serviceName + 'Service: ' + message);
|
||||
}
|
||||
}
|
||||
42
bookie/src/app/core/http-auth-interceptor.ts
Normal file
42
bookie/src/app/core/http-auth-interceptor.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
|
||||
|
||||
import {Observable, throwError} from 'rxjs';
|
||||
import {catchError} from 'rxjs/operators';
|
||||
import {ToasterService} from './toaster.service';
|
||||
import {Router} from '@angular/router';
|
||||
import {ConfirmDialogComponent} from '../shared/confirm-dialog/confirm-dialog.component';
|
||||
import { MatDialog } from '@angular/material/dialog';
|
||||
|
||||
@Injectable()
|
||||
export class HttpAuthInterceptor implements HttpInterceptor {
|
||||
|
||||
constructor(private router: Router, private dialog: MatDialog, private toaster: ToasterService) {
|
||||
}
|
||||
|
||||
intercept(req: HttpRequest<any>, next: HttpHandler):
|
||||
Observable<HttpEvent<any>> {
|
||||
return next.handle(req)
|
||||
.pipe(
|
||||
catchError((error: any) => {
|
||||
if (error.status === 401) {
|
||||
this.toaster.show('Danger', 'User has been logged out');
|
||||
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
||||
width: '250px',
|
||||
data: {
|
||||
title: 'Logged out!',
|
||||
content: 'You have been logged out.\nYou can press Cancel to stay on page and login in another tab to resume here, or you can press Ok to navigate to the login page.'
|
||||
}
|
||||
});
|
||||
dialogRef.afterClosed().subscribe((result: boolean) => {
|
||||
if (result) {
|
||||
this.router.navigate(['login']);
|
||||
}
|
||||
});
|
||||
}
|
||||
return throwError(error);
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
17
bookie/src/app/core/toaster.service.ts
Normal file
17
bookie/src/app/core/toaster.service.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import { MatSnackBar } from '@angular/material/snack-bar';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ToasterService {
|
||||
|
||||
constructor(private snackBar: MatSnackBar) {
|
||||
}
|
||||
|
||||
show(type: string, msg: string) {
|
||||
this.snackBar.open(msg, type, {
|
||||
duration: 3000,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user