error handling in ui
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
import { Component, OnInit } from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
import { Hero } from '../hero';
|
import { Hero } from '../hero';
|
||||||
import { HeroService } from '../hero.service';
|
import { HeroService } from '../hero.service';
|
||||||
|
import { MessageService } from '../message.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-dashboard',
|
selector: 'app-dashboard',
|
||||||
@ -10,14 +11,18 @@ import { HeroService } from '../hero.service';
|
|||||||
export class DashboardComponent implements OnInit {
|
export class DashboardComponent implements OnInit {
|
||||||
heroes: Hero[] = [];
|
heroes: Hero[] = [];
|
||||||
|
|
||||||
constructor(private heroService: HeroService) { }
|
constructor(private heroService: HeroService, private messageService: MessageService) { }
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.getHeroes();
|
this.getHeroes();
|
||||||
}
|
}
|
||||||
|
|
||||||
async getHeroes(): Promise<void> {
|
async getHeroes(): Promise<void> {
|
||||||
this.heroes = await this.heroService.getHeroes()
|
try {
|
||||||
|
this.heroes = await this.heroService.getHeroes();
|
||||||
|
} catch (err) {
|
||||||
|
this.messageService.add(JSON.stringify(err, undefined, 4))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import { Location } from '@angular/common';
|
|||||||
|
|
||||||
import { Hero } from '../hero';
|
import { Hero } from '../hero';
|
||||||
import { HeroService } from '../hero.service';
|
import { HeroService } from '../hero.service';
|
||||||
|
import { MessageService } from '../message.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-hero-detail',
|
selector: 'app-hero-detail',
|
||||||
@ -14,22 +15,31 @@ export class HeroDetailComponent implements OnInit {
|
|||||||
|
|
||||||
@Input() hero: Hero;
|
@Input() hero: Hero;
|
||||||
|
|
||||||
constructor(private route: ActivatedRoute, private heroService: HeroService, private location: Location) { }
|
constructor(private route: ActivatedRoute, private heroService: HeroService, private location: Location, private messageService: MessageService) { }
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.getHero();
|
this.getHero();
|
||||||
}
|
}
|
||||||
|
|
||||||
async getHero(): Promise<void> {
|
async getHero(): Promise<void> {
|
||||||
const id = +this.route.snapshot.paramMap.get('id');
|
try {
|
||||||
this.hero = await this.heroService.getHero(id)
|
const id = +this.route.snapshot.paramMap.get('id');
|
||||||
|
this.hero = await this.heroService.getHero(id)
|
||||||
|
} catch (err) {
|
||||||
|
this.messageService.add(JSON.stringify(err, undefined, 4))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
goBack(): void {
|
goBack(): void {
|
||||||
this.location.back();
|
this.location.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
save(): void {
|
async save(): Promise<void> {
|
||||||
// this.heroService.updateHero(this.hero).subscribe(() => this.goBack());
|
try {
|
||||||
|
await this.heroService.updateHero(this.hero)
|
||||||
|
this.goBack()
|
||||||
|
} catch (err) {
|
||||||
|
this.messageService.add(JSON.stringify(err, undefined, 4))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,11 +18,11 @@ export class HeroService {
|
|||||||
|
|
||||||
getHero(id: number): Promise<Hero> {
|
getHero(id: number): Promise<Hero> {
|
||||||
this.messageService.add(`HeroService: fetch hero id=${id}`);
|
this.messageService.add(`HeroService: fetch hero id=${id}`);
|
||||||
return this.http.get<Hero>(`http://172.16.3.185:5000/hero?id=${id}`).toPromise()
|
return this.http.get<Hero>(`http://172.16.3.185:5000/hero/${id}`).toPromise()
|
||||||
}
|
}
|
||||||
|
|
||||||
updateHero(hero: Hero): Observable<any> {
|
updateHero(hero: Hero): Promise<any> {
|
||||||
this.messageService.add(`HeroService: save change: id=${hero.id}, name=${hero.name}`);
|
this.messageService.add(`HeroService: save change: id=${hero.id}, name=${hero.name}`);
|
||||||
return of(true);
|
return this.http.put<Hero>(`http://172.16.3.185:5000/hero/${hero.id}`, hero).toPromise()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user