]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
1fd61899
C
1import { concat, Observable } from 'rxjs'
2import { tap, toArray } from 'rxjs/operators'
2e46eb97 3import { Component, OnInit, ViewChild } from '@angular/core'
be447678 4import { ActivatedRoute, Router } from '@angular/router'
2e46eb97 5import { AuthService, ComponentPagination, ConfirmService, Notifier, ScreenService, ServerService, User } from '@app/core'
67ed6552
C
6import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
7import { immutableAssign } from '@app/helpers'
1fd61899 8import { AdvancedInputFilter } from '@app/shared/shared-forms'
d846d99c 9import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
f8c00564 10import { LiveStreamInformationComponent } from '@app/shared/shared-video-live'
733dbc53 11import { MiniatureDisplayOptions, SelectionType, VideosSelectionComponent } from '@app/shared/shared-video-miniature'
67ed6552 12import { VideoSortField } from '@shared/models'
d846d99c 13import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
202f6b6c
C
14
15@Component({
17119e4a
C
16 templateUrl: './my-videos.component.html',
17 styleUrls: [ './my-videos.component.scss' ]
202f6b6c 18})
2e46eb97 19export class MyVideosComponent implements OnInit, DisableForReuseHook {
f36da21e
C
20 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
21 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
d846d99c 22 @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent
489290b8 23
b1d40cff 24 titlePage: string
693263e9 25 selection: SelectionType = {}
0cd4344f 26 pagination: ComponentPagination = {
234b535d 27 currentPage: 1,
1d904806 28 itemsPerPage: 10,
234b535d
C
29 totalItems: null
30 }
e2409062
C
31 miniatureDisplayOptions: MiniatureDisplayOptions = {
32 date: true,
33 views: true,
c4a6f790 34 by: true,
e2409062
C
35 privacyLabel: false,
36 privacyText: true,
37 state: true,
38 blacklistInfo: true
39 }
c4a6f790 40
d846d99c
C
41 videoActions: DropdownAction<{ video: Video }>[] = []
42
693263e9
C
43 videos: Video[] = []
44 getVideosObservableFunction = this.getVideosObservable.bind(this)
2e46eb97 45
8e286cdc 46 sort: VideoSortField = '-publishedAt'
202f6b6c 47
241609f1
C
48 user: User
49
1fd61899
C
50 inputFilters: AdvancedInputFilter[] = [
51 {
52 queryParams: { 'search': 'isLive:true' },
53 label: $localize`Only live videos`
54 }
55 ]
56
2e46eb97
C
57 private search: string
58
b1d40cff
C
59 constructor (
60 protected router: Router,
489290b8 61 protected serverService: ServerService,
b1d40cff
C
62 protected route: ActivatedRoute,
63 protected authService: AuthService,
f8b2c1b4 64 protected notifier: Notifier,
bbe0f064 65 protected screenService: ScreenService,
308c4275 66 private confirmService: ConfirmService,
693263e9 67 private videoService: VideoService
b1d40cff 68 ) {
66357162 69 this.titlePage = $localize`My videos`
202f6b6c
C
70 }
71
bf64ed41 72 ngOnInit () {
d846d99c
C
73 this.buildActions()
74
241609f1 75 this.user = this.authService.getUser()
bf64ed41
RK
76 }
77
2e46eb97
C
78 onSearch (search: string) {
79 this.search = search
80 this.reloadData()
4f5d0459
RK
81 }
82
2e46eb97 83 reloadData () {
1fd61899 84 this.videosSelection.reloadVideos()
bf64ed41
RK
85 }
86
8e286cdc
RK
87 onChangeSortColumn () {
88 this.videosSelection.reloadVideos()
89 }
90
693263e9
C
91 disableForReuse () {
92 this.videosSelection.disableForReuse()
9af61e84
C
93 }
94
693263e9
C
95 enabledForReuse () {
96 this.videosSelection.enabledForReuse()
ce0e281d
C
97 }
98
0df302ca 99 getVideosObservable (page: number) {
0cd4344f
C
100 const newPagination = immutableAssign(this.pagination, { currentPage: page })
101
1fd61899 102 return this.videoService.getMyVideos(newPagination, this.sort, this.search)
aa0f1963
RK
103 .pipe(
104 tap(res => this.pagination.totalItems = res.total)
105 )
244e76a5
RK
106 }
107
1f30a185 108 async deleteSelectedVideos () {
693263e9
C
109 const toDeleteVideosIds = Object.keys(this.selection)
110 .filter(k => this.selection[ k ] === true)
2186386c 111 .map(k => parseInt(k, 10))
ce0e281d 112
2186386c 113 const res = await this.confirmService.confirm(
66357162
C
114 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
115 $localize`Delete`
2186386c 116 )
1f30a185
C
117 if (res === false) return
118
119 const observables: Observable<any>[] = []
120 for (const videoId of toDeleteVideosIds) {
2186386c 121 const o = this.videoService.removeVideo(videoId)
489290b8 122 .pipe(tap(() => this.removeVideoFromArray(videoId)))
1f30a185
C
123
124 observables.push(o)
125 }
126
489290b8
C
127 concat(...observables)
128 .pipe(toArray())
1f30a185 129 .subscribe(
489290b8 130 () => {
66357162 131 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
693263e9 132 this.selection = {}
1f30a185
C
133 },
134
f8b2c1b4 135 err => this.notifier.error(err.message)
1f30a185 136 )
ce0e281d
C
137 }
138
1f30a185 139 async deleteVideo (video: Video) {
2186386c 140 const res = await this.confirmService.confirm(
66357162
C
141 $localize`Do you really want to delete ${video.name}?`,
142 $localize`Delete`
2186386c 143 )
1f30a185
C
144 if (res === false) return
145
146 this.videoService.removeVideo(video.id)
2186386c 147 .subscribe(
f8b2c1b4 148 () => {
66357162 149 this.notifier.success($localize`Video ${video.name} deleted.`)
693263e9 150 this.removeVideoFromArray(video.id)
2186386c
C
151 },
152
f8b2c1b4 153 error => this.notifier.error(error.message)
2186386c
C
154 )
155 }
1f30a185 156
d846d99c 157 changeOwnership (video: Video) {
74d63469
GR
158 this.videoChangeOwnershipModal.show(video)
159 }
160
d846d99c
C
161 displayLiveInformation (video: Video) {
162 this.liveStreamInformationModal.show(video)
163 }
164
489290b8
C
165 private removeVideoFromArray (id: number) {
166 this.videos = this.videos.filter(v => v.id !== id)
ce0e281d 167 }
d846d99c
C
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 }
202f6b6c 189}