]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-library/my-videos/my-videos.component.ts
Fix my videos search on refresh
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-library / my-videos / my-videos.component.ts
1 import { concat, Observable } from 'rxjs'
2 import { tap, toArray } from 'rxjs/operators'
3 import { Component, OnInit, ViewChild } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { AuthService, ComponentPagination, ConfirmService, Notifier, ScreenService, ServerService, User } from '@app/core'
6 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
7 import { immutableAssign } from '@app/helpers'
8 import { AdvancedInputFilter } from '@app/shared/shared-forms'
9 import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
10 import { LiveStreamInformationComponent } from '@app/shared/shared-video-live'
11 import { MiniatureDisplayOptions, SelectionType, VideosSelectionComponent } from '@app/shared/shared-video-miniature'
12 import { VideoSortField } from '@shared/models'
13 import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
14
15 @Component({
16 templateUrl: './my-videos.component.html',
17 styleUrls: [ './my-videos.component.scss' ]
18 })
19 export class MyVideosComponent implements OnInit, DisableForReuseHook {
20 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
21 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
22 @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent
23
24 titlePage: string
25 selection: SelectionType = {}
26 pagination: ComponentPagination = {
27 currentPage: 1,
28 itemsPerPage: 10,
29 totalItems: null
30 }
31 miniatureDisplayOptions: MiniatureDisplayOptions = {
32 date: true,
33 views: true,
34 by: true,
35 privacyLabel: false,
36 privacyText: true,
37 state: true,
38 blacklistInfo: true
39 }
40
41 videoActions: DropdownAction<{ video: Video }>[] = []
42
43 videos: Video[] = []
44 getVideosObservableFunction = this.getVideosObservable.bind(this)
45
46 sort: VideoSortField = '-publishedAt'
47
48 user: User
49
50 inputFilters: AdvancedInputFilter[] = [
51 {
52 queryParams: { search: 'isLive:true' },
53 label: $localize`Only live videos`
54 }
55 ]
56
57 disabled = false
58
59 private search: string
60
61 constructor (
62 protected router: Router,
63 protected serverService: ServerService,
64 protected route: ActivatedRoute,
65 protected authService: AuthService,
66 protected notifier: Notifier,
67 protected screenService: ScreenService,
68 private confirmService: ConfirmService,
69 private videoService: VideoService
70 ) {
71 this.titlePage = $localize`My videos`
72 }
73
74 ngOnInit () {
75 this.buildActions()
76
77 this.user = this.authService.getUser()
78
79 if (this.route.snapshot.queryParams['search']) {
80 this.search = this.route.snapshot.queryParams['search']
81 }
82 }
83
84 onSearch (search: string) {
85 this.search = search
86 this.reloadData()
87 }
88
89 reloadData () {
90 this.videosSelection.reloadVideos()
91 }
92
93 onChangeSortColumn () {
94 this.videosSelection.reloadVideos()
95 }
96
97 disableForReuse () {
98 this.disabled = true
99 }
100
101 enabledForReuse () {
102 this.disabled = false
103 }
104
105 getVideosObservable (page: number) {
106 const newPagination = immutableAssign(this.pagination, { currentPage: page })
107
108 return this.videoService.getMyVideos(newPagination, this.sort, this.search)
109 .pipe(
110 tap(res => this.pagination.totalItems = res.total)
111 )
112 }
113
114 async deleteSelectedVideos () {
115 const toDeleteVideosIds = Object.keys(this.selection)
116 .filter(k => this.selection[k] === true)
117 .map(k => parseInt(k, 10))
118
119 const res = await this.confirmService.confirm(
120 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
121 $localize`Delete`
122 )
123 if (res === false) return
124
125 const observables: Observable<any>[] = []
126 for (const videoId of toDeleteVideosIds) {
127 const o = this.videoService.removeVideo(videoId)
128 .pipe(tap(() => this.removeVideoFromArray(videoId)))
129
130 observables.push(o)
131 }
132
133 concat(...observables)
134 .pipe(toArray())
135 .subscribe({
136 next: () => {
137 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
138 this.selection = {}
139 },
140
141 error: err => this.notifier.error(err.message)
142 })
143 }
144
145 async deleteVideo (video: Video) {
146 const res = await this.confirmService.confirm(
147 $localize`Do you really want to delete ${video.name}?`,
148 $localize`Delete`
149 )
150 if (res === false) return
151
152 this.videoService.removeVideo(video.id)
153 .subscribe({
154 next: () => {
155 this.notifier.success($localize`Video ${video.name} deleted.`)
156 this.removeVideoFromArray(video.id)
157 },
158
159 error: err => this.notifier.error(err.message)
160 })
161 }
162
163 changeOwnership (video: Video) {
164 this.videoChangeOwnershipModal.show(video)
165 }
166
167 displayLiveInformation (video: Video) {
168 this.liveStreamInformationModal.show(video)
169 }
170
171 private removeVideoFromArray (id: number) {
172 this.videos = this.videos.filter(v => v.id !== id)
173 }
174
175 private buildActions () {
176 this.videoActions = [
177 {
178 label: $localize`Display live information`,
179 handler: ({ video }) => this.displayLiveInformation(video),
180 isDisplayed: ({ video }) => video.isLive,
181 iconName: 'live'
182 },
183 {
184 label: $localize`Change ownership`,
185 handler: ({ video }) => this.changeOwnership(video),
186 iconName: 'ownership-change'
187 },
188 {
189 label: $localize`Delete`,
190 handler: ({ video }) => this.deleteVideo(video),
191 iconName: 'delete'
192 }
193 ]
194 }
195 }