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, OwnerDisplayType } 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 OnInit, DisableForReuseHook {
26 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
27 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
30 selection: SelectionType = {}
31 pagination: ComponentPagination = {
36 miniatureDisplayOptions: MiniatureDisplayOptions = {
45 ownerDisplayType: OwnerDisplayType = 'videoChannel'
49 videosSearchChanged = new Subject<string>()
50 getVideosObservableFunction = this.getVideosObservable.bind(this)
53 protected router: Router,
54 protected serverService: ServerService,
55 protected route: ActivatedRoute,
56 protected authService: AuthService,
57 protected notifier: Notifier,
58 protected screenService: ScreenService,
60 private confirmService: ConfirmService,
61 private videoService: VideoService
63 this.titlePage = this.i18n('My videos')
67 this.videosSearchChanged
71 this.videosSelection.reloadVideos()
75 onVideosSearchChanged () {
76 this.videosSearchChanged.next()
80 this.videosSelection.disableForReuse()
84 this.videosSelection.enabledForReuse()
87 getVideosObservable (page: number, sort: VideoSortField) {
88 const newPagination = immutableAssign(this.pagination, { currentPage: page })
90 return this.videoService.getMyVideos(newPagination, sort, this.videosSearch)
92 tap(res => this.pagination.totalItems = res.total)
96 async deleteSelectedVideos () {
97 const toDeleteVideosIds = Object.keys(this.selection)
98 .filter(k => this.selection[ k ] === true)
99 .map(k => parseInt(k, 10))
101 const res = await this.confirmService.confirm(
102 this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
105 if (res === false) return
107 const observables: Observable<any>[] = []
108 for (const videoId of toDeleteVideosIds) {
109 const o = this.videoService.removeVideo(videoId)
110 .pipe(tap(() => this.removeVideoFromArray(videoId)))
115 concat(...observables)
119 this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }))
124 err => this.notifier.error(err.message)
128 async deleteVideo (video: Video) {
129 const res = await this.confirmService.confirm(
130 this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
133 if (res === false) return
135 this.videoService.removeVideo(video.id)
138 this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name }))
139 this.removeVideoFromArray(video.id)
142 error => this.notifier.error(error.message)
146 changeOwnership (event: Event, video: Video) {
147 event.preventDefault()
148 this.videoChangeOwnershipModal.show(video)
151 private removeVideoFromArray (id: number) {
152 this.videos = this.videos.filter(v => v.id !== id)