]>
Commit | Line | Data |
---|---|---|
f3aaa9a9 | 1 | import { Component, OnInit } from '@angular/core' |
be447678 | 2 | import { ActivatedRoute, Router } from '@angular/router' |
202f6b6c | 3 | import { NotificationsService } from 'angular2-notifications' |
ce0e281d C |
4 | import 'rxjs/add/observable/from' |
5 | import 'rxjs/add/operator/concatAll' | |
6 | import { Observable } from 'rxjs/Observable' | |
b2731bff | 7 | import { AuthService } from '../../core/auth' |
332542bc | 8 | import { ConfirmService } from '../../core/confirm' |
be447678 | 9 | import { AbstractVideoList } from '../../shared/video/abstract-video-list' |
332542bc | 10 | import { Video } from '../../shared/video/video.model' |
202f6b6c C |
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 | }) | |
f3aaa9a9 | 18 | export class AccountVideosComponent extends AbstractVideoList implements OnInit { |
202f6b6c C |
19 | titlePage = 'My videos' |
20 | currentRoute = '/account/videos' | |
ce0e281d | 21 | checkedVideos: { [ id: number ]: boolean } = {} |
202f6b6c C |
22 | |
23 | constructor (protected router: Router, | |
24 | protected route: ActivatedRoute, | |
b2731bff | 25 | protected authService: AuthService, |
202f6b6c | 26 | protected notificationsService: NotificationsService, |
332542bc | 27 | protected confirmService: ConfirmService, |
202f6b6c C |
28 | private videoService: VideoService) { |
29 | super() | |
30 | } | |
31 | ||
32 | ngOnInit () { | |
33 | super.ngOnInit() | |
34 | } | |
35 | ||
ce0e281d C |
36 | abortSelectionMode () { |
37 | this.checkedVideos = {} | |
38 | } | |
39 | ||
40 | isInSelectionMode () { | |
41 | return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true) | |
42 | } | |
43 | ||
202f6b6c C |
44 | getVideosObservable () { |
45 | return this.videoService.getMyVideos(this.pagination, this.sort) | |
46 | } | |
332542bc | 47 | |
ce0e281d C |
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.text) | |
72 | ) | |
73 | } | |
74 | ) | |
75 | } | |
76 | ||
332542bc C |
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.`) | |
ce0e281d | 86 | this.spliceVideosById(video.id) |
332542bc C |
87 | }, |
88 | ||
89 | error => this.notificationsService.error('Error', error.text) | |
90 | ) | |
91 | } | |
92 | ) | |
93 | } | |
ce0e281d C |
94 | |
95 | private spliceVideosById (id: number) { | |
96 | const index = this.videos.findIndex(v => v.id === id) | |
97 | this.videos.splice(index, 1) | |
98 | } | |
202f6b6c | 99 | } |