]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/my-account-video-playlists/my-account-video-playlists.component.ts
fix CONTRIBUTING.md command order (#3305)
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-video-playlists / my-account-video-playlists.component.ts
CommitLineData
67ed6552 1import { Subject } from 'rxjs'
a02b93ce 2import { debounceTime, mergeMap } from 'rxjs/operators'
830b4faf 3import { Component, OnInit } from '@angular/core'
67ed6552
C
4import { AuthService, ComponentPagination, ConfirmService, Notifier, User } from '@app/core'
5import { VideoPlaylist, VideoPlaylistService } from '@app/shared/shared-video-playlist'
830b4faf
C
6import { VideoPlaylistType } from '@shared/models'
7
8@Component({
9 selector: 'my-account-video-playlists',
10 templateUrl: './my-account-video-playlists.component.html',
11 styleUrls: [ './my-account-video-playlists.component.scss' ]
12})
13export class MyAccountVideoPlaylistsComponent implements OnInit {
bf64ed41 14 videoPlaylistsSearch: string
830b4faf 15 videoPlaylists: VideoPlaylist[] = []
bf64ed41 16 videoPlaylistSearchChanged = new Subject<string>()
830b4faf
C
17
18 pagination: ComponentPagination = {
19 currentPage: 1,
ad453580 20 itemsPerPage: 5,
830b4faf
C
21 totalItems: null
22 }
23
ad453580
C
24 onDataSubject = new Subject<any[]>()
25
830b4faf
C
26 private user: User
27
28 constructor (
29 private authService: AuthService,
30 private notifier: Notifier,
31 private confirmService: ConfirmService,
66357162
C
32 private videoPlaylistService: VideoPlaylistService
33 ) {}
830b4faf
C
34
35 ngOnInit () {
36 this.user = this.authService.getUser()
37
38 this.loadVideoPlaylists()
bf64ed41
RK
39
40 this.videoPlaylistSearchChanged
41 .pipe(
42 debounceTime(500))
43 .subscribe(() => {
9d45db29 44 this.loadVideoPlaylists(true)
bf64ed41 45 })
830b4faf
C
46 }
47
48 async deleteVideoPlaylist (videoPlaylist: VideoPlaylist) {
49 const res = await this.confirmService.confirm(
66357162
C
50 $localize`Do you really want to delete ${videoPlaylist.displayName}?`,
51 $localize`Delete`
830b4faf
C
52 )
53 if (res === false) return
54
55 this.videoPlaylistService.removeVideoPlaylist(videoPlaylist)
56 .subscribe(
57 () => {
58 this.videoPlaylists = this.videoPlaylists
59 .filter(p => p.id !== videoPlaylist.id)
60
66357162 61 this.notifier.success($localize`Playlist ${videoPlaylist.displayName}} deleted.`)
830b4faf
C
62 },
63
64 error => this.notifier.error(error.message)
65 )
66 }
67
68 isRegularPlaylist (playlist: VideoPlaylist) {
69 return playlist.type.id === VideoPlaylistType.REGULAR
70 }
71
f0a39880 72 onNearOfBottom () {
830b4faf
C
73 // Last page
74 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
75
76 this.pagination.currentPage += 1
77 this.loadVideoPlaylists()
78 }
f0a39880 79
4f5d0459
RK
80 resetSearch () {
81 this.videoPlaylistsSearch = ''
82 this.onVideoPlaylistSearchChanged()
83 }
84
bf64ed41
RK
85 onVideoPlaylistSearchChanged () {
86 this.videoPlaylistSearchChanged.next()
87 }
88
9d45db29 89 private loadVideoPlaylists (reset = false) {
f0a39880 90 this.authService.userInformationLoaded
a02b93ce 91 .pipe(mergeMap(() => {
bf64ed41 92 return this.videoPlaylistService.listAccountPlaylists(this.user.account, this.pagination, '-updatedAt', this.videoPlaylistsSearch)
f3628e69 93 }))
f0a39880 94 .subscribe(res => {
9d45db29 95 if (reset) this.videoPlaylists = []
f0a39880
C
96 this.videoPlaylists = this.videoPlaylists.concat(res.data)
97 this.pagination.totalItems = res.total
ad453580
C
98
99 this.onDataSubject.next(res.data)
f0a39880
C
100 })
101 }
830b4faf 102}