Skip to content

[Docs][Tutorial] unused complexity on hero deletion #41233

Closed

Description

@geromegrignon

📚 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?**

https://angular.io/tutorial/toh-pt6#delete-a-hero

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

Activity

  1. modified the milestone: needsTriage on Mar 16, 2021
  2. added
    open for contributions
    P4
    on Mar 18, 2021
  3. modified the milestones: needsTriage, Backlog on Mar 18, 2021
  4. added a commit that references this issue on Mar 21, 2021
    5dd7ad8
  5. added a commit that references this issue on Mar 22, 2021
    74b2d4a
  6. added a commit that references this issue on Apr 5, 2021
    df1ddd5
  7. angular-automatic-lock-bot commented on Apr 22, 2021

    @angular-automatic-lock-bot

    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.

  8. locked and limited conversation to collaborators on Apr 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P4open for contributions

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions