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