]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/recommendations/recommended-videos.component.ts
Fix scrolling with hash in url
[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'
bee29df8 4import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
b0c36821 5import { RecommendationInfo } from '@app/shared/video/recommendation-info.model'
7f5f4152
BJ
6import { RecommendedVideosStore } from '@app/videos/recommendations/recommended-videos.store'
7import { User } from '@app/shared'
d816f3a0
RK
8import { AuthService, Notifier } from '@app/core'
9import { UserService } from '@app/shared/users/user.service'
5def76eb 10import { I18n } from '@ngx-translate/i18n-polyfill'
d3217560 11import { SessionStorageService } from '@app/shared/misc/storage.service'
c2caa99b 12import { MiniatureDisplayOptions } from '@app/shared/video/video-miniature.component'
7f5f4152
BJ
13
14@Component({
15 selector: 'my-recommended-videos',
d816f3a0
RK
16 templateUrl: './recommended-videos.component.html',
17 styleUrls: [ './recommended-videos.component.scss' ]
7f5f4152
BJ
18})
19export class RecommendedVideosComponent implements OnChanges {
b0c36821 20 @Input() inputRecommendation: RecommendationInfo
7f5f4152 21 @Input() user: User
bee29df8 22 @Input() playlist: VideoPlaylist
6aa54148 23 @Output() gotRecommendations = new EventEmitter<Video[]>()
7f5f4152 24
8adc5ddb 25 autoPlayNextVideo: boolean
5def76eb 26 autoPlayNextVideoTooltip: string
8adc5ddb 27
c2caa99b
RK
28 displayOptions: MiniatureDisplayOptions = {
29 date: true,
30 views: true,
31 by: true,
32 avatar: true
33 }
34
7f5f4152
BJ
35 readonly hasVideos$: Observable<boolean>
36 readonly videos$: Observable<Video[]>
37
38 constructor (
d816f3a0
RK
39 private userService: UserService,
40 private authService: AuthService,
41 private notifier: Notifier,
5def76eb 42 private i18n: I18n,
d3217560
RK
43 private store: RecommendedVideosStore,
44 private sessionStorageService: SessionStorageService
7f5f4152
BJ
45 ) {
46 this.videos$ = this.store.recommendations$
47 this.hasVideos$ = this.store.hasRecommendations$
6aa54148 48 this.videos$.subscribe(videos => this.gotRecommendations.emit(videos))
d816f3a0 49
d3217560
RK
50 if (this.authService.isLoggedIn()) {
51 this.autoPlayNextVideo = this.authService.getUser().autoPlayNextVideo
52 } else {
53 this.autoPlayNextVideo = this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' || false
54 this.sessionStorageService.watch([User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO]).subscribe(
55 () => this.autoPlayNextVideo = this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
56 )
57 }
5def76eb
RK
58
59 this.autoPlayNextVideoTooltip = this.i18n('When active, the next video is automatically played after the current one.')
7f5f4152
BJ
60 }
61
62 public ngOnChanges (): void {
b0c36821
J
63 if (this.inputRecommendation) {
64 this.store.requestNewRecommendations(this.inputRecommendation)
7f5f4152
BJ
65 }
66 }
67
0a57bbff
C
68 onVideoRemoved () {
69 this.store.requestNewRecommendations(this.inputRecommendation)
70 }
d816f3a0
RK
71
72 switchAutoPlayNextVideo () {
d3217560 73 this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString())
d816f3a0
RK
74
75 if (this.authService.isLoggedIn()) {
76 const details = {
77 autoPlayNextVideo: this.autoPlayNextVideo
78 }
79
80 this.userService.updateMyProfile(details).subscribe(
81 () => {
82 this.authService.refreshUserInformation()
83 },
84 err => this.notifier.error(err.message)
85 )
86 }
87 }
7f5f4152 88}