]>
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' |
202f6b6c C |
15 | |
16 | @Component({ | |
17 | selector: 'my-account-videos', | |
4bb6886d C |
18 | templateUrl: './my-account-videos.component.html', |
19 | styleUrls: [ './my-account-videos.component.scss' ] | |
202f6b6c | 20 | }) |
4bb6886d | 21 | export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy { |
b1d40cff | 22 | titlePage: string |
4bb6886d | 23 | currentRoute = '/my-account/videos' |
ce0e281d | 24 | checkedVideos: { [ id: number ]: boolean } = {} |
0cd4344f | 25 | pagination: ComponentPagination = { |
234b535d | 26 | currentPage: 1, |
2e78e268 | 27 | itemsPerPage: 5, |
234b535d C |
28 | totalItems: null |
29 | } | |
202f6b6c | 30 | |
9af61e84 C |
31 | protected baseVideoWidth = -1 |
32 | protected baseVideoHeight = 155 | |
33 | ||
b1d40cff C |
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 | ) { | |
202f6b6c | 44 | super() |
b1d40cff C |
45 | |
46 | this.titlePage = this.i18n('My videos') | |
202f6b6c C |
47 | } |
48 | ||
49 | ngOnInit () { | |
50 | super.ngOnInit() | |
51 | } | |
52 | ||
9af61e84 C |
53 | ngOnDestroy () { |
54 | super.ngOnDestroy() | |
55 | } | |
56 | ||
ce0e281d C |
57 | abortSelectionMode () { |
58 | this.checkedVideos = {} | |
59 | } | |
60 | ||
61 | isInSelectionMode () { | |
62 | return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true) | |
63 | } | |
64 | ||
0cd4344f C |
65 | getVideosObservable (page: number) { |
66 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) | |
67 | ||
68 | return this.videoService.getMyVideos(newPagination, this.sort) | |
202f6b6c | 69 | } |
332542bc | 70 | |
244e76a5 RK |
71 | generateSyndicationList () { |
72 | throw new Error('Method not implemented.') | |
73 | } | |
74 | ||
1f30a185 | 75 | async deleteSelectedVideos () { |
ce0e281d C |
76 | const toDeleteVideosIds = Object.keys(this.checkedVideos) |
77 | .filter(k => this.checkedVideos[k] === true) | |
78 | .map(k => parseInt(k, 10)) | |
79 | ||
1f30a185 C |
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 | |
db400f44 C |
86 | .removeVideo(videoId) |
87 | .pipe(tap(() => this.spliceVideosById(videoId))) | |
1f30a185 C |
88 | |
89 | observables.push(o) | |
90 | } | |
91 | ||
db400f44 C |
92 | observableFrom(observables).pipe( |
93 | concatAll()) | |
1f30a185 C |
94 | .subscribe( |
95 | res => { | |
96 | this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`) | |
06be7ed0 C |
97 | this.abortSelectionMode() |
98 | this.reloadVideos() | |
1f30a185 C |
99 | }, |
100 | ||
101 | err => this.notificationsService.error('Error', err.message) | |
102 | ) | |
ce0e281d C |
103 | } |
104 | ||
1f30a185 C |
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.`) | |
06be7ed0 | 113 | this.reloadVideos() |
1f30a185 C |
114 | }, |
115 | ||
116 | error => this.notificationsService.error('Error', error.message) | |
117 | ) | |
332542bc | 118 | } |
ce0e281d | 119 | |
a86887a4 C |
120 | protected buildVideoHeight () { |
121 | // In account videos, the video height is fixed | |
122 | return this.baseVideoHeight | |
123 | } | |
124 | ||
ce0e281d | 125 | private spliceVideosById (id: number) { |
0cd4344f C |
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 | } | |
ce0e281d | 135 | } |
202f6b6c | 136 | } |