]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-library/my-videos/my-videos.component.ts
Refactor search filters
[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 private search: string
58
59 constructor (
60 protected router: Router,
61 protected serverService: ServerService,
62 protected route: ActivatedRoute,
63 protected authService: AuthService,
64 protected notifier: Notifier,
65 protected screenService: ScreenService,
66 private confirmService: ConfirmService,
67 private videoService: VideoService
68 ) {
69 this.titlePage = $localize`My videos`
70 }
71
72 ngOnInit () {
73 this.buildActions()
74
75 this.user = this.authService.getUser()
76 }
77
78 onSearch (search: string) {
79 this.search = search
80 this.reloadData()
81 }
82
83 reloadData () {
84 this.videosSelection.reloadVideos()
85 }
86
87 onChangeSortColumn () {
88 this.videosSelection.reloadVideos()
89 }
90
91 disableForReuse () {
92 this.videosSelection.disableForReuse()
93 }
94
95 enabledForReuse () {
96 this.videosSelection.enabledForReuse()
97 }
98
99 getVideosObservable (page: number) {
100 const newPagination = immutableAssign(this.pagination, { currentPage: page })
101
102 return this.videoService.getMyVideos(newPagination, this.sort, this.search)
103 .pipe(
104 tap(res => this.pagination.totalItems = res.total)
105 )
106 }
107
108 async deleteSelectedVideos () {
109 const toDeleteVideosIds = Object.keys(this.selection)
110 .filter(k => this.selection[ k ] === true)
111 .map(k => parseInt(k, 10))
112
113 const res = await this.confirmService.confirm(
114 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
115 $localize`Delete`
116 )
117 if (res === false) return
118
119 const observables: Observable<any>[] = []
120 for (const videoId of toDeleteVideosIds) {
121 const o = this.videoService.removeVideo(videoId)
122 .pipe(tap(() => this.removeVideoFromArray(videoId)))
123
124 observables.push(o)
125 }
126
127 concat(...observables)
128 .pipe(toArray())
129 .subscribe(
130 () => {
131 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
132 this.selection = {}
133 },
134
135 err => this.notifier.error(err.message)
136 )
137 }
138
139 async deleteVideo (video: Video) {
140 const res = await this.confirmService.confirm(
141 $localize`Do you really want to delete ${video.name}?`,
142 $localize`Delete`
143 )
144 if (res === false) return
145
146 this.videoService.removeVideo(video.id)
147 .subscribe(
148 () => {
149 this.notifier.success($localize`Video ${video.name} deleted.`)
150 this.removeVideoFromArray(video.id)
151 },
152
153 error => this.notifier.error(error.message)
154 )
155 }
156
157 changeOwnership (video: Video) {
158 this.videoChangeOwnershipModal.show(video)
159 }
160
161 displayLiveInformation (video: Video) {
162 this.liveStreamInformationModal.show(video)
163 }
164
165 private removeVideoFromArray (id: number) {
166 this.videos = this.videos.filter(v => v.id !== id)
167 }
168
169 private buildActions () {
170 this.videoActions = [
171 {
172 label: $localize`Display live information`,
173 handler: ({ video }) => this.displayLiveInformation(video),
174 isDisplayed: ({ video }) => video.isLive,
175 iconName: 'live'
176 },
177 {
178 label: $localize`Change ownership`,
179 handler: ({ video }) => this.changeOwnership(video),
180 iconName: 'ownership-change'
181 },
182 {
183 label: $localize`Delete`,
184 handler: ({ video }) => this.deleteVideo(video),
185 iconName: 'delete'
186 }
187 ]
188 }
189 }