]>
Commit | Line | Data |
---|---|---|
830b4faf C |
1 | import { Component, OnDestroy, OnInit } from '@angular/core' |
2 | import { ActivatedRoute, Router } from '@angular/router' | |
3 | import { AuthService, Notifier, ServerService } from '@app/core' | |
4 | import { Subscription } from 'rxjs' | |
5 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
6 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
7 | import { MyAccountVideoPlaylistEdit } from '@app/+my-account/my-account-video-playlists/my-account-video-playlist-edit' | |
8 | import { populateAsyncUserVideoChannels } from '@app/shared/misc/utils' | |
9 | import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service' | |
10 | import { VideoPlaylistValidatorsService } from '@app/shared' | |
11 | import { VideoPlaylistUpdate } from '@shared/models/videos/playlist/video-playlist-update.model' | |
12 | import { VideoConstant } from '@shared/models' | |
13 | import { VideoPlaylistPrivacy } from '@shared/models/videos/playlist/video-playlist-privacy.model' | |
14 | import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model' | |
15 | ||
16 | @Component({ | |
17 | selector: 'my-account-video-playlist-update', | |
18 | templateUrl: './my-account-video-playlist-edit.component.html', | |
19 | styleUrls: [ './my-account-video-playlist-edit.component.scss' ] | |
20 | }) | |
21 | export class MyAccountVideoPlaylistUpdateComponent extends MyAccountVideoPlaylistEdit implements OnInit, OnDestroy { | |
22 | error: string | |
23 | videoPlaylistToUpdate: VideoPlaylist | |
24 | videoPlaylistPrivacies: VideoConstant<VideoPlaylistPrivacy>[] = [] | |
25 | ||
26 | private paramsSub: Subscription | |
27 | ||
28 | constructor ( | |
29 | protected formValidatorService: FormValidatorService, | |
30 | private authService: AuthService, | |
31 | private videoPlaylistValidatorsService: VideoPlaylistValidatorsService, | |
32 | private notifier: Notifier, | |
33 | private router: Router, | |
34 | private route: ActivatedRoute, | |
35 | private videoPlaylistService: VideoPlaylistService, | |
36 | private i18n: I18n, | |
37 | private serverService: ServerService | |
38 | ) { | |
39 | super() | |
40 | } | |
41 | ||
42 | ngOnInit () { | |
43 | this.buildForm({ | |
44 | 'display-name': this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DISPLAY_NAME, | |
45 | privacy: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_PRIVACY, | |
46 | description: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DESCRIPTION, | |
47 | videoChannelId: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_CHANNEL_ID, | |
48 | thumbnailfile: null | |
49 | }) | |
50 | ||
51 | populateAsyncUserVideoChannels(this.authService, this.userVideoChannels) | |
52 | ||
53 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
54 | const videoPlaylistId = routeParams['videoPlaylistId'] | |
55 | ||
56 | this.videoPlaylistService.getVideoPlaylist(videoPlaylistId).subscribe( | |
57 | videoPlaylistToUpdate => { | |
58 | this.videoPlaylistToUpdate = videoPlaylistToUpdate | |
59 | ||
60 | this.hydrateFormFromPlaylist() | |
61 | ||
62 | this.serverService.videoPlaylistPrivaciesLoaded.subscribe( | |
63 | () => { | |
64 | this.videoPlaylistPrivacies = this.serverService.getVideoPlaylistPrivacies() | |
65 | .filter(p => { | |
66 | // If the playlist is not private, we cannot put it in private anymore | |
67 | return this.videoPlaylistToUpdate.privacy.id === VideoPlaylistPrivacy.PRIVATE || | |
68 | p.id !== VideoPlaylistPrivacy.PRIVATE | |
69 | }) | |
70 | } | |
71 | ) | |
72 | }, | |
73 | ||
74 | err => this.error = err.message | |
75 | ) | |
76 | }) | |
77 | } | |
78 | ||
79 | ngOnDestroy () { | |
80 | if (this.paramsSub) this.paramsSub.unsubscribe() | |
81 | } | |
82 | ||
83 | formValidated () { | |
84 | this.error = undefined | |
85 | ||
86 | const body = this.form.value | |
87 | const videoPlaylistUpdate: VideoPlaylistUpdate = { | |
88 | displayName: body['display-name'], | |
89 | privacy: body['privacy'], | |
90 | description: body.description || null, | |
91 | videoChannelId: body.videoChannelId || null, | |
92 | thumbnailfile: body.thumbnailfile || undefined | |
93 | } | |
94 | ||
95 | this.videoPlaylistService.updateVideoPlaylist(this.videoPlaylistToUpdate, videoPlaylistUpdate).subscribe( | |
96 | () => { | |
97 | this.notifier.success( | |
98 | this.i18n('Playlist {{videoPlaylistName}} updated.', { videoPlaylistName: videoPlaylistUpdate.displayName }) | |
99 | ) | |
100 | ||
101 | this.router.navigate([ '/my-account', 'video-playlists' ]) | |
102 | }, | |
103 | ||
104 | err => this.error = err.message | |
105 | ) | |
106 | } | |
107 | ||
108 | isCreation () { | |
109 | return false | |
110 | } | |
111 | ||
112 | getFormButtonTitle () { | |
113 | return this.i18n('Update') | |
114 | } | |
115 | ||
116 | private hydrateFormFromPlaylist () { | |
117 | this.form.patchValue({ | |
118 | 'display-name': this.videoPlaylistToUpdate.displayName, | |
119 | privacy: this.videoPlaylistToUpdate.privacy.id, | |
120 | description: this.videoPlaylistToUpdate.description, | |
121 | videoChannelId: this.videoPlaylistToUpdate.videoChannel ? this.videoPlaylistToUpdate.videoChannel.id : null | |
122 | }) | |
123 | ||
124 | fetch(this.videoPlaylistToUpdate.thumbnailUrl) | |
125 | .then(response => response.blob()) | |
126 | .then(data => { | |
127 | this.form.patchValue({ | |
128 | thumbnailfile: data | |
129 | }) | |
130 | }) | |
131 | } | |
132 | } |