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