]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-library/my-history/my-history.component.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-library / my-history / my-history.component.ts
CommitLineData
2e46eb97
C
1import { tap } from 'rxjs/operators'
2import { Component, ComponentFactoryResolver, OnInit, ViewChild } from '@angular/core'
80bfd33c 3import { ActivatedRoute, Router } from '@angular/router'
67ed6552
C
4import {
5 AuthService,
6 ComponentPagination,
7 ConfirmService,
2e46eb97 8 DisableForReuseHook,
67ed6552
C
9 LocalStorageService,
10 Notifier,
11 ScreenService,
12 ServerService,
2e46eb97 13 User,
67ed6552
C
14 UserService
15} from '@app/core'
16import { immutableAssign } from '@app/helpers'
2e46eb97
C
17import { UserHistoryService, Video } from '@app/shared/shared-main'
18import { MiniatureDisplayOptions, VideosSelectionComponent } from '@app/shared/shared-video-miniature'
80bfd33c
C
19
20@Component({
17119e4a
C
21 templateUrl: './my-history.component.html',
22 styleUrls: [ './my-history.component.scss' ]
80bfd33c 23})
2e46eb97
C
24export class MyHistoryComponent implements OnInit, DisableForReuseHook {
25 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
26
80bfd33c 27 titlePage: string
80bfd33c
C
28 pagination: ComponentPagination = {
29 currentPage: 1,
30 itemsPerPage: 5,
31 totalItems: null
32 }
2e46eb97 33
276d9652 34 videosHistoryEnabled: boolean
d8b34ee5 35
2e46eb97
C
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
80bfd33c 52
dd24f1bb
C
53 disabled = false
54
80bfd33c
C
55 constructor (
56 protected router: Router,
489290b8 57 protected serverService: ServerService,
80bfd33c
C
58 protected route: ActivatedRoute,
59 protected authService: AuthService,
276d9652 60 protected userService: UserService,
f8b2c1b4 61 protected notifier: Notifier,
80bfd33c 62 protected screenService: ScreenService,
d3217560 63 protected storageService: LocalStorageService,
80bfd33c 64 private confirmService: ConfirmService,
5bcbcbe3
RK
65 private userHistoryService: UserHistoryService,
66 protected cfr: ComponentFactoryResolver
80bfd33c 67 ) {
d8b34ee5 68 this.titlePage = $localize`My watch history`
80bfd33c
C
69 }
70
71 ngOnInit () {
2e46eb97 72 this.user = this.authService.getUser()
276d9652 73
4f926722 74 this.authService.userInformationLoaded
2e46eb97
C
75 .subscribe(() => this.videosHistoryEnabled = this.user.videosHistoryEnabled)
76 }
d8b34ee5 77
2e46eb97 78 disableForReuse () {
dd24f1bb 79 this.disabled = true
d8b34ee5
RK
80 }
81
2e46eb97 82 enabledForReuse () {
dd24f1bb 83 this.disabled = false
d8b34ee5
RK
84 }
85
2e46eb97
C
86 reloadData () {
87 this.videosSelection.reloadVideos()
80bfd33c
C
88 }
89
2e46eb97
C
90 onSearch (search: string) {
91 this.search = search
92 this.reloadData()
80bfd33c
C
93 }
94
95 getVideosObservable (page: number) {
96 const newPagination = immutableAssign(this.pagination, { currentPage: page })
97
d8b34ee5
RK
98 return this.userHistoryService.getUserVideosHistory(newPagination, this.search)
99 .pipe(
100 tap(res => this.pagination.totalItems = res.total)
101 )
80bfd33c
C
102 }
103
104 generateSyndicationList () {
5bcbcbe3 105 /* method disabled */
80bfd33c
C
106 throw new Error('Method not implemented.')
107 }
276d9652
C
108
109 onVideosHistoryChange () {
110 this.userService.updateMyProfile({ videosHistoryEnabled: this.videosHistoryEnabled })
1378c0d3
C
111 .subscribe({
112 next: () => {
9df52d66
C
113 const message = this.videosHistoryEnabled === true
114 ? $localize`Videos history is enabled`
115 : $localize`Videos history is disabled`
276d9652 116
f8b2c1b4 117 this.notifier.success(message)
276d9652
C
118
119 this.authService.refreshUserInformation()
120 },
121
1378c0d3
C
122 error: err => this.notifier.error(err.message)
123 })
276d9652
C
124 }
125
126 async deleteHistory () {
66357162
C
127 const title = $localize`Delete videos history`
128 const message = $localize`Are you sure you want to delete all your videos history?`
276d9652
C
129
130 const res = await this.confirmService.confirm(message, title)
131 if (res !== true) return
132
133 this.userHistoryService.deleteUserVideosHistory()
1378c0d3
C
134 .subscribe({
135 next: () => {
66357162 136 this.notifier.success($localize`Videos history deleted`)
276d9652 137
2e46eb97 138 this.reloadData()
276d9652
C
139 },
140
1378c0d3
C
141 error: err => this.notifier.error(err.message)
142 })
276d9652 143 }
80bfd33c 144}