]>
Commit | Line | Data |
---|---|---|
489290b8 C |
1 | import { concat, Observable } from 'rxjs' |
2 | import { tap, toArray } from 'rxjs/operators' | |
693263e9 | 3 | import { Component, ViewChild } from '@angular/core' |
be447678 | 4 | import { ActivatedRoute, Router } from '@angular/router' |
0cd4344f C |
5 | import { immutableAssign } from '@app/shared/misc/utils' |
6 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
489290b8 | 7 | import { Notifier, ServerService } from '@app/core' |
b2731bff | 8 | import { AuthService } from '../../core/auth' |
332542bc | 9 | import { ConfirmService } from '../../core/confirm' |
332542bc | 10 | import { Video } from '../../shared/video/video.model' |
202f6b6c | 11 | import { VideoService } from '../../shared/video/video.service' |
b1d40cff | 12 | import { I18n } from '@ngx-translate/i18n-polyfill' |
bbe0f064 | 13 | import { ScreenService } from '@app/shared/misc/screen.service' |
74d63469 | 14 | import { VideoChangeOwnershipComponent } from './video-change-ownership/video-change-ownership.component' |
e2409062 | 15 | import { MiniatureDisplayOptions } from '@app/shared/video/video-miniature.component' |
693263e9 C |
16 | import { SelectionType, VideosSelectionComponent } from '@app/shared/video/videos-selection.component' |
17 | import { VideoSortField } from '@app/shared/video/sort-field.type' | |
18 | import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook' | |
202f6b6c C |
19 | |
20 | @Component({ | |
21 | selector: 'my-account-videos', | |
4bb6886d C |
22 | templateUrl: './my-account-videos.component.html', |
23 | styleUrls: [ './my-account-videos.component.scss' ] | |
202f6b6c | 24 | }) |
693263e9 C |
25 | export class MyAccountVideosComponent implements DisableForReuseHook { |
26 | @ViewChild('videosSelection') videosSelection: VideosSelectionComponent | |
489290b8 C |
27 | @ViewChild('videoChangeOwnershipModal') videoChangeOwnershipModal: VideoChangeOwnershipComponent |
28 | ||
b1d40cff | 29 | titlePage: string |
693263e9 | 30 | selection: SelectionType = {} |
0cd4344f | 31 | pagination: ComponentPagination = { |
234b535d | 32 | currentPage: 1, |
2e78e268 | 33 | itemsPerPage: 5, |
234b535d C |
34 | totalItems: null |
35 | } | |
e2409062 C |
36 | miniatureDisplayOptions: MiniatureDisplayOptions = { |
37 | date: true, | |
38 | views: true, | |
39 | by: false, | |
40 | privacyLabel: false, | |
41 | privacyText: true, | |
42 | state: true, | |
43 | blacklistInfo: true | |
44 | } | |
693263e9 C |
45 | videos: Video[] = [] |
46 | getVideosObservableFunction = this.getVideosObservable.bind(this) | |
202f6b6c | 47 | |
b1d40cff C |
48 | constructor ( |
49 | protected router: Router, | |
489290b8 | 50 | protected serverService: ServerService, |
b1d40cff C |
51 | protected route: ActivatedRoute, |
52 | protected authService: AuthService, | |
f8b2c1b4 | 53 | protected notifier: Notifier, |
bbe0f064 | 54 | protected screenService: ScreenService, |
489290b8 | 55 | private i18n: I18n, |
308c4275 | 56 | private confirmService: ConfirmService, |
693263e9 | 57 | private videoService: VideoService |
b1d40cff | 58 | ) { |
b1d40cff | 59 | this.titlePage = this.i18n('My videos') |
202f6b6c C |
60 | } |
61 | ||
693263e9 C |
62 | disableForReuse () { |
63 | this.videosSelection.disableForReuse() | |
9af61e84 C |
64 | } |
65 | ||
693263e9 C |
66 | enabledForReuse () { |
67 | this.videosSelection.enabledForReuse() | |
ce0e281d C |
68 | } |
69 | ||
693263e9 | 70 | getVideosObservable (page: number, sort: VideoSortField) { |
0cd4344f C |
71 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) |
72 | ||
693263e9 | 73 | return this.videoService.getMyVideos(newPagination, sort) |
244e76a5 RK |
74 | } |
75 | ||
1f30a185 | 76 | async deleteSelectedVideos () { |
693263e9 C |
77 | const toDeleteVideosIds = Object.keys(this.selection) |
78 | .filter(k => this.selection[ k ] === true) | |
2186386c | 79 | .map(k => parseInt(k, 10)) |
ce0e281d | 80 | |
2186386c C |
81 | const res = await this.confirmService.confirm( |
82 | this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }), | |
83 | this.i18n('Delete') | |
84 | ) | |
1f30a185 C |
85 | if (res === false) return |
86 | ||
87 | const observables: Observable<any>[] = [] | |
88 | for (const videoId of toDeleteVideosIds) { | |
2186386c | 89 | const o = this.videoService.removeVideo(videoId) |
489290b8 | 90 | .pipe(tap(() => this.removeVideoFromArray(videoId))) |
1f30a185 C |
91 | |
92 | observables.push(o) | |
93 | } | |
94 | ||
489290b8 C |
95 | concat(...observables) |
96 | .pipe(toArray()) | |
1f30a185 | 97 | .subscribe( |
489290b8 | 98 | () => { |
f8b2c1b4 | 99 | this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length })) |
2186386c | 100 | |
693263e9 | 101 | this.selection = {} |
1f30a185 C |
102 | }, |
103 | ||
f8b2c1b4 | 104 | err => this.notifier.error(err.message) |
1f30a185 | 105 | ) |
ce0e281d C |
106 | } |
107 | ||
1f30a185 | 108 | async deleteVideo (video: Video) { |
2186386c C |
109 | const res = await this.confirmService.confirm( |
110 | this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }), | |
111 | this.i18n('Delete') | |
112 | ) | |
1f30a185 C |
113 | if (res === false) return |
114 | ||
115 | this.videoService.removeVideo(video.id) | |
2186386c | 116 | .subscribe( |
f8b2c1b4 C |
117 | () => { |
118 | this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name })) | |
693263e9 | 119 | this.removeVideoFromArray(video.id) |
2186386c C |
120 | }, |
121 | ||
f8b2c1b4 | 122 | error => this.notifier.error(error.message) |
2186386c C |
123 | ) |
124 | } | |
1f30a185 | 125 | |
74d63469 GR |
126 | changeOwnership (event: Event, video: Video) { |
127 | event.preventDefault() | |
128 | this.videoChangeOwnershipModal.show(video) | |
129 | } | |
130 | ||
489290b8 C |
131 | private removeVideoFromArray (id: number) { |
132 | this.videos = this.videos.filter(v => v.id !== id) | |
ce0e281d | 133 | } |
202f6b6c | 134 | } |