]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/account/account-videos/account-videos.component.ts
Add confirm when admin use custom js/css
[github/Chocobozzz/PeerTube.git] / client / src / app / account / account-videos / account-videos.component.ts
... / ...
CommitLineData
1import { Component, OnInit } from '@angular/core'
2import { ActivatedRoute, Router } from '@angular/router'
3import { immutableAssign } from '@app/shared/misc/utils'
4import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
5import { NotificationsService } from 'angular2-notifications'
6import 'rxjs/add/observable/from'
7import 'rxjs/add/operator/concatAll'
8import { Observable } from 'rxjs/Observable'
9import { AuthService } from '../../core/auth'
10import { ConfirmService } from '../../core/confirm'
11import { AbstractVideoList } from '../../shared/video/abstract-video-list'
12import { Video } from '../../shared/video/video.model'
13import { 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})
20export 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 async deleteSelectedVideos () {
60 const toDeleteVideosIds = Object.keys(this.checkedVideos)
61 .filter(k => this.checkedVideos[k] === true)
62 .map(k => parseInt(k, 10))
63
64 const res = await this.confirmService.confirm(`Do you really want to delete ${toDeleteVideosIds.length} videos?`, 'Delete')
65 if (res === false) return
66
67 const observables: Observable<any>[] = []
68 for (const videoId of toDeleteVideosIds) {
69 const o = this.videoService
70 .removeVideo(videoId)
71 .do(() => this.spliceVideosById(videoId))
72
73 observables.push(o)
74 }
75
76 Observable.from(observables)
77 .concatAll()
78 .subscribe(
79 res => {
80 this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`)
81 this.buildVideoPages()
82 },
83
84 err => this.notificationsService.error('Error', err.message)
85 )
86 }
87
88 async deleteVideo (video: Video) {
89 const res = await this.confirmService.confirm(`Do you really want to delete ${video.name}?`, 'Delete')
90 if (res === false) return
91
92 this.videoService.removeVideo(video.id)
93 .subscribe(
94 status => {
95 this.notificationsService.success('Success', `Video ${video.name} deleted.`)
96 this.spliceVideosById(video.id)
97 this.buildVideoPages()
98 },
99
100 error => this.notificationsService.error('Error', error.message)
101 )
102 }
103
104 private spliceVideosById (id: number) {
105 for (const key of Object.keys(this.loadedPages)) {
106 const videos = this.loadedPages[key]
107 const index = videos.findIndex(v => v.id === id)
108
109 if (index !== -1) {
110 videos.splice(index, 1)
111 return
112 }
113 }
114 }
115}