]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit } from '@angular/core' | |
2 | import { ActivatedRoute, Router } from '@angular/router' | |
3 | import { NotificationsService } from 'angular2-notifications' | |
4 | import 'rxjs/add/observable/from' | |
5 | import 'rxjs/add/operator/concatAll' | |
6 | import { Observable } from 'rxjs/Observable' | |
7 | import { AuthService } from '../../core/auth' | |
8 | import { ConfirmService } from '../../core/confirm' | |
9 | import { AbstractVideoList } from '../../shared/video/abstract-video-list' | |
10 | import { Video } from '../../shared/video/video.model' | |
11 | import { VideoService } from '../../shared/video/video.service' | |
12 | ||
13 | @Component({ | |
14 | selector: 'my-account-videos', | |
15 | templateUrl: './account-videos.component.html', | |
16 | styleUrls: [ './account-videos.component.scss' ] | |
17 | }) | |
18 | export class AccountVideosComponent extends AbstractVideoList implements OnInit { | |
19 | titlePage = 'My videos' | |
20 | currentRoute = '/account/videos' | |
21 | checkedVideos: { [ id: number ]: boolean } = {} | |
22 | ||
23 | constructor (protected router: Router, | |
24 | protected route: ActivatedRoute, | |
25 | protected authService: AuthService, | |
26 | protected notificationsService: NotificationsService, | |
27 | protected confirmService: ConfirmService, | |
28 | private videoService: VideoService) { | |
29 | super() | |
30 | } | |
31 | ||
32 | ngOnInit () { | |
33 | super.ngOnInit() | |
34 | } | |
35 | ||
36 | abortSelectionMode () { | |
37 | this.checkedVideos = {} | |
38 | } | |
39 | ||
40 | isInSelectionMode () { | |
41 | return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true) | |
42 | } | |
43 | ||
44 | getVideosObservable () { | |
45 | return this.videoService.getMyVideos(this.pagination, this.sort) | |
46 | } | |
47 | ||
48 | deleteSelectedVideos () { | |
49 | const toDeleteVideosIds = Object.keys(this.checkedVideos) | |
50 | .filter(k => this.checkedVideos[k] === true) | |
51 | .map(k => parseInt(k, 10)) | |
52 | ||
53 | this.confirmService.confirm(`Do you really want to delete ${toDeleteVideosIds.length} videos?`, 'Delete').subscribe( | |
54 | res => { | |
55 | if (res === false) return | |
56 | ||
57 | const observables: Observable<any>[] = [] | |
58 | for (const videoId of toDeleteVideosIds) { | |
59 | const o = this.videoService | |
60 | .removeVideo(videoId) | |
61 | .do(() => this.spliceVideosById(videoId)) | |
62 | ||
63 | observables.push(o) | |
64 | } | |
65 | ||
66 | Observable.from(observables) | |
67 | .concatAll() | |
68 | .subscribe( | |
69 | res => this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`), | |
70 | ||
71 | err => this.notificationsService.error('Error', err.message) | |
72 | ) | |
73 | } | |
74 | ) | |
75 | } | |
76 | ||
77 | deleteVideo (video: Video) { | |
78 | this.confirmService.confirm(`Do you really want to delete ${video.name}?`, 'Delete').subscribe( | |
79 | res => { | |
80 | if (res === false) return | |
81 | ||
82 | this.videoService.removeVideo(video.id) | |
83 | .subscribe( | |
84 | status => { | |
85 | this.notificationsService.success('Success', `Video ${video.name} deleted.`) | |
86 | this.spliceVideosById(video.id) | |
87 | }, | |
88 | ||
89 | error => this.notificationsService.error('Error', error.message) | |
90 | ) | |
91 | } | |
92 | ) | |
93 | } | |
94 | ||
95 | private spliceVideosById (id: number) { | |
96 | const index = this.videos.findIndex(v => v.id === id) | |
97 | this.videos.splice(index, 1) | |
98 | } | |
99 | } |