1 import { Component, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { immutableAssign } from '@app/shared/misc/utils'
4 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
5 import { NotificationsService } from 'angular2-notifications'
6 import 'rxjs/add/observable/from'
7 import 'rxjs/add/operator/concatAll'
8 import { Observable } from 'rxjs/Observable'
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: './account-videos.component.html',
18 styleUrls: [ './account-videos.component.scss' ]
20 export class AccountVideosComponent extends AbstractVideoList implements OnInit {
21 titlePage = 'My videos'
22 currentRoute = '/account/videos'
23 checkedVideos: { [ id: number ]: boolean } = {}
26 pagination: ComponentPagination = {
32 constructor (protected router: Router,
33 protected route: ActivatedRoute,
34 protected authService: AuthService,
35 protected notificationsService: NotificationsService,
36 protected confirmService: ConfirmService,
37 private videoService: VideoService) {
45 abortSelectionMode () {
46 this.checkedVideos = {}
49 isInSelectionMode () {
50 return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true)
53 getVideosObservable (page: number) {
54 const newPagination = immutableAssign(this.pagination, { currentPage: page })
56 return this.videoService.getMyVideos(newPagination, this.sort)
59 deleteSelectedVideos () {
60 const toDeleteVideosIds = Object.keys(this.checkedVideos)
61 .filter(k => this.checkedVideos[k] === true)
62 .map(k => parseInt(k, 10))
64 this.confirmService.confirm(`Do you really want to delete ${toDeleteVideosIds.length} videos?`, 'Delete').subscribe(
66 if (res === false) return
68 const observables: Observable<any>[] = []
69 for (const videoId of toDeleteVideosIds) {
70 const o = this.videoService
72 .do(() => this.spliceVideosById(videoId))
77 Observable.from(observables)
81 this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`)
82 this.buildVideoPages()
85 err => this.notificationsService.error('Error', err.message)
91 deleteVideo (video: Video) {
92 this.confirmService.confirm(`Do you really want to delete ${video.name}?`, 'Delete').subscribe(
94 if (res === false) return
96 this.videoService.removeVideo(video.id)
99 this.notificationsService.success('Success', `Video ${video.name} deleted.`)
100 this.spliceVideosById(video.id)
101 this.buildVideoPages()
104 error => this.notificationsService.error('Error', error.message)
110 private spliceVideosById (id: number) {
111 for (const key of Object.keys(this.loadedPages)) {
112 const videos = this.loadedPages[key]
113 const index = videos.findIndex(v => v.id === id)
116 videos.splice(index, 1)