Restore videos list components
[oweals/peertube.git] / client / src / app / +my-account / my-account-videos / my-account-videos.component.ts
1 import { concat, Observable } from 'rxjs'
2 import { tap, toArray } from 'rxjs/operators'
3 import { Component, Inject, LOCALE_ID, OnDestroy, OnInit, ViewChild } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { immutableAssign } from '@app/shared/misc/utils'
6 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
7 import { Notifier, ServerService } from '@app/core'
8 import { AuthService } from '../../core/auth'
9 import { ConfirmService } from '../../core/confirm'
10 import { AbstractVideoList } from '../../shared/video/abstract-video-list'
11 import { Video } from '../../shared/video/video.model'
12 import { VideoService } from '../../shared/video/video.service'
13 import { I18n } from '@ngx-translate/i18n-polyfill'
14 import { VideoPrivacy, VideoState } from '../../../../../shared/models/videos'
15 import { ScreenService } from '@app/shared/misc/screen.service'
16 import { VideoChangeOwnershipComponent } from './video-change-ownership/video-change-ownership.component'
17
18 @Component({
19   selector: 'my-account-videos',
20   templateUrl: './my-account-videos.component.html',
21   styleUrls: [ './my-account-videos.component.scss' ]
22 })
23 export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy {
24   @ViewChild('videoChangeOwnershipModal') videoChangeOwnershipModal: VideoChangeOwnershipComponent
25
26   titlePage: string
27   checkedVideos: { [ id: number ]: boolean } = {}
28   pagination: ComponentPagination = {
29     currentPage: 1,
30     itemsPerPage: 5,
31     totalItems: null
32   }
33
34   constructor (
35     protected router: Router,
36     protected serverService: ServerService,
37     protected route: ActivatedRoute,
38     protected authService: AuthService,
39     protected notifier: Notifier,
40     protected screenService: ScreenService,
41     private i18n: I18n,
42     private confirmService: ConfirmService,
43     private videoService: VideoService,
44     @Inject(LOCALE_ID) private localeId: string
45   ) {
46     super()
47
48     this.titlePage = this.i18n('My videos')
49   }
50
51   ngOnInit () {
52     super.ngOnInit()
53   }
54
55   ngOnDestroy () {
56     super.ngOnDestroy()
57   }
58
59   abortSelectionMode () {
60     this.checkedVideos = {}
61   }
62
63   isInSelectionMode () {
64     return Object.keys(this.checkedVideos).some(k => this.checkedVideos[ k ] === true)
65   }
66
67   getVideosObservable (page: number) {
68     const newPagination = immutableAssign(this.pagination, { currentPage: page })
69
70     return this.videoService.getMyVideos(newPagination, this.sort)
71   }
72
73   generateSyndicationList () {
74     throw new Error('Method not implemented.')
75   }
76
77   async deleteSelectedVideos () {
78     const toDeleteVideosIds = Object.keys(this.checkedVideos)
79                                     .filter(k => this.checkedVideos[ k ] === true)
80                                     .map(k => parseInt(k, 10))
81
82     const res = await this.confirmService.confirm(
83       this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
84       this.i18n('Delete')
85     )
86     if (res === false) return
87
88     const observables: Observable<any>[] = []
89     for (const videoId of toDeleteVideosIds) {
90       const o = this.videoService.removeVideo(videoId)
91                     .pipe(tap(() => this.removeVideoFromArray(videoId)))
92
93       observables.push(o)
94     }
95
96     concat(...observables)
97       .pipe(toArray())
98       .subscribe(
99         () => {
100           this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }))
101
102           this.abortSelectionMode()
103         },
104
105         err => this.notifier.error(err.message)
106       )
107   }
108
109   async deleteVideo (video: Video) {
110     const res = await this.confirmService.confirm(
111       this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
112       this.i18n('Delete')
113     )
114     if (res === false) return
115
116     this.videoService.removeVideo(video.id)
117         .subscribe(
118           () => {
119             this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name }))
120             this.reloadVideos()
121           },
122
123           error => this.notifier.error(error.message)
124         )
125   }
126
127   changeOwnership (event: Event, video: Video) {
128     event.preventDefault()
129     this.videoChangeOwnershipModal.show(video)
130   }
131
132   getStateLabel (video: Video) {
133     let suffix: string
134
135     if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
136       suffix = this.i18n('Published')
137     } else if (video.scheduledUpdate) {
138       const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
139       suffix = this.i18n('Publication scheduled on ') + updateAt
140     } else if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
141       suffix = this.i18n('Waiting transcoding')
142     } else if (video.state.id === VideoState.TO_TRANSCODE) {
143       suffix = this.i18n('To transcode')
144     } else if (video.state.id === VideoState.TO_IMPORT) {
145       suffix = this.i18n('To import')
146     } else {
147       return ''
148     }
149
150     return ' - ' + suffix
151   }
152
153   private removeVideoFromArray (id: number) {
154     this.videos = this.videos.filter(v => v.id !== id)
155   }
156 }