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