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