add Delete a hero

This commit is contained in:
Костя 2025-06-24 13:08:02 +03:00
parent 973524fc7e
commit 5f12d19dbc
4 changed files with 53 additions and 0 deletions

View File

@ -56,6 +56,16 @@ export class HeroService {
); );
} }
/** DELETE: delete the hero from the server */
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'))
);
}
/** /**
* Handle Http operation that failed. * Handle Http operation that failed.
* Let the app continue. * Let the app continue.

View File

@ -5,6 +5,15 @@
padding: 0; padding: 0;
width: 15em; width: 15em;
} }
input {
display: block;
width: 100%;
padding: .5rem;
margin: 1rem 0;
box-sizing: border-box;
}
.heroes li { .heroes li {
position: relative; position: relative;
cursor: pointer; cursor: pointer;
@ -51,4 +60,31 @@
text-align: right; text-align: right;
margin-right: .8em; margin-right: .8em;
border-radius: 4px 0 0 4px; border-radius: 4px 0 0 4px;
}
.add-button {
padding: .5rem 1.5rem;
font-size: 1rem;
margin-bottom: 2rem;
}
.add-button:hover {
color: white;
background-color: #42545C;
}
button.delete {
position: absolute;
left: 210px;
top: 5px;
background-color: white;
color: #525252;
font-size: 1.1rem;
margin: 0;
padding: 1px 10px 3px 10px;
}
button.delete:hover {
background-color: #525252;
color: white;
} }

View File

@ -4,6 +4,8 @@
<a routerLink="/detail/{{hero.id}}"> <a routerLink="/detail/{{hero.id}}">
<span class="badge">{{hero.id}}</span> {{hero.name}} <span class="badge">{{hero.id}}</span> {{hero.name}}
</a> </a>
<button type="button" class="delete" title="delete hero"
(click)="delete(hero)">x</button>
</li> </li>
</ul> </ul>

View File

@ -39,4 +39,9 @@ export class HeroesComponent implements OnInit {
}); });
} }
delete(hero: Hero): void {
this.heroes = this.heroes.filter(h => h !== hero);
this.heroService.deleteHero(hero.id).subscribe();
}
} }