]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-library/my-videos/my-videos.component.ts
Add more when deleting a video
[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'
978c87e7 12import { VideoChannel, 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
978c87e7 50 inputFilters: AdvancedInputFilter[]
1fd61899 51
dd24f1bb
C
52 disabled = false
53
2e46eb97 54 private search: string
978c87e7 55 private userChannels: VideoChannel[] = []
2e46eb97 56
b1d40cff
C
57 constructor (
58 protected router: Router,
489290b8 59 protected serverService: ServerService,
b1d40cff
C
60 protected route: ActivatedRoute,
61 protected authService: AuthService,
f8b2c1b4 62 protected notifier: Notifier,
bbe0f064 63 protected screenService: ScreenService,
308c4275 64 private confirmService: ConfirmService,
693263e9 65 private videoService: VideoService
b1d40cff 66 ) {
66357162 67 this.titlePage = $localize`My videos`
202f6b6c
C
68 }
69
bf64ed41 70 ngOnInit () {
d846d99c
C
71 this.buildActions()
72
241609f1 73 this.user = this.authService.getUser()
ca44cb36
C
74
75 if (this.route.snapshot.queryParams['search']) {
76 this.search = this.route.snapshot.queryParams['search']
77 }
978c87e7
C
78
79 this.authService.userInformationLoaded.subscribe(() => {
80 this.user = this.authService.getUser()
81 this.userChannels = this.user.videoChannels
82
83 const channelFilters = this.userChannels.map(c => {
84 return {
dd6d2a7c 85 value: 'channel:' + c.name,
978c87e7
C
86 label: c.name
87 }
88 })
89
90 this.inputFilters = [
91 {
92 title: $localize`Advanced filters`,
93 children: [
94 {
dd6d2a7c 95 value: 'isLive:true',
978c87e7
C
96 label: $localize`Only live videos`
97 }
98 ]
99 },
100
101 {
102 title: $localize`Channel filters`,
103 children: channelFilters
104 }
105 ]
106 })
bf64ed41
RK
107 }
108
2e46eb97
C
109 onSearch (search: string) {
110 this.search = search
111 this.reloadData()
4f5d0459
RK
112 }
113
2e46eb97 114 reloadData () {
1fd61899 115 this.videosSelection.reloadVideos()
bf64ed41
RK
116 }
117
8e286cdc
RK
118 onChangeSortColumn () {
119 this.videosSelection.reloadVideos()
120 }
121
693263e9 122 disableForReuse () {
dd24f1bb 123 this.disabled = true
9af61e84
C
124 }
125
693263e9 126 enabledForReuse () {
dd24f1bb 127 this.disabled = false
ce0e281d
C
128 }
129
0df302ca 130 getVideosObservable (page: number) {
0cd4344f
C
131 const newPagination = immutableAssign(this.pagination, { currentPage: page })
132
978c87e7
C
133 return this.videoService.getMyVideos({
134 videoPagination: newPagination,
135 sort: this.sort,
136 userChannels: this.userChannels,
137 search: this.search
138 })
aa0f1963
RK
139 .pipe(
140 tap(res => this.pagination.totalItems = res.total)
141 )
244e76a5
RK
142 }
143
1f30a185 144 async deleteSelectedVideos () {
693263e9 145 const toDeleteVideosIds = Object.keys(this.selection)
9df52d66 146 .filter(k => this.selection[k] === true)
2186386c 147 .map(k => parseInt(k, 10))
ce0e281d 148
2186386c 149 const res = await this.confirmService.confirm(
66357162
C
150 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
151 $localize`Delete`
2186386c 152 )
1f30a185
C
153 if (res === false) return
154
155 const observables: Observable<any>[] = []
156 for (const videoId of toDeleteVideosIds) {
2186386c 157 const o = this.videoService.removeVideo(videoId)
489290b8 158 .pipe(tap(() => this.removeVideoFromArray(videoId)))
1f30a185
C
159
160 observables.push(o)
161 }
162
489290b8
C
163 concat(...observables)
164 .pipe(toArray())
1378c0d3
C
165 .subscribe({
166 next: () => {
66357162 167 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
693263e9 168 this.selection = {}
1f30a185
C
169 },
170
1378c0d3
C
171 error: err => this.notifier.error(err.message)
172 })
ce0e281d
C
173 }
174
1f30a185 175 async deleteVideo (video: Video) {
2186386c 176 const res = await this.confirmService.confirm(
66357162
C
177 $localize`Do you really want to delete ${video.name}?`,
178 $localize`Delete`
2186386c 179 )
1f30a185
C
180 if (res === false) return
181
182 this.videoService.removeVideo(video.id)
1378c0d3
C
183 .subscribe({
184 next: () => {
66357162 185 this.notifier.success($localize`Video ${video.name} deleted.`)
693263e9 186 this.removeVideoFromArray(video.id)
2186386c
C
187 },
188
1378c0d3
C
189 error: err => this.notifier.error(err.message)
190 })
2186386c 191 }
1f30a185 192
d846d99c 193 changeOwnership (video: Video) {
74d63469
GR
194 this.videoChangeOwnershipModal.show(video)
195 }
196
d846d99c
C
197 displayLiveInformation (video: Video) {
198 this.liveStreamInformationModal.show(video)
199 }
200
489290b8
C
201 private removeVideoFromArray (id: number) {
202 this.videos = this.videos.filter(v => v.id !== id)
ce0e281d 203 }
d846d99c
C
204
205 private buildActions () {
206 this.videoActions = [
207 {
208 label: $localize`Display live information`,
209 handler: ({ video }) => this.displayLiveInformation(video),
210 isDisplayed: ({ video }) => video.isLive,
211 iconName: 'live'
212 },
213 {
214 label: $localize`Change ownership`,
215 handler: ({ video }) => this.changeOwnership(video),
216 iconName: 'ownership-change'
217 },
218 {
219 label: $localize`Delete`,
220 handler: ({ video }) => this.deleteVideo(video),
221 iconName: 'delete'
222 }
223 ]
224 }
202f6b6c 225}