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 import { MiniatureDisplayOptions } from '@app/shared/video/video-miniature.component'
20 selector: 'my-account-videos',
21 templateUrl: './my-account-videos.component.html',
22 styleUrls: [ './my-account-videos.component.scss' ]
24 export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy {
25 @ViewChild('videoChangeOwnershipModal') videoChangeOwnershipModal: VideoChangeOwnershipComponent
28 checkedVideos: { [ id: number ]: boolean } = {}
29 pagination: ComponentPagination = {
34 miniatureDisplayOptions: MiniatureDisplayOptions = {
45 protected router: Router,
46 protected serverService: ServerService,
47 protected route: ActivatedRoute,
48 protected authService: AuthService,
49 protected notifier: Notifier,
50 protected screenService: ScreenService,
52 private confirmService: ConfirmService,
53 private videoService: VideoService,
54 @Inject(LOCALE_ID) private localeId: string
58 this.titlePage = this.i18n('My videos')
69 abortSelectionMode () {
70 this.checkedVideos = {}
73 isInSelectionMode () {
74 return Object.keys(this.checkedVideos).some(k => this.checkedVideos[ k ] === true)
77 getVideosObservable (page: number) {
78 const newPagination = immutableAssign(this.pagination, { currentPage: page })
80 return this.videoService.getMyVideos(newPagination, this.sort)
83 generateSyndicationList () {
84 throw new Error('Method not implemented.')
87 async deleteSelectedVideos () {
88 const toDeleteVideosIds = Object.keys(this.checkedVideos)
89 .filter(k => this.checkedVideos[ k ] === true)
90 .map(k => parseInt(k, 10))
92 const res = await this.confirmService.confirm(
93 this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
96 if (res === false) return
98 const observables: Observable<any>[] = []
99 for (const videoId of toDeleteVideosIds) {
100 const o = this.videoService.removeVideo(videoId)
101 .pipe(tap(() => this.removeVideoFromArray(videoId)))
106 concat(...observables)
110 this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }))
112 this.abortSelectionMode()
115 err => this.notifier.error(err.message)
119 async deleteVideo (video: Video) {
120 const res = await this.confirmService.confirm(
121 this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
124 if (res === false) return
126 this.videoService.removeVideo(video.id)
129 this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name }))
133 error => this.notifier.error(error.message)
137 changeOwnership (event: Event, video: Video) {
138 event.preventDefault()
139 this.videoChangeOwnershipModal.show(video)
142 getStateLabel (video: Video) {
145 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
146 suffix = this.i18n('Published')
147 } else if (video.scheduledUpdate) {
148 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
149 suffix = this.i18n('Publication scheduled on ') + updateAt
150 } else if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
151 suffix = this.i18n('Waiting transcoding')
152 } else if (video.state.id === VideoState.TO_TRANSCODE) {
153 suffix = this.i18n('To transcode')
154 } else if (video.state.id === VideoState.TO_IMPORT) {
155 suffix = this.i18n('To import')
160 return ' - ' + suffix
163 private removeVideoFromArray (id: number) {
164 this.videos = this.videos.filter(v => v.id !== id)