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