Add MatDialog and use messageService to display errors
This commit is contained in:
@@ -10,6 +10,7 @@ import { MatInputModule } from '@angular/material/input';
|
|||||||
import { MatButtonToggleModule } from '@angular/material/button-toggle';
|
import { MatButtonToggleModule } from '@angular/material/button-toggle';
|
||||||
import { MatCardModule } from '@angular/material/card';
|
import { MatCardModule } from '@angular/material/card';
|
||||||
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
||||||
|
import { MatDialogModule } from '@angular/material/dialog';
|
||||||
import { FlexLayoutModule } from '@angular/flex-layout';
|
import { FlexLayoutModule } from '@angular/flex-layout';
|
||||||
|
|
||||||
import { httpInterceptorProviders } from './http-interceptors';
|
import { httpInterceptorProviders } from './http-interceptors';
|
||||||
@@ -56,6 +57,7 @@ import { DialogComponent } from './dialog/dialog.component';
|
|||||||
MatButtonToggleModule,
|
MatButtonToggleModule,
|
||||||
MatCardModule,
|
MatCardModule,
|
||||||
MatProgressBarModule,
|
MatProgressBarModule,
|
||||||
|
MatDialogModule,
|
||||||
FlexLayoutModule,
|
FlexLayoutModule,
|
||||||
HttpClientModule,
|
HttpClientModule,
|
||||||
HttpClientXsrfModule.withOptions({
|
HttpClientXsrfModule.withOptions({
|
||||||
|
|||||||
@@ -11,6 +11,9 @@ import { StType } from '../type';
|
|||||||
import { FilterService } from '../services/filter.service';
|
import { FilterService } from '../services/filter.service';
|
||||||
import { ProgressService } from '../services/progress.service';
|
import { ProgressService } from '../services/progress.service';
|
||||||
import { MatProgressBar } from '@angular/material/progress-bar';
|
import { MatProgressBar } from '@angular/material/progress-bar';
|
||||||
|
import { MessageService } from '../services/message.service';
|
||||||
|
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
|
||||||
|
import { DialogComponent } from '../dialog/dialog.component';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-dashboard',
|
selector: 'app-dashboard',
|
||||||
@@ -56,11 +59,14 @@ export class DashboardComponent implements OnInit, AfterViewInit {
|
|||||||
deviceChart: StType = StType.Device;
|
deviceChart: StType = StType.Device;
|
||||||
progressValue: number = 0;
|
progressValue: number = 0;
|
||||||
isLoading = true;
|
isLoading = true;
|
||||||
|
private dialogRef: MatDialogRef<DialogComponent>;
|
||||||
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private systemConfigService: SystemConfigService,
|
private systemConfigService: SystemConfigService,
|
||||||
private progressService: ProgressService,
|
private progressService: ProgressService,
|
||||||
|
private messageService: MessageService,
|
||||||
|
public dialog: MatDialog
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
@@ -85,5 +91,20 @@ export class DashboardComponent implements OnInit, AfterViewInit {
|
|||||||
this.progressValue = this.progressService.percentValue;
|
this.progressValue = this.progressService.percentValue;
|
||||||
}, 100);
|
}, 100);
|
||||||
|
|
||||||
|
// Listen for messages from other services/components
|
||||||
|
this.messageService.messageAdded$
|
||||||
|
.subscribe(
|
||||||
|
_ => {
|
||||||
|
// Open dialog
|
||||||
|
if (!this.dialogRef)
|
||||||
|
this.dialogRef = this.dialog.open(DialogComponent);
|
||||||
|
|
||||||
|
this.dialogRef.afterClosed().subscribe(
|
||||||
|
_ => {
|
||||||
|
this.dialogRef = null;
|
||||||
|
this.messageService.clear();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
<h1 mat-dialog-title>Error</h1>
|
<h1 mat-dialog-title>Error</h1>
|
||||||
<div mat-dialog-content>
|
<div mat-dialog-content>
|
||||||
|
<ul>
|
||||||
|
<li *ngFor='let message of messageService.messages'> {{message}} </li>
|
||||||
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
@@ -15,7 +15,5 @@ export class DialogComponent implements OnInit {
|
|||||||
|
|
||||||
constructor(public messageService: MessageService) { }
|
constructor(public messageService: MessageService) { }
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void { }
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,11 +9,12 @@ import {
|
|||||||
import { Observable, throwError } from 'rxjs';
|
import { Observable, throwError } from 'rxjs';
|
||||||
import { apiRetry } from '../api-utils';
|
import { apiRetry } from '../api-utils';
|
||||||
import { retry, catchError } from 'rxjs/operators';
|
import { retry, catchError } from 'rxjs/operators';
|
||||||
|
import { MessageService } from '../services/message.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ErrorInterceptor implements HttpInterceptor {
|
export class ErrorInterceptor implements HttpInterceptor {
|
||||||
|
|
||||||
constructor() { }
|
constructor(private messageService: MessageService) { }
|
||||||
|
|
||||||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||||
return next.handle(request)
|
return next.handle(request)
|
||||||
@@ -29,6 +30,8 @@ export class ErrorInterceptor implements HttpInterceptor {
|
|||||||
errorMsg = `Error Status: ${error.status}\nMessage: ${error.message}`;
|
errorMsg = `Error Status: ${error.status}\nMessage: ${error.message}`;
|
||||||
}
|
}
|
||||||
console.log(errorMsg);
|
console.log(errorMsg);
|
||||||
|
|
||||||
|
this.messageService.add(errorMsg);
|
||||||
return throwError(errorMsg);
|
return throwError(errorMsg);
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,11 +1,17 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
|
import { Subject } from 'rxjs';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
export class MessageService {
|
export class MessageService {
|
||||||
messages: string[] = [];
|
messages: string[] = [];
|
||||||
|
private messageAddedSource = new Subject<string>();
|
||||||
|
messageAdded$ = this.messageAddedSource.asObservable();
|
||||||
|
|
||||||
add(message: string) {
|
add(message: string) {
|
||||||
this.messages.push(message);
|
this.messages.push(message);
|
||||||
|
this.messageAddedSource.next(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
clear() {
|
clear() {
|
||||||
|
|||||||
Reference in New Issue
Block a user