]>
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' | |
5def76eb | 10 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d3217560 | 11 | import { SessionStorageService } from '@app/shared/misc/storage.service' |
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 { | |
b0c36821 | 19 | @Input() inputRecommendation: RecommendationInfo |
7f5f4152 | 20 | @Input() user: User |
bee29df8 | 21 | @Input() playlist: VideoPlaylist |
6aa54148 | 22 | @Output() gotRecommendations = new EventEmitter<Video[]>() |
7f5f4152 | 23 | |
8adc5ddb | 24 | autoPlayNextVideo: boolean |
5def76eb | 25 | autoPlayNextVideoTooltip: string |
8adc5ddb | 26 | |
7f5f4152 BJ |
27 | readonly hasVideos$: Observable<boolean> |
28 | readonly videos$: Observable<Video[]> | |
29 | ||
30 | constructor ( | |
d816f3a0 RK |
31 | private userService: UserService, |
32 | private authService: AuthService, | |
33 | private notifier: Notifier, | |
5def76eb | 34 | private i18n: I18n, |
d3217560 RK |
35 | private store: RecommendedVideosStore, |
36 | private sessionStorageService: SessionStorageService | |
7f5f4152 BJ |
37 | ) { |
38 | this.videos$ = this.store.recommendations$ | |
39 | this.hasVideos$ = this.store.hasRecommendations$ | |
6aa54148 | 40 | this.videos$.subscribe(videos => this.gotRecommendations.emit(videos)) |
d816f3a0 | 41 | |
d3217560 RK |
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 | } | |
5def76eb RK |
50 | |
51 | this.autoPlayNextVideoTooltip = this.i18n('When active, the next video is automatically played after the current one.') | |
7f5f4152 BJ |
52 | } |
53 | ||
54 | public ngOnChanges (): void { | |
b0c36821 J |
55 | if (this.inputRecommendation) { |
56 | this.store.requestNewRecommendations(this.inputRecommendation) | |
7f5f4152 BJ |
57 | } |
58 | } | |
59 | ||
0a57bbff C |
60 | onVideoRemoved () { |
61 | this.store.requestNewRecommendations(this.inputRecommendation) | |
62 | } | |
d816f3a0 RK |
63 | |
64 | switchAutoPlayNextVideo () { | |
d3217560 | 65 | this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString()) |
d816f3a0 RK |
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 | } | |
7f5f4152 | 80 | } |