]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-library/my-video-playlists/my-video-playlists.component.ts
Fix typo
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-library / my-video-playlists / my-video-playlists.component.ts
1 import { Subject } from 'rxjs'
2 import { mergeMap } from 'rxjs/operators'
3 import { Component } from '@angular/core'
4 import { AuthService, ComponentPagination, ConfirmService, Notifier } from '@app/core'
5 import { VideoPlaylist, VideoPlaylistService } from '@app/shared/shared-video-playlist'
6 import { VideoPlaylistType } from '@shared/models'
7
8 @Component({
9 templateUrl: './my-video-playlists.component.html',
10 styleUrls: [ './my-video-playlists.component.scss' ]
11 })
12 export class MyVideoPlaylistsComponent {
13 videoPlaylists: VideoPlaylist[] = []
14
15 pagination: ComponentPagination = {
16 currentPage: 1,
17 itemsPerPage: 5,
18 totalItems: null
19 }
20
21 onDataSubject = new Subject<any[]>()
22
23 search: string
24
25 constructor (
26 private authService: AuthService,
27 private notifier: Notifier,
28 private confirmService: ConfirmService,
29 private videoPlaylistService: VideoPlaylistService
30 ) {}
31
32 async deleteVideoPlaylist (videoPlaylist: VideoPlaylist) {
33 const res = await this.confirmService.confirm(
34 $localize`Do you really want to delete ${videoPlaylist.displayName}?`,
35 $localize`Delete`
36 )
37 if (res === false) return
38
39 this.videoPlaylistService.removeVideoPlaylist(videoPlaylist)
40 .subscribe({
41 next: () => {
42 this.videoPlaylists = this.videoPlaylists
43 .filter(p => p.id !== videoPlaylist.id)
44
45 this.notifier.success($localize`Playlist ${videoPlaylist.displayName} deleted.`)
46 },
47
48 error: err => this.notifier.error(err.message)
49 })
50 }
51
52 isRegularPlaylist (playlist: VideoPlaylist) {
53 return playlist.type.id === VideoPlaylistType.REGULAR
54 }
55
56 onNearOfBottom () {
57 // Last page
58 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
59
60 this.pagination.currentPage += 1
61 this.loadVideoPlaylists()
62 }
63
64 onSearch (search: string) {
65 this.search = search
66 this.loadVideoPlaylists(true)
67 }
68
69 private loadVideoPlaylists (reset = false) {
70 this.authService.userInformationLoaded
71 .pipe(mergeMap(() => {
72 const user = this.authService.getUser()
73
74 return this.videoPlaylistService.listAccountPlaylists(user.account, this.pagination, '-updatedAt', this.search)
75 })).subscribe(res => {
76 if (reset) this.videoPlaylists = []
77
78 this.videoPlaylists = this.videoPlaylists.concat(res.data)
79 this.pagination.totalItems = res.total
80
81 this.onDataSubject.next(res.data)
82 })
83 }
84 }