Retrieve Related Data Pipeline

PHOTO EMBED

Mon Dec 26 2022 19:15:03 GMT+0000 (Coordinated Universal Time)

Saved by @ilivanilton #angular #rxjs-pattern

// html
<div *ngFor"let post of postsForUser$ | async">
 {{ post.title }}
</div>

// component
postsForUser$ = this.entedUser$.pipe(
 switchMap(userName => this.userService.getUserId(userName)),
 switchMap(userId => this.userService.getPostsForUserId(userId))
)

// service
private _userSubject = new Subject<string>();
enteredUser$ = this._userSubject.asObservable();

userChanged(userName: string): void{
  this.enteredUser.next(userName);
}

getUserId(userName: string): Observable<number>{
  return this.http.get<User[]>(`${userUrl}?userName=^${userName}$`).pipe(
   catchError(this.handleError),
   map(users => (users.lenght === 0) ? 0 : users[0].id)
  )
}

getPostsForUserId(userId: number): Observable<Post[]>{
  return this.http.get<Post[]>(`${postsUresgatrl}?userId=^${userId}$`).pipe(
   catchError(this.handleError)
  )
}
content_copyCOPY

https://youtu.be/SPy35fOjRyU?t=133