I'm sending a request to the server to modify one of the users.
As a response, the server sends back the updated user.
After that I'm trying to update the BehaviorSubject of all the users like this:
private currentHeroes = new BehaviorSubject<Hero[]>(null);
currentHeroes$ = this.currentHeroes.asObservable();
powerUp(id: number) {
return this.http
.post<Hero>(environment.apiUrl + 'heroes/powerUp/' + id, {})
.pipe(
tap((updatedHero: Hero) => {
this.currentHeroes.next(
this.currentHeroes.value.map((hero: Hero) =>
hero.id === updatedHero.id ? updatedHero : hero
).sort(a=>a.currentPower)
);
})
);
}
In case of deleting:
delete(id: number) {
return this.http.delete<Hero>(environment.apiUrl + 'heroes/' + id).pipe(
tap((deletedHero: Hero) => {
this.currentHeroes.value.filter(
(hero: Hero) => hero.id !== deletedHero.id
);
})
);
}
I'm trying to modify the array of users. Replace the old user with the updated user.
And the code doesn't reach the pipe of the currentHeroes$. I'm not really sure why is that happening or how can I fix it.
I have tried to return a value from the pipe or use tap instead of map but no luck.
Does any can maybe take a look to explain to me why does it happen? or how can I fix it?
Thanks!