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