]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/account/account-videos/account-videos.component.ts
Replace current state when changing page
[github/Chocobozzz/PeerTube.git] / client / src / app / account / account-videos / account-videos.component.ts
1 import { Component, OnInit, OnDestroy } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { Location } from '@angular/common'
4 import { immutableAssign } from '@app/shared/misc/utils'
5 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
6 import { NotificationsService } from 'angular2-notifications'
7 import 'rxjs/add/observable/from'
8 import 'rxjs/add/operator/concatAll'
9 import { Observable } from 'rxjs/Observable'
10 import { AuthService } from '../../core/auth'
11 import { ConfirmService } from '../../core/confirm'
12 import { AbstractVideoList } from '../../shared/video/abstract-video-list'
13 import { Video } from '../../shared/video/video.model'
14 import { VideoService } from '../../shared/video/video.service'
15
16 @Component({
17 selector: 'my-account-videos',
18 templateUrl: './account-videos.component.html',
19 styleUrls: [ './account-videos.component.scss' ]
20 })
21 export class AccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy {
22 titlePage = 'My videos'
23 currentRoute = '/account/videos'
24 checkedVideos: { [ id: number ]: boolean } = {}
25 pagination: ComponentPagination = {
26 currentPage: 1,
27 itemsPerPage: 5,
28 totalItems: null
29 }
30
31 protected baseVideoWidth = -1
32 protected baseVideoHeight = 155
33
34 constructor (protected router: Router,
35 protected route: ActivatedRoute,
36 protected authService: AuthService,
37 protected notificationsService: NotificationsService,
38 protected confirmService: ConfirmService,
39 protected location: Location,
40 private videoService: VideoService) {
41 super()
42 }
43
44 ngOnInit () {
45 super.ngOnInit()
46
47 // this.generateSyndicationList()
48 }
49
50 ngOnDestroy () {
51 super.ngOnDestroy()
52 }
53
54 abortSelectionMode () {
55 this.checkedVideos = {}
56 }
57
58 isInSelectionMode () {
59 return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true)
60 }
61
62 getVideosObservable (page: number) {
63 const newPagination = immutableAssign(this.pagination, { currentPage: page })
64
65 return this.videoService.getMyVideos(newPagination, this.sort)
66 }
67
68 generateSyndicationList () {
69 throw new Error('Method not implemented.')
70 }
71
72 async deleteSelectedVideos () {
73 const toDeleteVideosIds = Object.keys(this.checkedVideos)
74 .filter(k => this.checkedVideos[k] === true)
75 .map(k => parseInt(k, 10))
76
77 const res = await this.confirmService.confirm(`Do you really want to delete ${toDeleteVideosIds.length} videos?`, 'Delete')
78 if (res === false) return
79
80 const observables: Observable<any>[] = []
81 for (const videoId of toDeleteVideosIds) {
82 const o = this.videoService
83 .removeVideo(videoId)
84 .do(() => this.spliceVideosById(videoId))
85
86 observables.push(o)
87 }
88
89 Observable.from(observables)
90 .concatAll()
91 .subscribe(
92 res => {
93 this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`)
94 this.buildVideoPages()
95 },
96
97 err => this.notificationsService.error('Error', err.message)
98 )
99 }
100
101 async deleteVideo (video: Video) {
102 const res = await this.confirmService.confirm(`Do you really want to delete ${video.name}?`, 'Delete')
103 if (res === false) return
104
105 this.videoService.removeVideo(video.id)
106 .subscribe(
107 status => {
108 this.notificationsService.success('Success', `Video ${video.name} deleted.`)
109 this.spliceVideosById(video.id)
110 this.buildVideoPages()
111 },
112
113 error => this.notificationsService.error('Error', error.message)
114 )
115 }
116
117 protected buildVideoHeight () {
118 // In account videos, the video height is fixed
119 return this.baseVideoHeight
120 }
121
122 private spliceVideosById (id: number) {
123 for (const key of Object.keys(this.loadedPages)) {
124 const videos = this.loadedPages[key]
125 const index = videos.findIndex(v => v.id === id)
126
127 if (index !== -1) {
128 videos.splice(index, 1)
129 return
130 }
131 }
132 }
133 }