]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-library/my-videos/my-videos.component.ts
Fix filters error in console
[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'
384ba8b7
C
11import {
12 MiniatureDisplayOptions,
13 SelectionType,
14 VideoActionsDisplayType,
15 VideosSelectionComponent
16} from '@app/shared/shared-video-miniature'
22e90922 17import { VideoChannel, VideoSortField } from '@shared/models'
d846d99c 18import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
202f6b6c
C
19
20@Component({
17119e4a
C
21 templateUrl: './my-videos.component.html',
22 styleUrls: [ './my-videos.component.scss' ]
202f6b6c 23})
2e46eb97 24export class MyVideosComponent implements OnInit, DisableForReuseHook {
f36da21e
C
25 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
26 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
d846d99c 27 @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent
489290b8 28
b1d40cff 29 titlePage: string
693263e9 30 selection: SelectionType = {}
0cd4344f 31 pagination: ComponentPagination = {
234b535d 32 currentPage: 1,
1d904806 33 itemsPerPage: 10,
234b535d
C
34 totalItems: null
35 }
e2409062
C
36 miniatureDisplayOptions: MiniatureDisplayOptions = {
37 date: true,
38 views: true,
c4a6f790 39 by: true,
e2409062
C
40 privacyLabel: false,
41 privacyText: true,
42 state: true,
43 blacklistInfo: true
44 }
384ba8b7
C
45 videoDropdownDisplayOptions: VideoActionsDisplayType = {
46 playlist: false,
47 download: false,
48 update: false,
49 blacklist: false,
50 delete: true,
51 report: false,
52 duplicate: false,
53 mute: false,
26490335 54 liveInfo: true,
384ba8b7
C
55 removeFiles: false,
56 transcoding: false,
57 studio: true,
58 stats: true
59 }
c4a6f790 60
384ba8b7 61 moreVideoActions: DropdownAction<{ video: Video }>[][] = []
d846d99c 62
693263e9
C
63 videos: Video[] = []
64 getVideosObservableFunction = this.getVideosObservable.bind(this)
2e46eb97 65
8e286cdc 66 sort: VideoSortField = '-publishedAt'
202f6b6c 67
241609f1
C
68 user: User
69
60ab5b99 70 inputFilters: AdvancedInputFilter[] = []
1fd61899 71
dd24f1bb
C
72 disabled = false
73
2e46eb97 74 private search: string
978c87e7 75 private userChannels: VideoChannel[] = []
2e46eb97 76
b1d40cff
C
77 constructor (
78 protected router: Router,
489290b8 79 protected serverService: ServerService,
b1d40cff
C
80 protected route: ActivatedRoute,
81 protected authService: AuthService,
f8b2c1b4 82 protected notifier: Notifier,
bbe0f064 83 protected screenService: ScreenService,
308c4275 84 private confirmService: ConfirmService,
693263e9 85 private videoService: VideoService
b1d40cff 86 ) {
66357162 87 this.titlePage = $localize`My videos`
202f6b6c
C
88 }
89
bf64ed41 90 ngOnInit () {
d846d99c
C
91 this.buildActions()
92
241609f1 93 this.user = this.authService.getUser()
ca44cb36
C
94
95 if (this.route.snapshot.queryParams['search']) {
96 this.search = this.route.snapshot.queryParams['search']
97 }
978c87e7
C
98
99 this.authService.userInformationLoaded.subscribe(() => {
100 this.user = this.authService.getUser()
101 this.userChannels = this.user.videoChannels
102
103 const channelFilters = this.userChannels.map(c => {
104 return {
dd6d2a7c 105 value: 'channel:' + c.name,
978c87e7
C
106 label: c.name
107 }
108 })
109
110 this.inputFilters = [
111 {
112 title: $localize`Advanced filters`,
113 children: [
114 {
dd6d2a7c 115 value: 'isLive:true',
978c87e7
C
116 label: $localize`Only live videos`
117 }
118 ]
119 },
120
121 {
122 title: $localize`Channel filters`,
123 children: channelFilters
124 }
125 ]
126 })
bf64ed41
RK
127 }
128
2e46eb97
C
129 onSearch (search: string) {
130 this.search = search
131 this.reloadData()
4f5d0459
RK
132 }
133
2e46eb97 134 reloadData () {
1fd61899 135 this.videosSelection.reloadVideos()
bf64ed41
RK
136 }
137
8e286cdc
RK
138 onChangeSortColumn () {
139 this.videosSelection.reloadVideos()
140 }
141
693263e9 142 disableForReuse () {
dd24f1bb 143 this.disabled = true
9af61e84
C
144 }
145
693263e9 146 enabledForReuse () {
dd24f1bb 147 this.disabled = false
ce0e281d
C
148 }
149
0df302ca 150 getVideosObservable (page: number) {
0cd4344f
C
151 const newPagination = immutableAssign(this.pagination, { currentPage: page })
152
978c87e7
C
153 return this.videoService.getMyVideos({
154 videoPagination: newPagination,
155 sort: this.sort,
156 userChannels: this.userChannels,
157 search: this.search
158 })
aa0f1963
RK
159 .pipe(
160 tap(res => this.pagination.totalItems = res.total)
161 )
244e76a5
RK
162 }
163
1f30a185 164 async deleteSelectedVideos () {
693263e9 165 const toDeleteVideosIds = Object.keys(this.selection)
9df52d66 166 .filter(k => this.selection[k] === true)
2186386c 167 .map(k => parseInt(k, 10))
ce0e281d 168
2186386c 169 const res = await this.confirmService.confirm(
66357162
C
170 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
171 $localize`Delete`
2186386c 172 )
1f30a185
C
173 if (res === false) return
174
175 const observables: Observable<any>[] = []
176 for (const videoId of toDeleteVideosIds) {
2186386c 177 const o = this.videoService.removeVideo(videoId)
489290b8 178 .pipe(tap(() => this.removeVideoFromArray(videoId)))
1f30a185
C
179
180 observables.push(o)
181 }
182
489290b8
C
183 concat(...observables)
184 .pipe(toArray())
1378c0d3
C
185 .subscribe({
186 next: () => {
66357162 187 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
693263e9 188 this.selection = {}
1f30a185
C
189 },
190
1378c0d3
C
191 error: err => this.notifier.error(err.message)
192 })
ce0e281d
C
193 }
194
384ba8b7
C
195 onVideoRemoved (video: Video) {
196 this.removeVideoFromArray(video.id)
2186386c 197 }
1f30a185 198
d846d99c 199 changeOwnership (video: Video) {
74d63469
GR
200 this.videoChangeOwnershipModal.show(video)
201 }
202
489290b8
C
203 private removeVideoFromArray (id: number) {
204 this.videos = this.videos.filter(v => v.id !== id)
ce0e281d 205 }
d846d99c
C
206
207 private buildActions () {
384ba8b7
C
208 this.moreVideoActions = [
209 [
210 {
211 label: $localize`Change ownership`,
212 handler: ({ video }) => this.changeOwnership(video),
213 iconName: 'ownership-change'
214 }
215 ]
d846d99c
C
216 ]
217 }
202f6b6c 218}