]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-library/my-videos/my-videos.component.ts
Refactor horizontal margins
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-library / my-videos / my-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, User } from '@app/core'
6 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
7 import { immutableAssign } from '@app/helpers'
8 import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
9 import { LiveStreamInformationComponent } from '@app/shared/shared-video-live'
10 import { MiniatureDisplayOptions, SelectionType, VideosSelectionComponent } from '@app/shared/shared-video-miniature'
11 import { VideoSortField } from '@shared/models'
12 import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
13
14 @Component({
15 templateUrl: './my-videos.component.html',
16 styleUrls: [ './my-videos.component.scss' ]
17 })
18 export class MyVideosComponent implements OnInit, DisableForReuseHook {
19 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
20 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
21 @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent
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
40 videoActions: DropdownAction<{ video: Video }>[] = []
41
42 videos: Video[] = []
43 videosSearch: string
44 videosSearchChanged = new Subject<string>()
45 getVideosObservableFunction = this.getVideosObservable.bind(this)
46
47 user: User
48
49 constructor (
50 protected router: Router,
51 protected serverService: ServerService,
52 protected route: ActivatedRoute,
53 protected authService: AuthService,
54 protected notifier: Notifier,
55 protected screenService: ScreenService,
56 private confirmService: ConfirmService,
57 private videoService: VideoService
58 ) {
59 this.titlePage = $localize`My videos`
60 }
61
62 ngOnInit () {
63 this.buildActions()
64
65 this.user = this.authService.getUser()
66
67 this.videosSearchChanged
68 .pipe(debounceTime(500))
69 .subscribe(() => {
70 this.videosSelection.reloadVideos()
71 })
72 }
73
74 resetSearch () {
75 this.videosSearch = ''
76 this.onVideosSearchChanged()
77 }
78
79 onVideosSearchChanged () {
80 this.videosSearchChanged.next()
81 }
82
83 disableForReuse () {
84 this.videosSelection.disableForReuse()
85 }
86
87 enabledForReuse () {
88 this.videosSelection.enabledForReuse()
89 }
90
91 getVideosObservable (page: number, sort: VideoSortField) {
92 const newPagination = immutableAssign(this.pagination, { currentPage: page })
93
94 return this.videoService.getMyVideos(newPagination, sort, this.videosSearch)
95 .pipe(
96 tap(res => this.pagination.totalItems = res.total)
97 )
98 }
99
100 async deleteSelectedVideos () {
101 const toDeleteVideosIds = Object.keys(this.selection)
102 .filter(k => this.selection[ k ] === true)
103 .map(k => parseInt(k, 10))
104
105 const res = await this.confirmService.confirm(
106 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
107 $localize`Delete`
108 )
109 if (res === false) return
110
111 const observables: Observable<any>[] = []
112 for (const videoId of toDeleteVideosIds) {
113 const o = this.videoService.removeVideo(videoId)
114 .pipe(tap(() => this.removeVideoFromArray(videoId)))
115
116 observables.push(o)
117 }
118
119 concat(...observables)
120 .pipe(toArray())
121 .subscribe(
122 () => {
123 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
124 this.selection = {}
125 },
126
127 err => this.notifier.error(err.message)
128 )
129 }
130
131 async deleteVideo (video: Video) {
132 const res = await this.confirmService.confirm(
133 $localize`Do you really want to delete ${video.name}?`,
134 $localize`Delete`
135 )
136 if (res === false) return
137
138 this.videoService.removeVideo(video.id)
139 .subscribe(
140 () => {
141 this.notifier.success($localize`Video ${video.name} deleted.`)
142 this.removeVideoFromArray(video.id)
143 },
144
145 error => this.notifier.error(error.message)
146 )
147 }
148
149 changeOwnership (video: Video) {
150 this.videoChangeOwnershipModal.show(video)
151 }
152
153 displayLiveInformation (video: Video) {
154 this.liveStreamInformationModal.show(video)
155 }
156
157 private removeVideoFromArray (id: number) {
158 this.videos = this.videos.filter(v => v.id !== id)
159 }
160
161 private buildActions () {
162 this.videoActions = [
163 {
164 label: $localize`Display live information`,
165 handler: ({ video }) => this.displayLiveInformation(video),
166 isDisplayed: ({ video }) => video.isLive,
167 iconName: 'live'
168 },
169 {
170 label: $localize`Change ownership`,
171 handler: ({ video }) => this.changeOwnership(video),
172 iconName: 'ownership-change'
173 },
174 {
175 label: $localize`Delete`,
176 handler: ({ video }) => this.deleteVideo(video),
177 iconName: 'delete'
178 }
179 ]
180 }
181 }