]>
Commit | Line | Data |
---|---|---|
db400f44 C |
1 | import { from as observableFrom, Observable } from 'rxjs' |
2 | import { concatAll, tap } from 'rxjs/operators' | |
3 | import { Component, OnDestroy, OnInit } from '@angular/core' | |
be447678 | 4 | import { ActivatedRoute, Router } from '@angular/router' |
2a2c19df | 5 | import { Location } from '@angular/common' |
0cd4344f C |
6 | import { immutableAssign } from '@app/shared/misc/utils' |
7 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
202f6b6c | 8 | import { NotificationsService } from 'angular2-notifications' |
b2731bff | 9 | import { AuthService } from '../../core/auth' |
332542bc | 10 | import { ConfirmService } from '../../core/confirm' |
be447678 | 11 | import { AbstractVideoList } from '../../shared/video/abstract-video-list' |
332542bc | 12 | import { Video } from '../../shared/video/video.model' |
202f6b6c | 13 | import { VideoService } from '../../shared/video/video.service' |
b1d40cff | 14 | import { I18n } from '@ngx-translate/i18n-polyfill' |
2186386c | 15 | import { VideoState } from '../../../../../shared/models/videos' |
202f6b6c C |
16 | |
17 | @Component({ | |
18 | selector: 'my-account-videos', | |
4bb6886d C |
19 | templateUrl: './my-account-videos.component.html', |
20 | styleUrls: [ './my-account-videos.component.scss' ] | |
202f6b6c | 21 | }) |
4bb6886d | 22 | export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy { |
b1d40cff | 23 | titlePage: string |
4bb6886d | 24 | currentRoute = '/my-account/videos' |
ce0e281d | 25 | checkedVideos: { [ id: number ]: boolean } = {} |
0cd4344f | 26 | pagination: ComponentPagination = { |
234b535d | 27 | currentPage: 1, |
2e78e268 | 28 | itemsPerPage: 5, |
234b535d C |
29 | totalItems: null |
30 | } | |
202f6b6c | 31 | |
9af61e84 C |
32 | protected baseVideoWidth = -1 |
33 | protected baseVideoHeight = 155 | |
34 | ||
b1d40cff C |
35 | constructor ( |
36 | protected router: Router, | |
37 | protected route: ActivatedRoute, | |
38 | protected authService: AuthService, | |
39 | protected notificationsService: NotificationsService, | |
40 | protected confirmService: ConfirmService, | |
41 | protected location: Location, | |
42 | protected i18n: I18n, | |
43 | private videoService: VideoService | |
44 | ) { | |
202f6b6c | 45 | super() |
b1d40cff C |
46 | |
47 | this.titlePage = this.i18n('My videos') | |
202f6b6c C |
48 | } |
49 | ||
50 | ngOnInit () { | |
51 | super.ngOnInit() | |
52 | } | |
53 | ||
9af61e84 C |
54 | ngOnDestroy () { |
55 | super.ngOnDestroy() | |
56 | } | |
57 | ||
ce0e281d C |
58 | abortSelectionMode () { |
59 | this.checkedVideos = {} | |
60 | } | |
61 | ||
62 | isInSelectionMode () { | |
2186386c | 63 | return Object.keys(this.checkedVideos).some(k => this.checkedVideos[ k ] === true) |
ce0e281d C |
64 | } |
65 | ||
0cd4344f C |
66 | getVideosObservable (page: number) { |
67 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) | |
68 | ||
69 | return this.videoService.getMyVideos(newPagination, this.sort) | |
202f6b6c | 70 | } |
332542bc | 71 | |
244e76a5 RK |
72 | generateSyndicationList () { |
73 | throw new Error('Method not implemented.') | |
74 | } | |
75 | ||
1f30a185 | 76 | async deleteSelectedVideos () { |
ce0e281d | 77 | const toDeleteVideosIds = Object.keys(this.checkedVideos) |
2186386c C |
78 | .filter(k => this.checkedVideos[ k ] === true) |
79 | .map(k => parseInt(k, 10)) | |
ce0e281d | 80 | |
2186386c C |
81 | const res = await this.confirmService.confirm( |
82 | this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }), | |
83 | this.i18n('Delete') | |
84 | ) | |
1f30a185 C |
85 | if (res === false) return |
86 | ||
87 | const observables: Observable<any>[] = [] | |
88 | for (const videoId of toDeleteVideosIds) { | |
2186386c | 89 | const o = this.videoService.removeVideo(videoId) |
db400f44 | 90 | .pipe(tap(() => this.spliceVideosById(videoId))) |
1f30a185 C |
91 | |
92 | observables.push(o) | |
93 | } | |
94 | ||
2186386c C |
95 | observableFrom(observables) |
96 | .pipe(concatAll()) | |
1f30a185 C |
97 | .subscribe( |
98 | res => { | |
2186386c C |
99 | this.notificationsService.success( |
100 | this.i18n('Success'), | |
101 | this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }) | |
102 | ) | |
103 | ||
06be7ed0 C |
104 | this.abortSelectionMode() |
105 | this.reloadVideos() | |
1f30a185 C |
106 | }, |
107 | ||
2186386c | 108 | err => this.notificationsService.error(this.i18n('Error'), err.message) |
1f30a185 | 109 | ) |
ce0e281d C |
110 | } |
111 | ||
1f30a185 | 112 | async deleteVideo (video: Video) { |
2186386c C |
113 | const res = await this.confirmService.confirm( |
114 | this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }), | |
115 | this.i18n('Delete') | |
116 | ) | |
1f30a185 C |
117 | if (res === false) return |
118 | ||
119 | this.videoService.removeVideo(video.id) | |
2186386c C |
120 | .subscribe( |
121 | status => { | |
122 | this.notificationsService.success( | |
123 | this.i18n('Success'), | |
124 | this.i18n('Video {{videoName}} deleted.', { videoName: video.name }) | |
125 | ) | |
126 | this.reloadVideos() | |
127 | }, | |
128 | ||
129 | error => this.notificationsService.error(this.i18n('Error'), error.message) | |
130 | ) | |
131 | } | |
1f30a185 | 132 | |
2186386c C |
133 | getStateLabel (video: Video) { |
134 | if (video.state.id === VideoState.PUBLISHED) return this.i18n('Published') | |
135 | ||
136 | if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) return this.i18n('Waiting transcoding') | |
137 | if (video.state.id === VideoState.TO_TRANSCODE) return this.i18n('To transcode') | |
138 | ||
139 | return this.i18n('Unknown state') | |
332542bc | 140 | } |
ce0e281d | 141 | |
a86887a4 C |
142 | protected buildVideoHeight () { |
143 | // In account videos, the video height is fixed | |
144 | return this.baseVideoHeight | |
145 | } | |
146 | ||
ce0e281d | 147 | private spliceVideosById (id: number) { |
0cd4344f | 148 | for (const key of Object.keys(this.loadedPages)) { |
2186386c | 149 | const videos = this.loadedPages[ key ] |
0cd4344f C |
150 | const index = videos.findIndex(v => v.id === id) |
151 | ||
152 | if (index !== -1) { | |
153 | videos.splice(index, 1) | |
154 | return | |
155 | } | |
156 | } | |
ce0e281d | 157 | } |
202f6b6c | 158 | } |