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'
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 = {
47 videosSearchChanged = new Subject<string>()
48 getVideosObservableFunction = this.getVideosObservable.bind(this)
51 protected router: Router,
52 protected serverService: ServerService,
53 protected route: ActivatedRoute,
54 protected authService: AuthService,
55 protected notifier: Notifier,
56 protected screenService: ScreenService,
58 private confirmService: ConfirmService,
59 private videoService: VideoService
61 this.titlePage = this.i18n('My videos')
65 this.videosSearchChanged
69 this.videosSelection.reloadVideos()
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)