]>
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 C |
13 | import { VideoService } from '../../shared/video/video.service' |
14 | ||
15 | @Component({ | |
16 | selector: 'my-account-videos', | |
4bb6886d C |
17 | templateUrl: './my-account-videos.component.html', |
18 | styleUrls: [ './my-account-videos.component.scss' ] | |
202f6b6c | 19 | }) |
4bb6886d | 20 | export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy { |
202f6b6c | 21 | titlePage = 'My videos' |
4bb6886d | 22 | currentRoute = '/my-account/videos' |
ce0e281d | 23 | checkedVideos: { [ id: number ]: boolean } = {} |
0cd4344f | 24 | pagination: ComponentPagination = { |
234b535d | 25 | currentPage: 1, |
2e78e268 | 26 | itemsPerPage: 5, |
234b535d C |
27 | totalItems: null |
28 | } | |
202f6b6c | 29 | |
9af61e84 C |
30 | protected baseVideoWidth = -1 |
31 | protected baseVideoHeight = 155 | |
32 | ||
202f6b6c C |
33 | constructor (protected router: Router, |
34 | protected route: ActivatedRoute, | |
b2731bff | 35 | protected authService: AuthService, |
202f6b6c | 36 | protected notificationsService: NotificationsService, |
332542bc | 37 | protected confirmService: ConfirmService, |
2a2c19df | 38 | protected location: Location, |
202f6b6c C |
39 | private videoService: VideoService) { |
40 | super() | |
41 | } | |
42 | ||
43 | ngOnInit () { | |
44 | super.ngOnInit() | |
45 | } | |
46 | ||
9af61e84 C |
47 | ngOnDestroy () { |
48 | super.ngOnDestroy() | |
49 | } | |
50 | ||
ce0e281d C |
51 | abortSelectionMode () { |
52 | this.checkedVideos = {} | |
53 | } | |
54 | ||
55 | isInSelectionMode () { | |
56 | return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true) | |
57 | } | |
58 | ||
0cd4344f C |
59 | getVideosObservable (page: number) { |
60 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) | |
61 | ||
62 | return this.videoService.getMyVideos(newPagination, this.sort) | |
202f6b6c | 63 | } |
332542bc | 64 | |
244e76a5 RK |
65 | generateSyndicationList () { |
66 | throw new Error('Method not implemented.') | |
67 | } | |
68 | ||
1f30a185 | 69 | async deleteSelectedVideos () { |
ce0e281d C |
70 | const toDeleteVideosIds = Object.keys(this.checkedVideos) |
71 | .filter(k => this.checkedVideos[k] === true) | |
72 | .map(k => parseInt(k, 10)) | |
73 | ||
1f30a185 C |
74 | const res = await this.confirmService.confirm(`Do you really want to delete ${toDeleteVideosIds.length} videos?`, 'Delete') |
75 | if (res === false) return | |
76 | ||
77 | const observables: Observable<any>[] = [] | |
78 | for (const videoId of toDeleteVideosIds) { | |
79 | const o = this.videoService | |
db400f44 C |
80 | .removeVideo(videoId) |
81 | .pipe(tap(() => this.spliceVideosById(videoId))) | |
1f30a185 C |
82 | |
83 | observables.push(o) | |
84 | } | |
85 | ||
db400f44 C |
86 | observableFrom(observables).pipe( |
87 | concatAll()) | |
1f30a185 C |
88 | .subscribe( |
89 | res => { | |
90 | this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`) | |
06be7ed0 C |
91 | this.abortSelectionMode() |
92 | this.reloadVideos() | |
1f30a185 C |
93 | }, |
94 | ||
95 | err => this.notificationsService.error('Error', err.message) | |
96 | ) | |
ce0e281d C |
97 | } |
98 | ||
1f30a185 C |
99 | async deleteVideo (video: Video) { |
100 | const res = await this.confirmService.confirm(`Do you really want to delete ${video.name}?`, 'Delete') | |
101 | if (res === false) return | |
102 | ||
103 | this.videoService.removeVideo(video.id) | |
104 | .subscribe( | |
105 | status => { | |
106 | this.notificationsService.success('Success', `Video ${video.name} deleted.`) | |
06be7ed0 | 107 | this.reloadVideos() |
1f30a185 C |
108 | }, |
109 | ||
110 | error => this.notificationsService.error('Error', error.message) | |
111 | ) | |
332542bc | 112 | } |
ce0e281d | 113 | |
a86887a4 C |
114 | protected buildVideoHeight () { |
115 | // In account videos, the video height is fixed | |
116 | return this.baseVideoHeight | |
117 | } | |
118 | ||
ce0e281d | 119 | private spliceVideosById (id: number) { |
0cd4344f C |
120 | for (const key of Object.keys(this.loadedPages)) { |
121 | const videos = this.loadedPages[key] | |
122 | const index = videos.findIndex(v => v.id === id) | |
123 | ||
124 | if (index !== -1) { | |
125 | videos.splice(index, 1) | |
126 | return | |
127 | } | |
128 | } | |
ce0e281d | 129 | } |
202f6b6c | 130 | } |