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