]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-account/my-account-videos/my-account-videos.component.ts
Reorganize client shared modules
[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(
63 debounceTime(500))
64 .subscribe(() => {
65 this.videosSelection.reloadVideos()
66 })
67 }
68
69 onVideosSearchChanged () {
70 this.videosSearchChanged.next()
71 }
72
73 disableForReuse () {
74 this.videosSelection.disableForReuse()
75 }
76
77 enabledForReuse () {
78 this.videosSelection.enabledForReuse()
79 }
80
81 getVideosObservable (page: number, sort: VideoSortField) {
82 const newPagination = immutableAssign(this.pagination, { currentPage: page })
83
84 return this.videoService.getMyVideos(newPagination, sort, this.videosSearch)
85 .pipe(
86 tap(res => this.pagination.totalItems = res.total)
87 )
88 }
89
90 async deleteSelectedVideos () {
91 const toDeleteVideosIds = Object.keys(this.selection)
92 .filter(k => this.selection[ k ] === true)
93 .map(k => parseInt(k, 10))
94
95 const res = await this.confirmService.confirm(
96 this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
97 this.i18n('Delete')
98 )
99 if (res === false) return
100
101 const observables: Observable<any>[] = []
102 for (const videoId of toDeleteVideosIds) {
103 const o = this.videoService.removeVideo(videoId)
104 .pipe(tap(() => this.removeVideoFromArray(videoId)))
105
106 observables.push(o)
107 }
108
109 concat(...observables)
110 .pipe(toArray())
111 .subscribe(
112 () => {
113 this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }))
114
115 this.selection = {}
116 },
117
118 err => this.notifier.error(err.message)
119 )
120 }
121
122 async deleteVideo (video: Video) {
123 const res = await this.confirmService.confirm(
124 this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
125 this.i18n('Delete')
126 )
127 if (res === false) return
128
129 this.videoService.removeVideo(video.id)
130 .subscribe(
131 () => {
132 this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name }))
133 this.removeVideoFromArray(video.id)
134 },
135
136 error => this.notifier.error(error.message)
137 )
138 }
139
140 changeOwnership (event: Event, video: Video) {
141 event.preventDefault()
142 this.videoChangeOwnershipModal.show(video)
143 }
144
145 private removeVideoFromArray (id: number) {
146 this.videos = this.videos.filter(v => v.id !== id)
147 }
148 }