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