]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/my-account-videos/my-account-videos.component.ts
harmonize search for libraries
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-videos / my-account-videos.component.ts
CommitLineData
bf64ed41 1import { concat, Observable, Subject } from 'rxjs'
67ed6552
C
2import { debounceTime, tap, toArray } from 'rxjs/operators'
3import { Component, OnInit, ViewChild } from '@angular/core'
be447678 4import { ActivatedRoute, Router } from '@angular/router'
67ed6552
C
5import { AuthService, ComponentPagination, ConfirmService, Notifier, ScreenService, ServerService } from '@app/core'
6import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
7import { immutableAssign } from '@app/helpers'
8import { Video, VideoService } from '@app/shared/shared-main'
9import { MiniatureDisplayOptions, OwnerDisplayType, SelectionType, VideosSelectionComponent } from '@app/shared/shared-video-miniature'
b1d40cff 10import { I18n } from '@ngx-translate/i18n-polyfill'
67ed6552 11import { VideoSortField } from '@shared/models'
74d63469 12import { VideoChangeOwnershipComponent } from './video-change-ownership/video-change-ownership.component'
202f6b6c
C
13
14@Component({
15 selector: 'my-account-videos',
4bb6886d
C
16 templateUrl: './my-account-videos.component.html',
17 styleUrls: [ './my-account-videos.component.scss' ]
202f6b6c 18})
bf64ed41 19export class MyAccountVideosComponent implements OnInit, DisableForReuseHook {
f36da21e
C
20 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
21 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
489290b8 22
b1d40cff 23 titlePage: string
693263e9 24 selection: SelectionType = {}
0cd4344f 25 pagination: ComponentPagination = {
234b535d 26 currentPage: 1,
1d904806 27 itemsPerPage: 10,
234b535d
C
28 totalItems: null
29 }
e2409062
C
30 miniatureDisplayOptions: MiniatureDisplayOptions = {
31 date: true,
32 views: true,
c4a6f790 33 by: true,
e2409062
C
34 privacyLabel: false,
35 privacyText: true,
36 state: true,
37 blacklistInfo: true
38 }
c4a6f790
C
39 ownerDisplayType: OwnerDisplayType = 'videoChannel'
40
693263e9 41 videos: Video[] = []
bf64ed41
RK
42 videosSearch: string
43 videosSearchChanged = new Subject<string>()
693263e9 44 getVideosObservableFunction = this.getVideosObservable.bind(this)
202f6b6c 45
b1d40cff
C
46 constructor (
47 protected router: Router,
489290b8 48 protected serverService: ServerService,
b1d40cff
C
49 protected route: ActivatedRoute,
50 protected authService: AuthService,
f8b2c1b4 51 protected notifier: Notifier,
bbe0f064 52 protected screenService: ScreenService,
489290b8 53 private i18n: I18n,
308c4275 54 private confirmService: ConfirmService,
693263e9 55 private videoService: VideoService
b1d40cff 56 ) {
b1d40cff 57 this.titlePage = this.i18n('My videos')
202f6b6c
C
58 }
59
bf64ed41
RK
60 ngOnInit () {
61 this.videosSearchChanged
4f5d0459 62 .pipe(debounceTime(500))
bf64ed41
RK
63 .subscribe(() => {
64 this.videosSelection.reloadVideos()
65 })
66 }
67
4f5d0459
RK
68 resetSearch () {
69 this.videosSearch = ''
70 this.onVideosSearchChanged()
71 }
72
bf64ed41
RK
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}