]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/account/account-videos/account-videos.component.ts
Add ability to update another user video
[github/Chocobozzz/PeerTube.git] / client / src / app / account / account-videos / account-videos.component.ts
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'
14
15 @Component({
16 selector: 'my-account-videos',
17 templateUrl: './account-videos.component.html',
18 styleUrls: [ './account-videos.component.scss' ]
19 })
20 export class AccountVideosComponent extends AbstractVideoList implements OnInit {
21 titlePage = 'My videos'
22 currentRoute = '/account/videos'
23 checkedVideos: { [ id: number ]: boolean } = {}
24 videoHeight = 155
25 videoWidth = -1
26 pagination: ComponentPagination = {
27 currentPage: 1,
28 itemsPerPage: 10,
29 totalItems: null
30 }
31
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) {
38 super()
39 }
40
41 ngOnInit () {
42 super.ngOnInit()
43 }
44
45 abortSelectionMode () {
46 this.checkedVideos = {}
47 }
48
49 isInSelectionMode () {
50 return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true)
51 }
52
53 getVideosObservable (page: number) {
54 const newPagination = immutableAssign(this.pagination, { currentPage: page })
55
56 return this.videoService.getMyVideos(newPagination, this.sort)
57 }
58
59 deleteSelectedVideos () {
60 const toDeleteVideosIds = Object.keys(this.checkedVideos)
61 .filter(k => this.checkedVideos[k] === true)
62 .map(k => parseInt(k, 10))
63
64 this.confirmService.confirm(`Do you really want to delete ${toDeleteVideosIds.length} videos?`, 'Delete').subscribe(
65 res => {
66 if (res === false) return
67
68 const observables: Observable<any>[] = []
69 for (const videoId of toDeleteVideosIds) {
70 const o = this.videoService
71 .removeVideo(videoId)
72 .do(() => this.spliceVideosById(videoId))
73
74 observables.push(o)
75 }
76
77 Observable.from(observables)
78 .concatAll()
79 .subscribe(
80 res => {
81 this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`)
82 this.buildVideoPages()
83 },
84
85 err => this.notificationsService.error('Error', err.message)
86 )
87 }
88 )
89 }
90
91 deleteVideo (video: Video) {
92 this.confirmService.confirm(`Do you really want to delete ${video.name}?`, 'Delete').subscribe(
93 res => {
94 if (res === false) return
95
96 this.videoService.removeVideo(video.id)
97 .subscribe(
98 status => {
99 this.notificationsService.success('Success', `Video ${video.name} deleted.`)
100 this.spliceVideosById(video.id)
101 this.buildVideoPages()
102 },
103
104 error => this.notificationsService.error('Error', error.message)
105 )
106 }
107 )
108 }
109
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)
114
115 if (index !== -1) {
116 videos.splice(index, 1)
117 return
118 }
119 }
120 }
121 }