Created
May 9, 2020 18:21
-
-
Save tkssharma/d24cd5befd7659e83f611c9656276468 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class UsersService { | |
constructor(private http: HttpClient, private postsService: PostsService) { } | |
private userSelectedAction = new Subject<number>(); | |
private usersUrl = 'https://jsonplaceholder.typicode.com/users'; | |
user$ = this.http.get<Users>(this.usersUrl).pipe(shareReplay()); | |
userWithPost$ = combineLatest( | |
this.user$, | |
this.postsService.post$ | |
).pipe( map( ([users, posts]) => { | |
return users.map( user => ({ | |
...user, | |
posts: posts.filter( post => post.userId == user.id) | |
})) | |
})); | |
selectedUser$ = combineLatest( | |
this.userSelectedAction, | |
this.userWithPost$ | |
).pipe( | |
map( ([selectedUserId, users]) => users.find( user => user.id == selectedUserId)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment