1 import { concat, Observable } from 'rxjs'
2 import { tap, toArray } from 'rxjs/operators'
3 import { Component, ViewChild } 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'
21 selector: 'my-account-videos',
22 templateUrl: './my-account-videos.component.html',
23 styleUrls: [ './my-account-videos.component.scss' ]
25 export class MyAccountVideosComponent implements DisableForReuseHook {
26 @ViewChild('videosSelection') videosSelection: VideosSelectionComponent
27 @ViewChild('videoChangeOwnershipModal') videoChangeOwnershipModal: VideoChangeOwnershipComponent
30 selection: SelectionType = {}
31 pagination: ComponentPagination = {
36 miniatureDisplayOptions: MiniatureDisplayOptions = {
46 getVideosObservableFunction = this.getVideosObservable.bind(this)
49 protected router: Router,
50 protected serverService: ServerService,
51 protected route: ActivatedRoute,
52 protected authService: AuthService,
53 protected notifier: Notifier,
54 protected screenService: ScreenService,
56 private confirmService: ConfirmService,
57 private videoService: VideoService
59 this.titlePage = this.i18n('My videos')
63 this.videosSelection.disableForReuse()
67 this.videosSelection.enabledForReuse()
70 getVideosObservable (page: number, sort: VideoSortField) {
71 const newPagination = immutableAssign(this.pagination, { currentPage: page })
73 return this.videoService.getMyVideos(newPagination, sort)
76 async deleteSelectedVideos () {
77 const toDeleteVideosIds = Object.keys(this.selection)
78 .filter(k => this.selection[ k ] === true)
79 .map(k => parseInt(k, 10))
81 const res = await this.confirmService.confirm(
82 this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
85 if (res === false) return
87 const observables: Observable<any>[] = []
88 for (const videoId of toDeleteVideosIds) {
89 const o = this.videoService.removeVideo(videoId)
90 .pipe(tap(() => this.removeVideoFromArray(videoId)))
95 concat(...observables)
99 this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }))
104 err => this.notifier.error(err.message)
108 async deleteVideo (video: Video) {
109 const res = await this.confirmService.confirm(
110 this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
113 if (res === false) return
115 this.videoService.removeVideo(video.id)
118 this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name }))
119 this.removeVideoFromArray(video.id)
122 error => this.notifier.error(error.message)
126 changeOwnership (event: Event, video: Video) {
127 event.preventDefault()
128 this.videoChangeOwnershipModal.show(video)
131 private removeVideoFromArray (id: number) {
132 this.videos = this.videos.filter(v => v.id !== id)