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