This is my first time experimenting with BehaviorSubject, async pipes and concatMap so I'm facing some problems updating data in my DOM.
I have:
private profilesStore = new BehaviorSubject<any>(null);
profiles$ = this.profilesStore.asObservable();
getUserProfiles(): Observable<Profile[]> {
const headers = this.authService.getHeaders();
return this.http.get<any>(`${API_URL}/profile`, { headers: headers })
.pipe(
catchError(err => throwError(err)),
tap(res => this.profilesStore.next(res)),
shareReplay()
);
}
and then
addProduct(profileId: any) {
const headers = this.authService.getHeaders();
return this.http.post<any>(`${apiUrl}/products/${profileId}`, {}, { headers: headers })
.pipe(
catchError(err => throwError(err)),
concatMap(() => this.profileService.profiles$),
map(profiles => {
const selectedProfile = profiles.findIndex(profile => profile.id === profileId);
profiles[selectedProfile].canEdit = true;
return profiles;
})
);
}
This logic it's like a cart logic. I add a product to one of the profiles, so to avoid calling again the api (getUserProfiles) I modify profiles$ stream and add the property that I want (in this case canEdit) but the problem comes when I delete the product from the cart and recover data from getUserProfiles() I understand that as I use concatMap with profiles$ I get the side effect on addProduct() even if I haven't called that function, my question is...
Why does it continue performing
map(profiles => {
const selectedProfile = profiles.findIndex(profile => profile.id === profileId);
profiles[selectedProfile].canEdit = true;
return profiles;
})
with the old profileId that i passed in the past as parameters, even if i haven't called addProduct() function and how to avoid that?