]>
Commit | Line | Data |
---|---|---|
bf64ed41 RK |
1 | import { concat, Observable, Subject } from 'rxjs' |
2 | import { tap, toArray, debounceTime } from 'rxjs/operators' | |
3 | import { Component, ViewChild, OnInit } 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 | }) |
bf64ed41 | 25 | export class MyAccountVideosComponent implements OnInit, DisableForReuseHook { |
f36da21e C |
26 | @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent |
27 | @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent | |
489290b8 | 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 | 45 | videos: Video[] = [] |
bf64ed41 RK |
46 | videosSearch: string |
47 | videosSearchChanged = new Subject<string>() | |
693263e9 | 48 | getVideosObservableFunction = this.getVideosObservable.bind(this) |
202f6b6c | 49 | |
b1d40cff C |
50 | constructor ( |
51 | protected router: Router, | |
489290b8 | 52 | protected serverService: ServerService, |
b1d40cff C |
53 | protected route: ActivatedRoute, |
54 | protected authService: AuthService, | |
f8b2c1b4 | 55 | protected notifier: Notifier, |
bbe0f064 | 56 | protected screenService: ScreenService, |
489290b8 | 57 | private i18n: I18n, |
308c4275 | 58 | private confirmService: ConfirmService, |
693263e9 | 59 | private videoService: VideoService |
b1d40cff | 60 | ) { |
b1d40cff | 61 | this.titlePage = this.i18n('My videos') |
202f6b6c C |
62 | } |
63 | ||
bf64ed41 RK |
64 | ngOnInit () { |
65 | this.videosSearchChanged | |
66 | .pipe( | |
67 | debounceTime(500)) | |
68 | .subscribe(() => { | |
69 | this.videosSelection.reloadVideos() | |
70 | }) | |
71 | } | |
72 | ||
73 | onVideosSearchChanged () { | |
74 | this.videosSearchChanged.next() | |
75 | } | |
76 | ||
693263e9 C |
77 | disableForReuse () { |
78 | this.videosSelection.disableForReuse() | |
9af61e84 C |
79 | } |
80 | ||
693263e9 C |
81 | enabledForReuse () { |
82 | this.videosSelection.enabledForReuse() | |
ce0e281d C |
83 | } |
84 | ||
693263e9 | 85 | getVideosObservable (page: number, sort: VideoSortField) { |
0cd4344f C |
86 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) |
87 | ||
bf64ed41 | 88 | return this.videoService.getMyVideos(newPagination, sort, this.videosSearch) |
244e76a5 RK |
89 | } |
90 | ||
1f30a185 | 91 | async deleteSelectedVideos () { |
693263e9 C |
92 | const toDeleteVideosIds = Object.keys(this.selection) |
93 | .filter(k => this.selection[ k ] === true) | |
2186386c | 94 | .map(k => parseInt(k, 10)) |
ce0e281d | 95 | |
2186386c C |
96 | const res = await this.confirmService.confirm( |
97 | this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }), | |
98 | this.i18n('Delete') | |
99 | ) | |
1f30a185 C |
100 | if (res === false) return |
101 | ||
102 | const observables: Observable<any>[] = [] | |
103 | for (const videoId of toDeleteVideosIds) { | |
2186386c | 104 | const o = this.videoService.removeVideo(videoId) |
489290b8 | 105 | .pipe(tap(() => this.removeVideoFromArray(videoId))) |
1f30a185 C |
106 | |
107 | observables.push(o) | |
108 | } | |
109 | ||
489290b8 C |
110 | concat(...observables) |
111 | .pipe(toArray()) | |
1f30a185 | 112 | .subscribe( |
489290b8 | 113 | () => { |
f8b2c1b4 | 114 | this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length })) |
2186386c | 115 | |
693263e9 | 116 | this.selection = {} |
1f30a185 C |
117 | }, |
118 | ||
f8b2c1b4 | 119 | err => this.notifier.error(err.message) |
1f30a185 | 120 | ) |
ce0e281d C |
121 | } |
122 | ||
1f30a185 | 123 | async deleteVideo (video: Video) { |
2186386c C |
124 | const res = await this.confirmService.confirm( |
125 | this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }), | |
126 | this.i18n('Delete') | |
127 | ) | |
1f30a185 C |
128 | if (res === false) return |
129 | ||
130 | this.videoService.removeVideo(video.id) | |
2186386c | 131 | .subscribe( |
f8b2c1b4 C |
132 | () => { |
133 | this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name })) | |
693263e9 | 134 | this.removeVideoFromArray(video.id) |
2186386c C |
135 | }, |
136 | ||
f8b2c1b4 | 137 | error => this.notifier.error(error.message) |
2186386c C |
138 | ) |
139 | } | |
1f30a185 | 140 | |
74d63469 GR |
141 | changeOwnership (event: Event, video: Video) { |
142 | event.preventDefault() | |
143 | this.videoChangeOwnershipModal.show(video) | |
144 | } | |
145 | ||
489290b8 C |
146 | private removeVideoFromArray (id: number) { |
147 | this.videos = this.videos.filter(v => v.id !== id) | |
ce0e281d | 148 | } |
202f6b6c | 149 | } |