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 { RecommendationInfo } from '@app/shared/video/recommendation-info.model'
5 import { RecommendedVideosStore } from '@app/videos/recommendations/recommended-videos.store'
6 import { User } from '@app/shared'
7 import { AuthService, Notifier } from '@app/core'
8 import { UserService } from '@app/shared/users/user.service'
9 import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
12 selector: 'my-recommended-videos',
13 templateUrl: './recommended-videos.component.html',
14 styleUrls: [ './recommended-videos.component.scss' ]
16 export class RecommendedVideosComponent implements OnChanges {
17 private static LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO = 'auto_play_next_video'
19 @Input() inputRecommendation: RecommendationInfo
21 @Output() gotRecommendations = new EventEmitter<Video[]>()
23 readonly hasVideos$: Observable<boolean>
24 readonly videos$: Observable<Video[]>
26 private autoPlayNextVideo: boolean
29 private userService: UserService,
30 private authService: AuthService,
31 private notifier: Notifier,
32 private store: RecommendedVideosStore
34 this.videos$ = this.store.recommendations$
35 this.hasVideos$ = this.store.hasRecommendations$
36 this.videos$.subscribe(videos => this.gotRecommendations.emit(videos))
38 this.autoPlayNextVideo = this.authService.isLoggedIn()
39 ? this.authService.getUser().autoPlayNextVideo
40 : peertubeLocalStorage.getItem(RecommendedVideosComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' || false
43 public ngOnChanges (): void {
44 if (this.inputRecommendation) {
45 this.store.requestNewRecommendations(this.inputRecommendation)
50 this.store.requestNewRecommendations(this.inputRecommendation)
53 switchAutoPlayNextVideo () {
54 peertubeLocalStorage.setItem(RecommendedVideosComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString())
56 if (this.authService.isLoggedIn()) {
58 autoPlayNextVideo: this.autoPlayNextVideo
61 this.userService.updateMyProfile(details).subscribe(
63 this.authService.refreshUserInformation()
65 err => this.notifier.error(err.message)