Add i18n attributes
[oweals/peertube.git] / client / src / app / +my-account / my-account-videos / my-account-videos.component.ts
1 import { from as observableFrom, Observable } from 'rxjs'
2 import { concatAll, tap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { Location } from '@angular/common'
6 import { immutableAssign } from '@app/shared/misc/utils'
7 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
8 import { NotificationsService } from 'angular2-notifications'
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 import { I18n } from '@ngx-translate/i18n-polyfill'
15
16 @Component({
17   selector: 'my-account-videos',
18   templateUrl: './my-account-videos.component.html',
19   styleUrls: [ './my-account-videos.component.scss' ]
20 })
21 export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy {
22   titlePage: string
23   currentRoute = '/my-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 (
35     protected router: Router,
36     protected route: ActivatedRoute,
37     protected authService: AuthService,
38     protected notificationsService: NotificationsService,
39     protected confirmService: ConfirmService,
40     protected location: Location,
41     protected i18n: I18n,
42     private videoService: VideoService
43   ) {
44     super()
45
46     this.titlePage = this.i18n('My videos')
47   }
48
49   ngOnInit () {
50     super.ngOnInit()
51   }
52
53   ngOnDestroy () {
54     super.ngOnDestroy()
55   }
56
57   abortSelectionMode () {
58     this.checkedVideos = {}
59   }
60
61   isInSelectionMode () {
62     return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true)
63   }
64
65   getVideosObservable (page: number) {
66     const newPagination = immutableAssign(this.pagination, { currentPage: page })
67
68     return this.videoService.getMyVideos(newPagination, this.sort)
69   }
70
71   generateSyndicationList () {
72     throw new Error('Method not implemented.')
73   }
74
75   async deleteSelectedVideos () {
76     const toDeleteVideosIds = Object.keys(this.checkedVideos)
77       .filter(k => this.checkedVideos[k] === true)
78       .map(k => parseInt(k, 10))
79
80     const res = await this.confirmService.confirm(`Do you really want to delete ${toDeleteVideosIds.length} videos?`, 'Delete')
81     if (res === false) return
82
83     const observables: Observable<any>[] = []
84     for (const videoId of toDeleteVideosIds) {
85       const o = this.videoService
86                     .removeVideo(videoId)
87                     .pipe(tap(() => this.spliceVideosById(videoId)))
88
89       observables.push(o)
90     }
91
92     observableFrom(observables).pipe(
93       concatAll())
94       .subscribe(
95         res => {
96           this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`)
97           this.abortSelectionMode()
98           this.reloadVideos()
99         },
100
101         err => this.notificationsService.error('Error', err.message)
102       )
103   }
104
105   async deleteVideo (video: Video) {
106     const res = await this.confirmService.confirm(`Do you really want to delete ${video.name}?`, 'Delete')
107     if (res === false) return
108
109     this.videoService.removeVideo(video.id)
110       .subscribe(
111         status => {
112           this.notificationsService.success('Success', `Video ${video.name} deleted.`)
113           this.reloadVideos()
114         },
115
116         error => this.notificationsService.error('Error', error.message)
117       )
118   }
119
120   protected buildVideoHeight () {
121     // In account videos, the video height is fixed
122     return this.baseVideoHeight
123   }
124
125   private spliceVideosById (id: number) {
126     for (const key of Object.keys(this.loadedPages)) {
127       const videos = this.loadedPages[key]
128       const index = videos.findIndex(v => v.id === id)
129
130       if (index !== -1) {
131         videos.splice(index, 1)
132         return
133       }
134     }
135   }
136 }