]>
Commit | Line | Data |
---|---|---|
1 | import { concat, Observable, Subject } from 'rxjs' | |
2 | import { tap, toArray, debounceTime } from 'rxjs/operators' | |
3 | import { Component, ViewChild, OnInit } from '@angular/core' | |
4 | import { ActivatedRoute, Router } from '@angular/router' | |
5 | import { immutableAssign } from '@app/shared/misc/utils' | |
6 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
7 | import { Notifier, ServerService } from '@app/core' | |
8 | import { AuthService } from '../../core/auth' | |
9 | import { ConfirmService } from '../../core/confirm' | |
10 | import { Video } from '../../shared/video/video.model' | |
11 | import { VideoService } from '../../shared/video/video.service' | |
12 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
13 | import { ScreenService } from '@app/shared/misc/screen.service' | |
14 | import { VideoChangeOwnershipComponent } from './video-change-ownership/video-change-ownership.component' | |
15 | import { MiniatureDisplayOptions, OwnerDisplayType } from '@app/shared/video/video-miniature.component' | |
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' | |
19 | ||
20 | @Component({ | |
21 | selector: 'my-account-videos', | |
22 | templateUrl: './my-account-videos.component.html', | |
23 | styleUrls: [ './my-account-videos.component.scss' ] | |
24 | }) | |
25 | export class MyAccountVideosComponent implements OnInit, DisableForReuseHook { | |
26 | @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent | |
27 | @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent | |
28 | ||
29 | titlePage: string | |
30 | selection: SelectionType = {} | |
31 | pagination: ComponentPagination = { | |
32 | currentPage: 1, | |
33 | itemsPerPage: 10, | |
34 | totalItems: null | |
35 | } | |
36 | miniatureDisplayOptions: MiniatureDisplayOptions = { | |
37 | date: true, | |
38 | views: true, | |
39 | by: true, | |
40 | privacyLabel: false, | |
41 | privacyText: true, | |
42 | state: true, | |
43 | blacklistInfo: true | |
44 | } | |
45 | ownerDisplayType: OwnerDisplayType = 'videoChannel' | |
46 | ||
47 | videos: Video[] = [] | |
48 | videosSearch: string | |
49 | videosSearchChanged = new Subject<string>() | |
50 | getVideosObservableFunction = this.getVideosObservable.bind(this) | |
51 | ||
52 | constructor ( | |
53 | protected router: Router, | |
54 | protected serverService: ServerService, | |
55 | protected route: ActivatedRoute, | |
56 | protected authService: AuthService, | |
57 | protected notifier: Notifier, | |
58 | protected screenService: ScreenService, | |
59 | private i18n: I18n, | |
60 | private confirmService: ConfirmService, | |
61 | private videoService: VideoService | |
62 | ) { | |
63 | this.titlePage = this.i18n('My videos') | |
64 | } | |
65 | ||
66 | ngOnInit () { | |
67 | this.videosSearchChanged | |
68 | .pipe( | |
69 | debounceTime(500)) | |
70 | .subscribe(() => { | |
71 | this.videosSelection.reloadVideos() | |
72 | }) | |
73 | } | |
74 | ||
75 | onVideosSearchChanged () { | |
76 | this.videosSearchChanged.next() | |
77 | } | |
78 | ||
79 | disableForReuse () { | |
80 | this.videosSelection.disableForReuse() | |
81 | } | |
82 | ||
83 | enabledForReuse () { | |
84 | this.videosSelection.enabledForReuse() | |
85 | } | |
86 | ||
87 | getVideosObservable (page: number, sort: VideoSortField) { | |
88 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) | |
89 | ||
90 | return this.videoService.getMyVideos(newPagination, sort, this.videosSearch) | |
91 | .pipe( | |
92 | tap(res => this.pagination.totalItems = res.total) | |
93 | ) | |
94 | } | |
95 | ||
96 | async deleteSelectedVideos () { | |
97 | const toDeleteVideosIds = Object.keys(this.selection) | |
98 | .filter(k => this.selection[ k ] === true) | |
99 | .map(k => parseInt(k, 10)) | |
100 | ||
101 | const res = await this.confirmService.confirm( | |
102 | this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }), | |
103 | this.i18n('Delete') | |
104 | ) | |
105 | if (res === false) return | |
106 | ||
107 | const observables: Observable<any>[] = [] | |
108 | for (const videoId of toDeleteVideosIds) { | |
109 | const o = this.videoService.removeVideo(videoId) | |
110 | .pipe(tap(() => this.removeVideoFromArray(videoId))) | |
111 | ||
112 | observables.push(o) | |
113 | } | |
114 | ||
115 | concat(...observables) | |
116 | .pipe(toArray()) | |
117 | .subscribe( | |
118 | () => { | |
119 | this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length })) | |
120 | ||
121 | this.selection = {} | |
122 | }, | |
123 | ||
124 | err => this.notifier.error(err.message) | |
125 | ) | |
126 | } | |
127 | ||
128 | async deleteVideo (video: Video) { | |
129 | const res = await this.confirmService.confirm( | |
130 | this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }), | |
131 | this.i18n('Delete') | |
132 | ) | |
133 | if (res === false) return | |
134 | ||
135 | this.videoService.removeVideo(video.id) | |
136 | .subscribe( | |
137 | () => { | |
138 | this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name })) | |
139 | this.removeVideoFromArray(video.id) | |
140 | }, | |
141 | ||
142 | error => this.notifier.error(error.message) | |
143 | ) | |
144 | } | |
145 | ||
146 | changeOwnership (event: Event, video: Video) { | |
147 | event.preventDefault() | |
148 | this.videoChangeOwnershipModal.show(video) | |
149 | } | |
150 | ||
151 | private removeVideoFromArray (id: number) { | |
152 | this.videos = this.videos.filter(v => v.id !== id) | |
153 | } | |
154 | } |