1 import { Observable } from 'rxjs'
2 import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core'
3 import { AuthService, Notifier, SessionStorageService, User, UserService } from '@app/core'
4 import { Video } from '@app/shared/shared-main'
5 import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature'
6 import { VideoPlaylist } from '@app/shared/shared-video-playlist'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { RecommendationInfo } from './recommendation-info.model'
9 import { RecommendedVideosStore } from './recommended-videos.store'
12 selector: 'my-recommended-videos',
13 templateUrl: './recommended-videos.component.html',
14 styleUrls: [ './recommended-videos.component.scss' ]
16 export class RecommendedVideosComponent implements OnInit, OnChanges {
17 @Input() inputRecommendation: RecommendationInfo
18 @Input() playlist: VideoPlaylist
19 @Output() gotRecommendations = new EventEmitter<Video[]>()
21 autoPlayNextVideo: boolean
22 autoPlayNextVideoTooltip: string
24 displayOptions: MiniatureDisplayOptions = {
33 readonly hasVideos$: Observable<boolean>
34 readonly videos$: Observable<Video[]>
37 private userService: UserService,
38 private authService: AuthService,
39 private notifier: Notifier,
41 private store: RecommendedVideosStore,
42 private sessionStorageService: SessionStorageService
44 this.videos$ = this.store.recommendations$
45 this.hasVideos$ = this.store.hasRecommendations$
46 this.videos$.subscribe(videos => this.gotRecommendations.emit(videos))
48 if (this.authService.isLoggedIn()) {
49 this.autoPlayNextVideo = this.authService.getUser().autoPlayNextVideo
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'
57 this.autoPlayNextVideoTooltip = this.i18n('When active, the next video is automatically played after the current one.')
61 this.userService.getAnonymousOrLoggedUser()
62 .subscribe(user => this.userMiniature = user)
66 if (this.inputRecommendation) {
67 this.store.requestNewRecommendations(this.inputRecommendation)
72 this.store.requestNewRecommendations(this.inputRecommendation)
75 switchAutoPlayNextVideo () {
76 this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString())
78 if (this.authService.isLoggedIn()) {
80 autoPlayNextVideo: this.autoPlayNextVideo
83 this.userService.updateMyProfile(details).subscribe(
85 this.authService.refreshUserInformation()
87 err => this.notifier.error(err.message)