]>
Commit | Line | Data |
---|---|---|
6aa54148 | 1 | import { Component, Input, Output, OnChanges, EventEmitter } from '@angular/core' |
7f5f4152 BJ |
2 | import { Observable } from 'rxjs' |
3 | import { Video } from '@app/shared/video/video.model' | |
bee29df8 | 4 | import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model' |
b0c36821 | 5 | import { RecommendationInfo } from '@app/shared/video/recommendation-info.model' |
7f5f4152 BJ |
6 | import { RecommendedVideosStore } from '@app/videos/recommendations/recommended-videos.store' |
7 | import { User } from '@app/shared' | |
d816f3a0 RK |
8 | import { AuthService, Notifier } from '@app/core' |
9 | import { UserService } from '@app/shared/users/user.service' | |
88a7f93f | 10 | import { peertubeSessionStorage } from '@app/shared/misc/peertube-web-storage' |
5def76eb | 11 | import { I18n } from '@ngx-translate/i18n-polyfill' |
7f5f4152 BJ |
12 | |
13 | @Component({ | |
14 | selector: 'my-recommended-videos', | |
d816f3a0 RK |
15 | templateUrl: './recommended-videos.component.html', |
16 | styleUrls: [ './recommended-videos.component.scss' ] | |
7f5f4152 BJ |
17 | }) |
18 | export class RecommendedVideosComponent implements OnChanges { | |
88a7f93f | 19 | static SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO = 'auto_play_next_video' |
d816f3a0 | 20 | |
b0c36821 | 21 | @Input() inputRecommendation: RecommendationInfo |
7f5f4152 | 22 | @Input() user: User |
bee29df8 | 23 | @Input() playlist: VideoPlaylist |
6aa54148 | 24 | @Output() gotRecommendations = new EventEmitter<Video[]>() |
7f5f4152 | 25 | |
8adc5ddb | 26 | autoPlayNextVideo: boolean |
5def76eb | 27 | autoPlayNextVideoTooltip: string |
8adc5ddb | 28 | |
7f5f4152 BJ |
29 | readonly hasVideos$: Observable<boolean> |
30 | readonly videos$: Observable<Video[]> | |
31 | ||
32 | constructor ( | |
d816f3a0 RK |
33 | private userService: UserService, |
34 | private authService: AuthService, | |
35 | private notifier: Notifier, | |
5def76eb | 36 | private i18n: I18n, |
7f5f4152 BJ |
37 | private store: RecommendedVideosStore |
38 | ) { | |
39 | this.videos$ = this.store.recommendations$ | |
40 | this.hasVideos$ = this.store.hasRecommendations$ | |
6aa54148 | 41 | this.videos$.subscribe(videos => this.gotRecommendations.emit(videos)) |
d816f3a0 RK |
42 | |
43 | this.autoPlayNextVideo = this.authService.isLoggedIn() | |
44 | ? this.authService.getUser().autoPlayNextVideo | |
88a7f93f | 45 | : peertubeSessionStorage.getItem(RecommendedVideosComponent.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' || false |
5def76eb RK |
46 | |
47 | this.autoPlayNextVideoTooltip = this.i18n('When active, the next video is automatically played after the current one.') | |
7f5f4152 BJ |
48 | } |
49 | ||
50 | public ngOnChanges (): void { | |
b0c36821 J |
51 | if (this.inputRecommendation) { |
52 | this.store.requestNewRecommendations(this.inputRecommendation) | |
7f5f4152 BJ |
53 | } |
54 | } | |
55 | ||
0a57bbff C |
56 | onVideoRemoved () { |
57 | this.store.requestNewRecommendations(this.inputRecommendation) | |
58 | } | |
d816f3a0 RK |
59 | |
60 | switchAutoPlayNextVideo () { | |
88a7f93f | 61 | peertubeSessionStorage.setItem(RecommendedVideosComponent.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString()) |
d816f3a0 RK |
62 | |
63 | if (this.authService.isLoggedIn()) { | |
64 | const details = { | |
65 | autoPlayNextVideo: this.autoPlayNextVideo | |
66 | } | |
67 | ||
68 | this.userService.updateMyProfile(details).subscribe( | |
69 | () => { | |
70 | this.authService.refreshUserInformation() | |
71 | }, | |
72 | err => this.notifier.error(err.message) | |
73 | ) | |
74 | } | |
75 | } | |
7f5f4152 | 76 | } |