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