Add visitor settings, rework logged-in dropdown (#2514)
[oweals/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 { I18n } from '@ngx-translate/i18n-polyfill'
11 import { SessionStorageService } from '@app/shared/misc/storage.service'
12
13 @Component({
14   selector: 'my-recommended-videos',
15   templateUrl: './recommended-videos.component.html',
16   styleUrls: [ './recommended-videos.component.scss' ]
17 })
18 export class RecommendedVideosComponent implements OnChanges {
19   @Input() inputRecommendation: RecommendationInfo
20   @Input() user: User
21   @Input() playlist: VideoPlaylist
22   @Output() gotRecommendations = new EventEmitter<Video[]>()
23
24   autoPlayNextVideo: boolean
25   autoPlayNextVideoTooltip: string
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 i18n: I18n,
35     private store: RecommendedVideosStore,
36     private sessionStorageService: SessionStorageService
37   ) {
38     this.videos$ = this.store.recommendations$
39     this.hasVideos$ = this.store.hasRecommendations$
40     this.videos$.subscribe(videos => this.gotRecommendations.emit(videos))
41
42     if (this.authService.isLoggedIn()) {
43       this.autoPlayNextVideo = this.authService.getUser().autoPlayNextVideo
44     } else {
45       this.autoPlayNextVideo = this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' || false
46       this.sessionStorageService.watch([User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO]).subscribe(
47         () => this.autoPlayNextVideo = this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
48       )
49     }
50
51     this.autoPlayNextVideoTooltip = this.i18n('When active, the next video is automatically played after the current one.')
52   }
53
54   public ngOnChanges (): void {
55     if (this.inputRecommendation) {
56       this.store.requestNewRecommendations(this.inputRecommendation)
57     }
58   }
59
60   onVideoRemoved () {
61     this.store.requestNewRecommendations(this.inputRecommendation)
62   }
63
64   switchAutoPlayNextVideo () {
65     this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString())
66
67     if (this.authService.isLoggedIn()) {
68       const details = {
69         autoPlayNextVideo: this.autoPlayNextVideo
70       }
71
72       this.userService.updateMyProfile(details).subscribe(
73         () => {
74           this.authService.refreshUserInformation()
75         },
76         err => this.notifier.error(err.message)
77       )
78     }
79   }
80 }