Skip to content

Commit

Permalink
#7 Message in response to error in client
Browse files Browse the repository at this point in the history
  • Loading branch information
Giovanni Silva committed Apr 10, 2017
1 parent 2b00d95 commit e7ac3b6
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
1 change: 1 addition & 0 deletions client/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<p-growl [value]="msgs"></p-growl>
<app-toolbar></app-toolbar>
<app-navbar></app-navbar>
<router-outlet></router-outlet>
36 changes: 33 additions & 3 deletions client/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
import { Component } from '@angular/core';
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit} from "@angular/core";
import {Message} from "primeng/primeng";
import {AppService} from "./app.service";

@Component({
selector: 'app-root',
templateUrl: './app.component.html'
templateUrl: './app.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class AppComponent {
export class AppComponent implements OnInit {

msgs: Message[] = []

constructor(private appService: AppService,
private changeDetectionRef: ChangeDetectorRef) {
}

ngOnInit() {
this.appService.events.message.subscribe(e => {

// For some reason the change detection for the first item in array doesn't work
// Create a copy of array then empty array, trigger the detection and then put the previous copy
// + new message and detect again :-)
let previous = this.msgs.slice()
this.msgs = []
setTimeout(() => {
previous.push(e)
this.msgs = previous
this.changeDetectionRef.detectChanges()
})

this.changeDetectionRef.detectChanges()

})

}

}
11 changes: 11 additions & 0 deletions client/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import { SharedModule } from "./shared/shared.module";
import { AuthModule, InitOptions } from 'angular-spa';
import { HomeModule } from "./+home/home.module";
import { SearchModule } from "./+search/search.module";
import {GrowlModule} from 'primeng/primeng';

import { environment } from '../environments/environment';
import {Interceptor} from 'angular-http-interceptor'
import {HttpStatusErrorInterceptor} from "./shared/interceptors/http-status-error";
import {AppService} from "./app.service";


@NgModule({
Expand All @@ -24,10 +28,17 @@ import { environment } from '../environments/environment';
HttpModule,
routing,
HomeModule,
GrowlModule,
SearchModule
],
providers: [
appRoutingProviders,
AppService,
{
provide: Interceptor,
useClass: HttpStatusErrorInterceptor,
multi: true
},
{
provide: InitOptions,
useValue: {
Expand Down
11 changes: 11 additions & 0 deletions client/src/app/app.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Injectable} from "@angular/core";
import {Message} from "primeng/primeng";

import {Subject} from "rxjs";

@Injectable()
export class AppService {
events = {
message: new Subject<Message>()
}
}
25 changes: 25 additions & 0 deletions client/src/app/shared/interceptors/http-status-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {Interceptor} from 'angular-http-interceptor'
import {Injectable} from "@angular/core";
import {Request, Response} from "@angular/http"
import {Observable} from "rxjs/Observable";

import "rxjs/add/observable/of"
import {AppService} from "../../app.service";

@Injectable()
export class HttpStatusErrorInterceptor implements Interceptor {
constructor(private appService: AppService) {}
before(request: Request): Observable<any> {
return Observable.of(request);
}

after(response: Response): void {

}

error(err: Response): void {
const severity = err.status == 404 ? 'info': 'error'
this.appService.events.message.next({summary: err.statusText, detail: err.text(), severity: severity})
}

}

0 comments on commit e7ac3b6

Please sign in to comment.