]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/my-account-videos/my-account-videos.component.ts
Merge branch 'master' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-videos / my-account-videos.component.ts
CommitLineData
bf64ed41
RK
1import { concat, Observable, Subject } from 'rxjs'
2import { tap, toArray, debounceTime } from 'rxjs/operators'
3import { Component, ViewChild, OnInit } from '@angular/core'
be447678 4import { ActivatedRoute, Router } from '@angular/router'
0cd4344f
C
5import { immutableAssign } from '@app/shared/misc/utils'
6import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
489290b8 7import { Notifier, ServerService } from '@app/core'
b2731bff 8import { AuthService } from '../../core/auth'
332542bc 9import { ConfirmService } from '../../core/confirm'
332542bc 10import { Video } from '../../shared/video/video.model'
202f6b6c 11import { VideoService } from '../../shared/video/video.service'
b1d40cff 12import { I18n } from '@ngx-translate/i18n-polyfill'
bbe0f064 13import { ScreenService } from '@app/shared/misc/screen.service'
74d63469 14import { VideoChangeOwnershipComponent } from './video-change-ownership/video-change-ownership.component'
e2409062 15import { MiniatureDisplayOptions } from '@app/shared/video/video-miniature.component'
693263e9
C
16import { SelectionType, VideosSelectionComponent } from '@app/shared/video/videos-selection.component'
17import { VideoSortField } from '@app/shared/video/sort-field.type'
18import { 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 25export 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)
aa0f1963
RK
89 .pipe(
90 tap(res => this.pagination.totalItems = res.total)
91 )
244e76a5
RK
92 }
93
1f30a185 94 async deleteSelectedVideos () {
693263e9
C
95 const toDeleteVideosIds = Object.keys(this.selection)
96 .filter(k => this.selection[ k ] === true)
2186386c 97 .map(k => parseInt(k, 10))
ce0e281d 98
2186386c
C
99 const res = await this.confirmService.confirm(
100 this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
101 this.i18n('Delete')
102 )
1f30a185
C
103 if (res === false) return
104
105 const observables: Observable<any>[] = []
106 for (const videoId of toDeleteVideosIds) {
2186386c 107 const o = this.videoService.removeVideo(videoId)
489290b8 108 .pipe(tap(() => this.removeVideoFromArray(videoId)))
1f30a185
C
109
110 observables.push(o)
111 }
112
489290b8
C
113 concat(...observables)
114 .pipe(toArray())
1f30a185 115 .subscribe(
489290b8 116 () => {
f8b2c1b4 117 this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }))
2186386c 118
693263e9 119 this.selection = {}
1f30a185
C
120 },
121
f8b2c1b4 122 err => this.notifier.error(err.message)
1f30a185 123 )
ce0e281d
C
124 }
125
1f30a185 126 async deleteVideo (video: Video) {
2186386c
C
127 const res = await this.confirmService.confirm(
128 this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
129 this.i18n('Delete')
130 )
1f30a185
C
131 if (res === false) return
132
133 this.videoService.removeVideo(video.id)
2186386c 134 .subscribe(
f8b2c1b4
C
135 () => {
136 this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name }))
693263e9 137 this.removeVideoFromArray(video.id)
2186386c
C
138 },
139
f8b2c1b4 140 error => this.notifier.error(error.message)
2186386c
C
141 )
142 }
1f30a185 143
74d63469
GR
144 changeOwnership (event: Event, video: Video) {
145 event.preventDefault()
146 this.videoChangeOwnershipModal.show(video)
147 }
148
489290b8
C
149 private removeVideoFromArray (id: number) {
150 this.videos = this.videos.filter(v => v.id !== id)
ce0e281d 151 }
202f6b6c 152}