]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/recommendations/recommended-videos.component.ts
autoplay next video switch for both user and visitors
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / recommendations / recommended-videos.component.ts
CommitLineData
6aa54148 1import { Component, Input, Output, OnChanges, EventEmitter } from '@angular/core'
7f5f4152
BJ
2import { Observable } from 'rxjs'
3import { Video } from '@app/shared/video/video.model'
b0c36821 4import { RecommendationInfo } from '@app/shared/video/recommendation-info.model'
7f5f4152
BJ
5import { RecommendedVideosStore } from '@app/videos/recommendations/recommended-videos.store'
6import { User } from '@app/shared'
d816f3a0
RK
7import { AuthService, Notifier } from '@app/core'
8import { UserService } from '@app/shared/users/user.service'
9import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
7f5f4152
BJ
10
11@Component({
12 selector: 'my-recommended-videos',
d816f3a0
RK
13 templateUrl: './recommended-videos.component.html',
14 styleUrls: [ './recommended-videos.component.scss' ]
7f5f4152
BJ
15})
16export class RecommendedVideosComponent implements OnChanges {
d816f3a0
RK
17 private static LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO = 'auto_play_next_video'
18
b0c36821 19 @Input() inputRecommendation: RecommendationInfo
7f5f4152 20 @Input() user: User
6aa54148 21 @Output() gotRecommendations = new EventEmitter<Video[]>()
7f5f4152
BJ
22
23 readonly hasVideos$: Observable<boolean>
24 readonly videos$: Observable<Video[]>
25
d816f3a0
RK
26 private autoPlayNextVideo: boolean
27
7f5f4152 28 constructor (
d816f3a0
RK
29 private userService: UserService,
30 private authService: AuthService,
31 private notifier: Notifier,
7f5f4152
BJ
32 private store: RecommendedVideosStore
33 ) {
34 this.videos$ = this.store.recommendations$
35 this.hasVideos$ = this.store.hasRecommendations$
6aa54148 36 this.videos$.subscribe(videos => this.gotRecommendations.emit(videos))
d816f3a0
RK
37
38 this.autoPlayNextVideo = this.authService.isLoggedIn()
39 ? this.authService.getUser().autoPlayNextVideo
40 : peertubeLocalStorage.getItem(RecommendedVideosComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' || false
7f5f4152
BJ
41 }
42
43 public ngOnChanges (): void {
b0c36821
J
44 if (this.inputRecommendation) {
45 this.store.requestNewRecommendations(this.inputRecommendation)
7f5f4152
BJ
46 }
47 }
48
0a57bbff
C
49 onVideoRemoved () {
50 this.store.requestNewRecommendations(this.inputRecommendation)
51 }
d816f3a0
RK
52
53 switchAutoPlayNextVideo () {
54 peertubeLocalStorage.setItem(RecommendedVideosComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString())
55
56 if (this.authService.isLoggedIn()) {
57 const details = {
58 autoPlayNextVideo: this.autoPlayNextVideo
59 }
60
61 this.userService.updateMyProfile(details).subscribe(
62 () => {
63 this.authService.refreshUserInformation()
64 },
65 err => this.notifier.error(err.message)
66 )
67 }
68 }
7f5f4152 69}