]>
Commit | Line | Data |
---|---|---|
f3aaa9a9 | 1 | import { Component, OnInit } from '@angular/core' |
be447678 | 2 | import { ActivatedRoute, Router } from '@angular/router' |
0cd4344f C |
3 | import { immutableAssign } from '@app/shared/misc/utils' |
4 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
202f6b6c | 5 | import { NotificationsService } from 'angular2-notifications' |
ce0e281d C |
6 | import 'rxjs/add/observable/from' |
7 | import 'rxjs/add/operator/concatAll' | |
8 | import { Observable } from 'rxjs/Observable' | |
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', | |
17 | templateUrl: './account-videos.component.html', | |
18 | styleUrls: [ './account-videos.component.scss' ] | |
19 | }) | |
f3aaa9a9 | 20 | export class AccountVideosComponent extends AbstractVideoList implements OnInit { |
202f6b6c C |
21 | titlePage = 'My videos' |
22 | currentRoute = '/account/videos' | |
ce0e281d | 23 | checkedVideos: { [ id: number ]: boolean } = {} |
0cd4344f C |
24 | videoHeight = 155 |
25 | videoWidth = -1 | |
26 | pagination: ComponentPagination = { | |
234b535d C |
27 | currentPage: 1, |
28 | itemsPerPage: 10, | |
29 | totalItems: null | |
30 | } | |
202f6b6c C |
31 | |
32 | constructor (protected router: Router, | |
33 | protected route: ActivatedRoute, | |
b2731bff | 34 | protected authService: AuthService, |
202f6b6c | 35 | protected notificationsService: NotificationsService, |
332542bc | 36 | protected confirmService: ConfirmService, |
202f6b6c C |
37 | private videoService: VideoService) { |
38 | super() | |
39 | } | |
40 | ||
41 | ngOnInit () { | |
42 | super.ngOnInit() | |
43 | } | |
44 | ||
ce0e281d C |
45 | abortSelectionMode () { |
46 | this.checkedVideos = {} | |
47 | } | |
48 | ||
49 | isInSelectionMode () { | |
50 | return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true) | |
51 | } | |
52 | ||
0cd4344f C |
53 | getVideosObservable (page: number) { |
54 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) | |
55 | ||
56 | return this.videoService.getMyVideos(newPagination, this.sort) | |
202f6b6c | 57 | } |
332542bc | 58 | |
1f30a185 | 59 | async deleteSelectedVideos () { |
ce0e281d C |
60 | const toDeleteVideosIds = Object.keys(this.checkedVideos) |
61 | .filter(k => this.checkedVideos[k] === true) | |
62 | .map(k => parseInt(k, 10)) | |
63 | ||
1f30a185 C |
64 | const res = await this.confirmService.confirm(`Do you really want to delete ${toDeleteVideosIds.length} videos?`, 'Delete') |
65 | if (res === false) return | |
66 | ||
67 | const observables: Observable<any>[] = [] | |
68 | for (const videoId of toDeleteVideosIds) { | |
69 | const o = this.videoService | |
70 | .removeVideo(videoId) | |
71 | .do(() => this.spliceVideosById(videoId)) | |
72 | ||
73 | observables.push(o) | |
74 | } | |
75 | ||
76 | Observable.from(observables) | |
77 | .concatAll() | |
78 | .subscribe( | |
79 | res => { | |
80 | this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`) | |
81 | this.buildVideoPages() | |
82 | }, | |
83 | ||
84 | err => this.notificationsService.error('Error', err.message) | |
85 | ) | |
ce0e281d C |
86 | } |
87 | ||
1f30a185 C |
88 | async deleteVideo (video: Video) { |
89 | const res = await this.confirmService.confirm(`Do you really want to delete ${video.name}?`, 'Delete') | |
90 | if (res === false) return | |
91 | ||
92 | this.videoService.removeVideo(video.id) | |
93 | .subscribe( | |
94 | status => { | |
95 | this.notificationsService.success('Success', `Video ${video.name} deleted.`) | |
96 | this.spliceVideosById(video.id) | |
97 | this.buildVideoPages() | |
98 | }, | |
99 | ||
100 | error => this.notificationsService.error('Error', error.message) | |
101 | ) | |
332542bc | 102 | } |
ce0e281d C |
103 | |
104 | private spliceVideosById (id: number) { | |
0cd4344f C |
105 | for (const key of Object.keys(this.loadedPages)) { |
106 | const videos = this.loadedPages[key] | |
107 | const index = videos.findIndex(v => v.id === id) | |
108 | ||
109 | if (index !== -1) { | |
110 | videos.splice(index, 1) | |
111 | return | |
112 | } | |
113 | } | |
ce0e281d | 114 | } |
202f6b6c | 115 | } |