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