]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-watch/shared/recommendations/recommended-videos.component.ts
Correctly unsubscribe on menu destroy
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / shared / recommendations / recommended-videos.component.ts
CommitLineData
6a7cea15
C
1import { Observable, startWith, Subscription, switchMap } from 'rxjs'
2import { Component, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output } from '@angular/core'
a9bfa85d 3import { AuthService, Notifier, User, UserService } from '@app/core'
67ed6552
C
4import { Video } from '@app/shared/shared-main'
5import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature'
6import { VideoPlaylist } from '@app/shared/shared-video-playlist'
67ed6552
C
7import { RecommendationInfo } from './recommendation-info.model'
8import { RecommendedVideosStore } from './recommended-videos.store'
7f5f4152
BJ
9
10@Component({
11 selector: 'my-recommended-videos',
d816f3a0
RK
12 templateUrl: './recommended-videos.component.html',
13 styleUrls: [ './recommended-videos.component.scss' ]
7f5f4152 14})
6a7cea15 15export class RecommendedVideosComponent implements OnInit, OnChanges, OnDestroy {
b0c36821 16 @Input() inputRecommendation: RecommendationInfo
bee29df8 17 @Input() playlist: VideoPlaylist
0f7407d9
C
18 @Input() displayAsRow: boolean
19
6aa54148 20 @Output() gotRecommendations = new EventEmitter<Video[]>()
7f5f4152 21
8adc5ddb 22 autoPlayNextVideo: boolean
5def76eb 23 autoPlayNextVideoTooltip: string
8adc5ddb 24
c2caa99b
RK
25 displayOptions: MiniatureDisplayOptions = {
26 date: true,
27 views: true,
28 by: true,
29 avatar: true
30 }
31
6a7cea15
C
32 user: User
33
34 private userSub: Subscription
5c20a455 35
7f5f4152
BJ
36 readonly hasVideos$: Observable<boolean>
37 readonly videos$: Observable<Video[]>
38
39 constructor (
d816f3a0
RK
40 private userService: UserService,
41 private authService: AuthService,
42 private notifier: Notifier,
a9bfa85d 43 private store: RecommendedVideosStore
7f5f4152
BJ
44 ) {
45 this.videos$ = this.store.recommendations$
46 this.hasVideos$ = this.store.hasRecommendations$
6aa54148 47 this.videos$.subscribe(videos => this.gotRecommendations.emit(videos))
d816f3a0 48
66357162 49 this.autoPlayNextVideoTooltip = $localize`When active, the next video is automatically played after the current one.`
7f5f4152
BJ
50 }
51
5c20a455 52 ngOnInit () {
6a7cea15
C
53 this.userSub = this.userService.listenAnonymousUpdate()
54 .pipe(
55 startWith(true),
56 switchMap(() => this.userService.getAnonymousOrLoggedUser())
57 )
58 .subscribe(user => {
59 this.user = user
60 this.autoPlayNextVideo = user.autoPlayNextVideo
6a7cea15 61 })
5c20a455
C
62 }
63
64 ngOnChanges () {
b0c36821
J
65 if (this.inputRecommendation) {
66 this.store.requestNewRecommendations(this.inputRecommendation)
7f5f4152
BJ
67 }
68 }
69
72c98d18 70 ngOnDestroy () {
6a7cea15
C
71 if (this.userSub) this.userSub.unsubscribe()
72 }
73
0a57bbff
C
74 onVideoRemoved () {
75 this.store.requestNewRecommendations(this.inputRecommendation)
76 }
d816f3a0
RK
77
78 switchAutoPlayNextVideo () {
a9bfa85d 79 const details = { autoPlayNextVideo: this.autoPlayNextVideo }
d816f3a0
RK
80
81 if (this.authService.isLoggedIn()) {
1378c0d3
C
82 this.userService.updateMyProfile(details)
83 .subscribe({
84 next: () => {
85 this.authService.refreshUserInformation()
86 },
87
88 error: err => this.notifier.error(err.message)
89 })
a9bfa85d
C
90 } else {
91 this.userService.updateMyAnonymousProfile(details)
d816f3a0
RK
92 }
93 }
7f5f4152 94}