]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/my-account-videos/my-account-videos.component.ts
Remove uneccessary details to link titles
[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'
c4a6f790 15import { MiniatureDisplayOptions, OwnerDisplayType } 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,
1d904806 33 itemsPerPage: 10,
234b535d
C
34 totalItems: null
35 }
e2409062
C
36 miniatureDisplayOptions: MiniatureDisplayOptions = {
37 date: true,
38 views: true,
c4a6f790 39 by: true,
e2409062
C
40 privacyLabel: false,
41 privacyText: true,
42 state: true,
43 blacklistInfo: true
44 }
c4a6f790
C
45 ownerDisplayType: OwnerDisplayType = 'videoChannel'
46
693263e9 47 videos: Video[] = []
bf64ed41
RK
48 videosSearch: string
49 videosSearchChanged = new Subject<string>()
693263e9 50 getVideosObservableFunction = this.getVideosObservable.bind(this)
202f6b6c 51
b1d40cff
C
52 constructor (
53 protected router: Router,
489290b8 54 protected serverService: ServerService,
b1d40cff
C
55 protected route: ActivatedRoute,
56 protected authService: AuthService,
f8b2c1b4 57 protected notifier: Notifier,
bbe0f064 58 protected screenService: ScreenService,
489290b8 59 private i18n: I18n,
308c4275 60 private confirmService: ConfirmService,
693263e9 61 private videoService: VideoService
b1d40cff 62 ) {
b1d40cff 63 this.titlePage = this.i18n('My videos')
202f6b6c
C
64 }
65
bf64ed41
RK
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
693263e9
C
79 disableForReuse () {
80 this.videosSelection.disableForReuse()
9af61e84
C
81 }
82
693263e9
C
83 enabledForReuse () {
84 this.videosSelection.enabledForReuse()
ce0e281d
C
85 }
86
693263e9 87 getVideosObservable (page: number, sort: VideoSortField) {
0cd4344f
C
88 const newPagination = immutableAssign(this.pagination, { currentPage: page })
89
bf64ed41 90 return this.videoService.getMyVideos(newPagination, sort, this.videosSearch)
aa0f1963
RK
91 .pipe(
92 tap(res => this.pagination.totalItems = res.total)
93 )
244e76a5
RK
94 }
95
1f30a185 96 async deleteSelectedVideos () {
693263e9
C
97 const toDeleteVideosIds = Object.keys(this.selection)
98 .filter(k => this.selection[ k ] === true)
2186386c 99 .map(k => parseInt(k, 10))
ce0e281d 100
2186386c
C
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 )
1f30a185
C
105 if (res === false) return
106
107 const observables: Observable<any>[] = []
108 for (const videoId of toDeleteVideosIds) {
2186386c 109 const o = this.videoService.removeVideo(videoId)
489290b8 110 .pipe(tap(() => this.removeVideoFromArray(videoId)))
1f30a185
C
111
112 observables.push(o)
113 }
114
489290b8
C
115 concat(...observables)
116 .pipe(toArray())
1f30a185 117 .subscribe(
489290b8 118 () => {
f8b2c1b4 119 this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }))
2186386c 120
693263e9 121 this.selection = {}
1f30a185
C
122 },
123
f8b2c1b4 124 err => this.notifier.error(err.message)
1f30a185 125 )
ce0e281d
C
126 }
127
1f30a185 128 async deleteVideo (video: Video) {
2186386c
C
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 )
1f30a185
C
133 if (res === false) return
134
135 this.videoService.removeVideo(video.id)
2186386c 136 .subscribe(
f8b2c1b4
C
137 () => {
138 this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name }))
693263e9 139 this.removeVideoFromArray(video.id)
2186386c
C
140 },
141
f8b2c1b4 142 error => this.notifier.error(error.message)
2186386c
C
143 )
144 }
1f30a185 145
74d63469
GR
146 changeOwnership (event: Event, video: Video) {
147 event.preventDefault()
148 this.videoChangeOwnershipModal.show(video)
149 }
150
489290b8
C
151 private removeVideoFromArray (id: number) {
152 this.videos = this.videos.filter(v => v.id !== id)
ce0e281d 153 }
202f6b6c 154}