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