]>
Commit | Line | Data |
---|---|---|
1 | import { from as observableFrom, Observable } from 'rxjs' | |
2 | import { concatAll, tap } from 'rxjs/operators' | |
3 | import { Component, OnDestroy, OnInit } from '@angular/core' | |
4 | import { ActivatedRoute, Router } from '@angular/router' | |
5 | import { Location } from '@angular/common' | |
6 | import { immutableAssign } from '@app/shared/misc/utils' | |
7 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
8 | import { NotificationsService } from 'angular2-notifications' | |
9 | import { AuthService } from '../../core/auth' | |
10 | import { ConfirmService } from '../../core/confirm' | |
11 | import { AbstractVideoList } from '../../shared/video/abstract-video-list' | |
12 | import { Video } from '../../shared/video/video.model' | |
13 | import { VideoService } from '../../shared/video/video.service' | |
14 | ||
15 | @Component({ | |
16 | selector: 'my-account-videos', | |
17 | templateUrl: './my-account-videos.component.html', | |
18 | styleUrls: [ './my-account-videos.component.scss' ] | |
19 | }) | |
20 | export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy { | |
21 | titlePage = 'My videos' | |
22 | currentRoute = '/my-account/videos' | |
23 | checkedVideos: { [ id: number ]: boolean } = {} | |
24 | pagination: ComponentPagination = { | |
25 | currentPage: 1, | |
26 | itemsPerPage: 5, | |
27 | totalItems: null | |
28 | } | |
29 | ||
30 | protected baseVideoWidth = -1 | |
31 | protected baseVideoHeight = 155 | |
32 | ||
33 | constructor (protected router: Router, | |
34 | protected route: ActivatedRoute, | |
35 | protected authService: AuthService, | |
36 | protected notificationsService: NotificationsService, | |
37 | protected confirmService: ConfirmService, | |
38 | protected location: Location, | |
39 | private videoService: VideoService) { | |
40 | super() | |
41 | } | |
42 | ||
43 | ngOnInit () { | |
44 | super.ngOnInit() | |
45 | } | |
46 | ||
47 | ngOnDestroy () { | |
48 | super.ngOnDestroy() | |
49 | } | |
50 | ||
51 | abortSelectionMode () { | |
52 | this.checkedVideos = {} | |
53 | } | |
54 | ||
55 | isInSelectionMode () { | |
56 | return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true) | |
57 | } | |
58 | ||
59 | getVideosObservable (page: number) { | |
60 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) | |
61 | ||
62 | return this.videoService.getMyVideos(newPagination, this.sort) | |
63 | } | |
64 | ||
65 | generateSyndicationList () { | |
66 | throw new Error('Method not implemented.') | |
67 | } | |
68 | ||
69 | async deleteSelectedVideos () { | |
70 | const toDeleteVideosIds = Object.keys(this.checkedVideos) | |
71 | .filter(k => this.checkedVideos[k] === true) | |
72 | .map(k => parseInt(k, 10)) | |
73 | ||
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 | |
80 | .removeVideo(videoId) | |
81 | .pipe(tap(() => this.spliceVideosById(videoId))) | |
82 | ||
83 | observables.push(o) | |
84 | } | |
85 | ||
86 | observableFrom(observables).pipe( | |
87 | concatAll()) | |
88 | .subscribe( | |
89 | res => { | |
90 | this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`) | |
91 | this.buildVideoPages() | |
92 | }, | |
93 | ||
94 | err => this.notificationsService.error('Error', err.message) | |
95 | ) | |
96 | } | |
97 | ||
98 | async deleteVideo (video: Video) { | |
99 | const res = await this.confirmService.confirm(`Do you really want to delete ${video.name}?`, 'Delete') | |
100 | if (res === false) return | |
101 | ||
102 | this.videoService.removeVideo(video.id) | |
103 | .subscribe( | |
104 | status => { | |
105 | this.notificationsService.success('Success', `Video ${video.name} deleted.`) | |
106 | this.spliceVideosById(video.id) | |
107 | this.buildVideoPages() | |
108 | }, | |
109 | ||
110 | error => this.notificationsService.error('Error', error.message) | |
111 | ) | |
112 | } | |
113 | ||
114 | protected buildVideoHeight () { | |
115 | // In account videos, the video height is fixed | |
116 | return this.baseVideoHeight | |
117 | } | |
118 | ||
119 | private spliceVideosById (id: number) { | |
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 | } | |
129 | } | |
130 | } |