]>
Commit | Line | Data |
---|---|---|
1 | import { tap } from 'rxjs/operators' | |
2 | import { Component, OnInit, ViewChild } from '@angular/core' | |
3 | import { AuthService, ComponentPagination, ConfirmService, DisableForReuseHook, Notifier, User, UserService } from '@app/core' | |
4 | import { immutableAssign } from '@app/helpers' | |
5 | import { UserHistoryService, Video } from '@app/shared/shared-main' | |
6 | import { MiniatureDisplayOptions, VideosSelectionComponent } from '@app/shared/shared-video-miniature' | |
7 | ||
8 | @Component({ | |
9 | templateUrl: './my-history.component.html', | |
10 | styleUrls: [ './my-history.component.scss' ] | |
11 | }) | |
12 | export class MyHistoryComponent implements OnInit, DisableForReuseHook { | |
13 | @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent | |
14 | ||
15 | titlePage: string | |
16 | pagination: ComponentPagination = { | |
17 | currentPage: 1, | |
18 | itemsPerPage: 5, | |
19 | totalItems: null | |
20 | } | |
21 | ||
22 | videosHistoryEnabled: boolean | |
23 | ||
24 | miniatureDisplayOptions: MiniatureDisplayOptions = { | |
25 | date: true, | |
26 | views: true, | |
27 | by: true, | |
28 | privacyLabel: false, | |
29 | privacyText: true, | |
30 | state: true, | |
31 | blacklistInfo: true | |
32 | } | |
33 | ||
34 | getVideosObservableFunction = this.getVideosObservable.bind(this) | |
35 | ||
36 | user: User | |
37 | ||
38 | videos: Video[] = [] | |
39 | search: string | |
40 | ||
41 | disabled = false | |
42 | ||
43 | constructor ( | |
44 | private authService: AuthService, | |
45 | private userService: UserService, | |
46 | private notifier: Notifier, | |
47 | private confirmService: ConfirmService, | |
48 | private userHistoryService: UserHistoryService | |
49 | ) { | |
50 | this.titlePage = $localize`My watch history` | |
51 | } | |
52 | ||
53 | ngOnInit () { | |
54 | this.user = this.authService.getUser() | |
55 | ||
56 | this.authService.userInformationLoaded | |
57 | .subscribe(() => this.videosHistoryEnabled = this.user.videosHistoryEnabled) | |
58 | } | |
59 | ||
60 | disableForReuse () { | |
61 | this.disabled = true | |
62 | } | |
63 | ||
64 | enabledForReuse () { | |
65 | this.disabled = false | |
66 | } | |
67 | ||
68 | reloadData () { | |
69 | this.videosSelection.reloadVideos() | |
70 | } | |
71 | ||
72 | onSearch (search: string) { | |
73 | this.search = search | |
74 | this.reloadData() | |
75 | } | |
76 | ||
77 | getVideosObservable (page: number) { | |
78 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) | |
79 | ||
80 | return this.userHistoryService.list(newPagination, this.search) | |
81 | .pipe( | |
82 | tap(res => this.pagination.totalItems = res.total) | |
83 | ) | |
84 | } | |
85 | ||
86 | generateSyndicationList () { | |
87 | /* method disabled */ | |
88 | throw new Error('Method not implemented.') | |
89 | } | |
90 | ||
91 | onVideosHistoryChange () { | |
92 | this.userService.updateMyProfile({ videosHistoryEnabled: this.videosHistoryEnabled }) | |
93 | .subscribe({ | |
94 | next: () => { | |
95 | const message = this.videosHistoryEnabled === true | |
96 | ? $localize`Video history is enabled` | |
97 | : $localize`Video history is disabled` | |
98 | ||
99 | this.notifier.success(message) | |
100 | ||
101 | this.authService.refreshUserInformation() | |
102 | }, | |
103 | ||
104 | error: err => this.notifier.error(err.message) | |
105 | }) | |
106 | } | |
107 | ||
108 | deleteHistoryElement (video: Video) { | |
109 | this.userHistoryService.deleteElement(video) | |
110 | .subscribe({ | |
111 | next: () => { | |
112 | this.videos = this.videos.filter(v => v.id !== video.id) | |
113 | }, | |
114 | ||
115 | error: err => this.notifier.error(err.message) | |
116 | }) | |
117 | } | |
118 | ||
119 | async clearAllHistory () { | |
120 | const title = $localize`Delete video history` | |
121 | const message = $localize`Are you sure you want to delete all your video history?` | |
122 | ||
123 | const res = await this.confirmService.confirm(message, title) | |
124 | if (res !== true) return | |
125 | ||
126 | this.userHistoryService.clearAll() | |
127 | .subscribe({ | |
128 | next: () => { | |
129 | this.notifier.success($localize`Video history deleted`) | |
130 | ||
131 | this.reloadData() | |
132 | }, | |
133 | ||
134 | error: err => this.notifier.error(err.message) | |
135 | }) | |
136 | } | |
137 | ||
138 | getNoResultMessage () { | |
139 | if (this.search) { | |
140 | return $localize`No videos found for "${this.search}".` | |
141 | } | |
142 | ||
143 | return $localize`You don't have any video in your watch history yet.` | |
144 | } | |
145 | } |