1 import { from as observableFrom, Observable } from 'rxjs'
2 import { concatAll, tap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { Location } from '@angular/common'
6 import { immutableAssign } from '@app/shared/misc/utils'
7 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
8 import { NotificationsService } from 'angular2-notifications'
9 import { AuthService } from '../../core/auth'
10 import { ConfirmService } from '../../core/confirm'
11 import { AbstractVideoList } from '../../shared/video/abstract-video-list'
12 import { Video } from '../../shared/video/video.model'
13 import { VideoService } from '../../shared/video/video.service'
14 import { I18n } from '@ngx-translate/i18n-polyfill'
17 selector: 'my-account-videos',
18 templateUrl: './my-account-videos.component.html',
19 styleUrls: [ './my-account-videos.component.scss' ]
21 export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy {
23 currentRoute = '/my-account/videos'
24 checkedVideos: { [ id: number ]: boolean } = {}
25 pagination: ComponentPagination = {
31 protected baseVideoWidth = -1
32 protected baseVideoHeight = 155
35 protected router: Router,
36 protected route: ActivatedRoute,
37 protected authService: AuthService,
38 protected notificationsService: NotificationsService,
39 protected confirmService: ConfirmService,
40 protected location: Location,
42 private videoService: VideoService
46 this.titlePage = this.i18n('My videos')
57 abortSelectionMode () {
58 this.checkedVideos = {}
61 isInSelectionMode () {
62 return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true)
65 getVideosObservable (page: number) {
66 const newPagination = immutableAssign(this.pagination, { currentPage: page })
68 return this.videoService.getMyVideos(newPagination, this.sort)
71 generateSyndicationList () {
72 throw new Error('Method not implemented.')
75 async deleteSelectedVideos () {
76 const toDeleteVideosIds = Object.keys(this.checkedVideos)
77 .filter(k => this.checkedVideos[k] === true)
78 .map(k => parseInt(k, 10))
80 const res = await this.confirmService.confirm(`Do you really want to delete ${toDeleteVideosIds.length} videos?`, 'Delete')
81 if (res === false) return
83 const observables: Observable<any>[] = []
84 for (const videoId of toDeleteVideosIds) {
85 const o = this.videoService
87 .pipe(tap(() => this.spliceVideosById(videoId)))
92 observableFrom(observables).pipe(
96 this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`)
97 this.abortSelectionMode()
101 err => this.notificationsService.error('Error', err.message)
105 async deleteVideo (video: Video) {
106 const res = await this.confirmService.confirm(`Do you really want to delete ${video.name}?`, 'Delete')
107 if (res === false) return
109 this.videoService.removeVideo(video.id)
112 this.notificationsService.success('Success', `Video ${video.name} deleted.`)
116 error => this.notificationsService.error('Error', error.message)
120 protected buildVideoHeight () {
121 // In account videos, the video height is fixed
122 return this.baseVideoHeight
125 private spliceVideosById (id: number) {
126 for (const key of Object.keys(this.loadedPages)) {
127 const videos = this.loadedPages[key]
128 const index = videos.findIndex(v => v.id === id)
131 videos.splice(index, 1)