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