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'
16 selector: 'my-account-videos',
17 templateUrl: './my-account-videos.component.html',
18 styleUrls: [ './my-account-videos.component.scss' ]
20 export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy {
21 titlePage = 'My videos'
22 currentRoute = '/my-account/videos'
23 checkedVideos: { [ id: number ]: boolean } = {}
24 pagination: ComponentPagination = {
30 protected baseVideoWidth = -1
31 protected baseVideoHeight = 155
33 constructor (protected router: Router,
34 protected route: ActivatedRoute,
35 protected authService: AuthService,
36 protected notificationsService: NotificationsService,
37 protected confirmService: ConfirmService,
38 protected location: Location,
39 private videoService: VideoService) {
51 abortSelectionMode () {
52 this.checkedVideos = {}
55 isInSelectionMode () {
56 return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true)
59 getVideosObservable (page: number) {
60 const newPagination = immutableAssign(this.pagination, { currentPage: page })
62 return this.videoService.getMyVideos(newPagination, this.sort)
65 generateSyndicationList () {
66 throw new Error('Method not implemented.')
69 async deleteSelectedVideos () {
70 const toDeleteVideosIds = Object.keys(this.checkedVideos)
71 .filter(k => this.checkedVideos[k] === true)
72 .map(k => parseInt(k, 10))
74 const res = await this.confirmService.confirm(`Do you really want to delete ${toDeleteVideosIds.length} videos?`, 'Delete')
75 if (res === false) return
77 const observables: Observable<any>[] = []
78 for (const videoId of toDeleteVideosIds) {
79 const o = this.videoService
81 .pipe(tap(() => this.spliceVideosById(videoId)))
86 observableFrom(observables).pipe(
90 this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`)
91 this.abortSelectionMode()
95 err => this.notificationsService.error('Error', err.message)
99 async deleteVideo (video: Video) {
100 const res = await this.confirmService.confirm(`Do you really want to delete ${video.name}?`, 'Delete')
101 if (res === false) return
103 this.videoService.removeVideo(video.id)
106 this.notificationsService.success('Success', `Video ${video.name} deleted.`)
110 error => this.notificationsService.error('Error', error.message)
114 protected buildVideoHeight () {
115 // In account videos, the video height is fixed
116 return this.baseVideoHeight
119 private spliceVideosById (id: number) {
120 for (const key of Object.keys(this.loadedPages)) {
121 const videos = this.loadedPages[key]
122 const index = videos.findIndex(v => v.id === id)
125 videos.splice(index, 1)