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 } from '@app/core'
6 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
7 import { immutableAssign } from '@app/helpers'
8 import { Video, VideoService } from '@app/shared/shared-main'
9 import { MiniatureDisplayOptions, OwnerDisplayType, SelectionType, VideosSelectionComponent } from '@app/shared/shared-video-miniature'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { VideoSortField } from '@shared/models'
12 import { VideoChangeOwnershipComponent } from './video-change-ownership/video-change-ownership.component'
15 selector: 'my-account-videos',
16 templateUrl: './my-account-videos.component.html',
17 styleUrls: [ './my-account-videos.component.scss' ]
19 export class MyAccountVideosComponent implements OnInit, DisableForReuseHook {
20 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
21 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
24 selection: SelectionType = {}
25 pagination: ComponentPagination = {
30 miniatureDisplayOptions: MiniatureDisplayOptions = {
39 ownerDisplayType: OwnerDisplayType = 'videoChannel'
43 videosSearchChanged = new Subject<string>()
44 getVideosObservableFunction = this.getVideosObservable.bind(this)
47 protected router: Router,
48 protected serverService: ServerService,
49 protected route: ActivatedRoute,
50 protected authService: AuthService,
51 protected notifier: Notifier,
52 protected screenService: ScreenService,
54 private confirmService: ConfirmService,
55 private videoService: VideoService
57 this.titlePage = this.i18n('My videos')
61 this.videosSearchChanged
62 .pipe(debounceTime(500))
64 this.videosSelection.reloadVideos()
69 this.videosSearch = ''
70 this.onVideosSearchChanged()
73 onVideosSearchChanged () {
74 this.videosSearchChanged.next()
78 this.videosSelection.disableForReuse()
82 this.videosSelection.enabledForReuse()
85 getVideosObservable (page: number, sort: VideoSortField) {
86 const newPagination = immutableAssign(this.pagination, { currentPage: page })
88 return this.videoService.getMyVideos(newPagination, sort, this.videosSearch)
90 tap(res => this.pagination.totalItems = res.total)
94 async deleteSelectedVideos () {
95 const toDeleteVideosIds = Object.keys(this.selection)
96 .filter(k => this.selection[ k ] === true)
97 .map(k => parseInt(k, 10))
99 const res = await this.confirmService.confirm(
100 this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
103 if (res === false) return
105 const observables: Observable<any>[] = []
106 for (const videoId of toDeleteVideosIds) {
107 const o = this.videoService.removeVideo(videoId)
108 .pipe(tap(() => this.removeVideoFromArray(videoId)))
113 concat(...observables)
117 this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }))
122 err => this.notifier.error(err.message)
126 async deleteVideo (video: Video) {
127 const res = await this.confirmService.confirm(
128 this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
131 if (res === false) return
133 this.videoService.removeVideo(video.id)
136 this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name }))
137 this.removeVideoFromArray(video.id)
140 error => this.notifier.error(error.message)
144 changeOwnership (event: Event, video: Video) {
145 event.preventDefault()
146 this.videoChangeOwnershipModal.show(video)
149 private removeVideoFromArray (id: number) {
150 this.videos = this.videos.filter(v => v.id !== id)