]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/recommendations/recommended-videos.store.ts
Refactor: Separated "Other Videos" section into a dedicated component/service (#969)
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / recommendations / recommended-videos.store.ts
1 import { Inject, Injectable } from '@angular/core'
2 import { Observable, ReplaySubject } from 'rxjs'
3 import { Video } from '@app/shared/video/video.model'
4 import { RecentVideosRecommendationService } from '@app/videos/recommendations/recent-videos-recommendation.service'
5 import { RecommendationService, UUID } from '@app/videos/recommendations/recommendations.service'
6 import { map, switchMap, take } from 'rxjs/operators'
7
8 /**
9 * This store is intended to provide data for the RecommendedVideosComponent.
10 */
11 @Injectable()
12 export class RecommendedVideosStore {
13 public readonly recommendations$: Observable<Video[]>
14 public readonly hasRecommendations$: Observable<boolean>
15 private readonly requestsForLoad$$ = new ReplaySubject<UUID>(1)
16
17 constructor (
18 @Inject(RecentVideosRecommendationService) private recommendations: RecommendationService
19 ) {
20 this.recommendations$ = this.requestsForLoad$$.pipe(
21 switchMap(requestedUUID => recommendations.getRecommendations(requestedUUID)
22 .pipe(take(1))
23 ))
24 this.hasRecommendations$ = this.recommendations$.pipe(
25 map(otherVideos => otherVideos.length > 0)
26 )
27 }
28
29 requestNewRecommendations (videoUUID: string) {
30 this.requestsForLoad$$.next(videoUUID)
31 }
32 }