Add search bars for a user's videos and playlist library
[oweals/peertube.git] / client / src / app / +my-account / my-account-videos / my-account-videos.component.ts
1 import { concat, Observable, Subject } from 'rxjs'
2 import { tap, toArray, debounceTime } from 'rxjs/operators'
3 import { Component, ViewChild, OnInit } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { immutableAssign } from '@app/shared/misc/utils'
6 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
7 import { Notifier, ServerService } from '@app/core'
8 import { AuthService } from '../../core/auth'
9 import { ConfirmService } from '../../core/confirm'
10 import { Video } from '../../shared/video/video.model'
11 import { VideoService } from '../../shared/video/video.service'
12 import { I18n } from '@ngx-translate/i18n-polyfill'
13 import { ScreenService } from '@app/shared/misc/screen.service'
14 import { VideoChangeOwnershipComponent } from './video-change-ownership/video-change-ownership.component'
15 import { MiniatureDisplayOptions } from '@app/shared/video/video-miniature.component'
16 import { SelectionType, VideosSelectionComponent } from '@app/shared/video/videos-selection.component'
17 import { VideoSortField } from '@app/shared/video/sort-field.type'
18 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
19
20 @Component({
21   selector: 'my-account-videos',
22   templateUrl: './my-account-videos.component.html',
23   styleUrls: [ './my-account-videos.component.scss' ]
24 })
25 export class MyAccountVideosComponent implements OnInit, DisableForReuseHook {
26   @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
27   @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
28
29   titlePage: string
30   selection: SelectionType = {}
31   pagination: ComponentPagination = {
32     currentPage: 1,
33     itemsPerPage: 5,
34     totalItems: null
35   }
36   miniatureDisplayOptions: MiniatureDisplayOptions = {
37     date: true,
38     views: true,
39     by: false,
40     privacyLabel: false,
41     privacyText: true,
42     state: true,
43     blacklistInfo: true
44   }
45   videos: Video[] = []
46   videosSearch: string
47   videosSearchChanged = new Subject<string>()
48   getVideosObservableFunction = this.getVideosObservable.bind(this)
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 i18n: I18n,
58     private confirmService: ConfirmService,
59     private videoService: VideoService
60   ) {
61     this.titlePage = this.i18n('My videos')
62   }
63
64   ngOnInit () {
65     this.videosSearchChanged
66       .pipe(
67         debounceTime(500))
68       .subscribe(() => {
69         this.videosSelection.reloadVideos()
70       })
71   }
72
73   onVideosSearchChanged () {
74     this.videosSearchChanged.next()
75   }
76
77   disableForReuse () {
78     this.videosSelection.disableForReuse()
79   }
80
81   enabledForReuse () {
82     this.videosSelection.enabledForReuse()
83   }
84
85   getVideosObservable (page: number, sort: VideoSortField) {
86     const newPagination = immutableAssign(this.pagination, { currentPage: page })
87
88     return this.videoService.getMyVideos(newPagination, sort, this.videosSearch)
89   }
90
91   async deleteSelectedVideos () {
92     const toDeleteVideosIds = Object.keys(this.selection)
93                                     .filter(k => this.selection[ k ] === true)
94                                     .map(k => parseInt(k, 10))
95
96     const res = await this.confirmService.confirm(
97       this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
98       this.i18n('Delete')
99     )
100     if (res === false) return
101
102     const observables: Observable<any>[] = []
103     for (const videoId of toDeleteVideosIds) {
104       const o = this.videoService.removeVideo(videoId)
105                     .pipe(tap(() => this.removeVideoFromArray(videoId)))
106
107       observables.push(o)
108     }
109
110     concat(...observables)
111       .pipe(toArray())
112       .subscribe(
113         () => {
114           this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }))
115
116           this.selection = {}
117         },
118
119         err => this.notifier.error(err.message)
120       )
121   }
122
123   async deleteVideo (video: Video) {
124     const res = await this.confirmService.confirm(
125       this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
126       this.i18n('Delete')
127     )
128     if (res === false) return
129
130     this.videoService.removeVideo(video.id)
131         .subscribe(
132           () => {
133             this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name }))
134             this.removeVideoFromArray(video.id)
135           },
136
137           error => this.notifier.error(error.message)
138         )
139   }
140
141   changeOwnership (event: Event, video: Video) {
142     event.preventDefault()
143     this.videoChangeOwnershipModal.show(video)
144   }
145
146   private removeVideoFromArray (id: number) {
147     this.videos = this.videos.filter(v => v.id !== id)
148   }
149 }