
committed by
Kara Erickson

parent
e8d0265c1e
commit
02d98ed823
@ -6,33 +6,35 @@ import { HeroesService } from './heroes.service';
|
||||
@Component({
|
||||
selector: 'app-heroes',
|
||||
templateUrl: './heroes.component.html',
|
||||
providers: [ HeroesService ],
|
||||
providers: [HeroesService],
|
||||
styleUrls: ['./heroes.component.css']
|
||||
})
|
||||
export class HeroesComponent implements OnInit {
|
||||
heroes: Hero[];
|
||||
editHero: Hero; // the hero currently being edited
|
||||
|
||||
constructor(private heroesService: HeroesService) { }
|
||||
constructor(private heroesService: HeroesService) {}
|
||||
|
||||
ngOnInit() {
|
||||
this.getHeroes();
|
||||
}
|
||||
|
||||
getHeroes(): void {
|
||||
this.heroesService.getHeroes()
|
||||
.subscribe(heroes => this.heroes = heroes);
|
||||
this.heroesService.getHeroes().subscribe(heroes => (this.heroes = heroes));
|
||||
}
|
||||
|
||||
add(name: string): void {
|
||||
this.editHero = undefined;
|
||||
name = name.trim();
|
||||
if (!name) { return; }
|
||||
if (!name) {
|
||||
return;
|
||||
}
|
||||
|
||||
// The server will generate the id for this new hero
|
||||
const newHero: Hero = { name } as Hero;
|
||||
// #docregion add-hero-subscribe
|
||||
this.heroesService.addHero(newHero)
|
||||
this.heroesService
|
||||
.addHero(newHero)
|
||||
.subscribe(hero => this.heroes.push(hero));
|
||||
// #enddocregion add-hero-subscribe
|
||||
}
|
||||
@ -50,26 +52,28 @@ export class HeroesComponent implements OnInit {
|
||||
*/
|
||||
}
|
||||
|
||||
edit(hero) {
|
||||
edit(hero: Hero) {
|
||||
this.editHero = hero;
|
||||
}
|
||||
|
||||
search(searchTerm: string) {
|
||||
this.editHero = undefined;
|
||||
if (searchTerm) {
|
||||
this.heroesService.searchHeroes(searchTerm)
|
||||
.subscribe(heroes => this.heroes = heroes);
|
||||
this.heroesService
|
||||
.searchHeroes(searchTerm)
|
||||
.subscribe(heroes => (this.heroes = heroes));
|
||||
}
|
||||
}
|
||||
|
||||
update() {
|
||||
if (this.editHero) {
|
||||
this.heroesService.updateHero(this.editHero)
|
||||
.subscribe(hero => {
|
||||
// replace the hero in the heroes list with update from server
|
||||
const ix = hero ? this.heroes.findIndex(h => h.id === hero.id) : -1;
|
||||
if (ix > -1) { this.heroes[ix] = hero; }
|
||||
});
|
||||
this.heroesService.updateHero(this.editHero).subscribe(hero => {
|
||||
// replace the hero in the heroes list with update from server
|
||||
const ix = hero ? this.heroes.findIndex(h => h.id === hero.id) : -1;
|
||||
if (ix > -1) {
|
||||
this.heroes[ix] = hero;
|
||||
}
|
||||
});
|
||||
this.editHero = undefined;
|
||||
}
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ export class PackageSearchService {
|
||||
// TODO: Add error handling
|
||||
return this.http.get(searchUrl, options).pipe(
|
||||
map((data: any) => {
|
||||
return data.results.map(entry => ({
|
||||
return data.results.map((entry: any) => ({
|
||||
name: entry.name[0],
|
||||
version: entry.version[0],
|
||||
description: entry.description[0]
|
||||
|
Reference in New Issue
Block a user