-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#7 Message in response to error in client
- Loading branch information
Giovanni Silva
committed
Apr 10, 2017
1 parent
2b00d95
commit e7ac3b6
Showing
5 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
}) | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} | ||
|
||
} |