1 import { Component, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { NotificationsService } from 'angular2-notifications'
4 import 'rxjs/add/observable/from'
5 import 'rxjs/add/operator/concatAll'
6 import { Observable } from 'rxjs/Observable'
7 import { AuthService } from '../../core/auth'
8 import { ConfirmService } from '../../core/confirm'
9 import { AbstractVideoList } from '../../shared/video/abstract-video-list'
10 import { Video } from '../../shared/video/video.model'
11 import { VideoService } from '../../shared/video/video.service'
14 selector: 'my-account-videos',
15 templateUrl: './account-videos.component.html',
16 styleUrls: [ './account-videos.component.scss' ]
18 export class AccountVideosComponent extends AbstractVideoList implements OnInit {
19 titlePage = 'My videos'
20 currentRoute = '/account/videos'
21 checkedVideos: { [ id: number ]: boolean } = {}
23 constructor (protected router: Router,
24 protected route: ActivatedRoute,
25 protected authService: AuthService,
26 protected notificationsService: NotificationsService,
27 protected confirmService: ConfirmService,
28 private videoService: VideoService) {
36 abortSelectionMode () {
37 this.checkedVideos = {}
40 isInSelectionMode () {
41 return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true)
44 getVideosObservable () {
45 return this.videoService.getMyVideos(this.pagination, this.sort)
48 deleteSelectedVideos () {
49 const toDeleteVideosIds = Object.keys(this.checkedVideos)
50 .filter(k => this.checkedVideos[k] === true)
51 .map(k => parseInt(k, 10))
53 this.confirmService.confirm(`Do you really want to delete ${toDeleteVideosIds.length} videos?`, 'Delete').subscribe(
55 if (res === false) return
57 const observables: Observable<any>[] = []
58 for (const videoId of toDeleteVideosIds) {
59 const o = this.videoService
61 .do(() => this.spliceVideosById(videoId))
66 Observable.from(observables)
69 res => this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`),
71 err => this.notificationsService.error('Error', err.message)
77 deleteVideo (video: Video) {
78 this.confirmService.confirm(`Do you really want to delete ${video.name}?`, 'Delete').subscribe(
80 if (res === false) return
82 this.videoService.removeVideo(video.id)
85 this.notificationsService.success('Success', `Video ${video.name} deleted.`)
86 this.spliceVideosById(video.id)
89 error => this.notificationsService.error('Error', error.message)
95 private spliceVideosById (id: number) {
96 const index = this.videos.findIndex(v => v.id === id)
97 this.videos.splice(index, 1)