Repository navigation
Closed
Labels
P4A relatively minor issue that is not relevant to core functionsopen for contributionsAn issue that is suitable for a community contributor (based on its complexity/scope).
Milestone
Description
📚 Docs or angular.io bug report
Description
On the last section of the Tour of Heroes tutorial, a feature is added to delete an existing hero.
Considering the provided function into the HeroService :
/** DELETE: delete the hero from the server */
deleteHero(hero: Hero | number): Observable<Hero> {
const id = typeof hero === 'number' ? hero : hero.id;
const url = `${this.heroesUrl}/${id}`;
return this.http.delete<Hero>(url, this.httpOptions).pipe(
tap(_ => this.log(`deleted hero id=${id}`)),
catchError(this.handleError<Hero>('deleteHero'))
);
}It only used once into the app, into the HeroComponent :
delete(hero: Hero): void {
this.heroes = this.heroes.filter(h => h !== hero);
this.heroService.deleteHero(hero).subscribe();
}As the provided argument is always an Hero, there is no explicit reason the parameter is of type Hero | number into the HeroService. It might lead to confusion.
As the deleteHero() from the HeroService just need the hero id, i would suggest refactoring the current code to :
// HeroComponent
delete(hero: Hero): void {
this.heroes = this.heroes.filter(h => h !== hero);
this.heroService.deleteHero(hero.id).subscribe();
}// HeroService
deleteHero(id: number): Observable<Hero> {
const url = `${this.heroesUrl}/${id}`;
return this.http.delete<Hero>(url, this.httpOptions).pipe(
tap(_ => this.log(`deleted hero id=${id}`)),
catchError(this.handleError<Hero>('deleteHero'))
);
}🔬 Minimal Reproduction
What's the affected URL?**
Reproduction Steps**
Add the deletion feature to the Tour of Heroes tutorial app
Expected vs Actual Behavior**
the deleteHero() function from the HeroService expects an argument of type Hero | number` while it never pass a number in the existing code.
Anything else relevant?
I can open a PR if the suggestion is accepted
Reacted by George Kalpakas
Metadata
Metadata
Assignees
Labels
P4A relatively minor issue that is not relevant to core functionsopen for contributionsAn issue that is suitable for a community contributor (based on its complexity/scope).
Activity
docs: simplify deleteHero method in tutorial
docs: simplify deleteHero method in tutorial (#41303)
docs: simplify deleteHero method in tutorial (angular#41303)
angular-automatic-lock-bot commentedon Apr 22, 2021
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.