]>
Commit | Line | Data |
---|---|---|
7f5f4152 | 1 | import { Observable } from 'rxjs' |
5c20a455 | 2 | import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core' |
67ed6552 C |
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' | |
66357162 | 7 | import { UserLocalStorageKeys } from '@root-helpers/users' |
67ed6552 C |
8 | import { RecommendationInfo } from './recommendation-info.model' |
9 | import { 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 | 16 | export 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, | |
d3217560 RK |
40 | private store: RecommendedVideosStore, |
41 | private sessionStorageService: SessionStorageService | |
7f5f4152 BJ |
42 | ) { |
43 | this.videos$ = this.store.recommendations$ | |
44 | this.hasVideos$ = this.store.hasRecommendations$ | |
6aa54148 | 45 | this.videos$.subscribe(videos => this.gotRecommendations.emit(videos)) |
d816f3a0 | 46 | |
d3217560 RK |
47 | if (this.authService.isLoggedIn()) { |
48 | this.autoPlayNextVideo = this.authService.getUser().autoPlayNextVideo | |
49 | } else { | |
a4ff3100 C |
50 | this.autoPlayNextVideo = this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' |
51 | ||
52 | this.sessionStorageService.watch([UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO]).subscribe( | |
53 | () => { | |
54 | this.autoPlayNextVideo = this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' | |
55 | } | |
d3217560 RK |
56 | ) |
57 | } | |
5def76eb | 58 | |
66357162 | 59 | this.autoPlayNextVideoTooltip = $localize`When active, the next video is automatically played after the current one.` |
7f5f4152 BJ |
60 | } |
61 | ||
5c20a455 C |
62 | ngOnInit () { |
63 | this.userService.getAnonymousOrLoggedUser() | |
64 | .subscribe(user => this.userMiniature = user) | |
65 | } | |
66 | ||
67 | ngOnChanges () { | |
b0c36821 J |
68 | if (this.inputRecommendation) { |
69 | this.store.requestNewRecommendations(this.inputRecommendation) | |
7f5f4152 BJ |
70 | } |
71 | } | |
72 | ||
0a57bbff C |
73 | onVideoRemoved () { |
74 | this.store.requestNewRecommendations(this.inputRecommendation) | |
75 | } | |
d816f3a0 RK |
76 | |
77 | switchAutoPlayNextVideo () { | |
a4ff3100 | 78 | this.sessionStorageService.setItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString()) |
d816f3a0 RK |
79 | |
80 | if (this.authService.isLoggedIn()) { | |
81 | const details = { | |
82 | autoPlayNextVideo: this.autoPlayNextVideo | |
83 | } | |
84 | ||
85 | this.userService.updateMyProfile(details).subscribe( | |
86 | () => { | |
87 | this.authService.refreshUserInformation() | |
88 | }, | |
89 | err => this.notifier.error(err.message) | |
90 | ) | |
91 | } | |
92 | } | |
7f5f4152 | 93 | } |