Add playlist channel validator when playlist is public
[oweals/peertube.git] / client / src / app / +my-account / my-account-video-playlists / my-account-video-playlist-create.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { AuthService, Notifier, ServerService } from '@app/core'
4 import { MyAccountVideoPlaylistEdit } from './my-account-video-playlist-edit'
5 import { I18n } from '@ngx-translate/i18n-polyfill'
6 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
7 import { VideoPlaylistValidatorsService } from '@app/shared'
8 import { VideoPlaylistCreate } from '@shared/models/videos/playlist/video-playlist-create.model'
9 import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
10 import { VideoConstant } from '@shared/models'
11 import { VideoPlaylistPrivacy } from '@shared/models/videos/playlist/video-playlist-privacy.model'
12 import { populateAsyncUserVideoChannels } from '@app/shared/misc/utils'
13
14 @Component({
15   selector: 'my-account-video-playlist-create',
16   templateUrl: './my-account-video-playlist-edit.component.html',
17   styleUrls: [ './my-account-video-playlist-edit.component.scss' ]
18 })
19 export class MyAccountVideoPlaylistCreateComponent extends MyAccountVideoPlaylistEdit implements OnInit {
20   error: string
21   videoPlaylistPrivacies: VideoConstant<VideoPlaylistPrivacy>[] = []
22
23   constructor (
24     protected formValidatorService: FormValidatorService,
25     private authService: AuthService,
26     private videoPlaylistValidatorsService: VideoPlaylistValidatorsService,
27     private notifier: Notifier,
28     private router: Router,
29     private videoPlaylistService: VideoPlaylistService,
30     private serverService: ServerService,
31     private i18n: I18n
32   ) {
33     super()
34   }
35
36   ngOnInit () {
37     this.buildForm({
38       displayName: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DISPLAY_NAME,
39       privacy: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_PRIVACY,
40       description: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DESCRIPTION,
41       videoChannelId: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_CHANNEL_ID,
42       thumbnailfile: null
43     })
44
45     this.form.get('privacy').valueChanges.subscribe(privacy => {
46       this.videoPlaylistValidatorsService.setChannelValidator(this.form.get('videoChannelId'), privacy)
47     })
48
49     populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
50
51     this.serverService.videoPlaylistPrivaciesLoaded.subscribe(
52       () => {
53         this.videoPlaylistPrivacies = this.serverService.getVideoPlaylistPrivacies()
54
55         this.form.patchValue({
56           privacy: VideoPlaylistPrivacy.PRIVATE
57         })
58       }
59     )
60   }
61
62   formValidated () {
63     this.error = undefined
64
65     const body = this.form.value
66     const videoPlaylistCreate: VideoPlaylistCreate = {
67       displayName: body.displayName,
68       privacy: body.privacy,
69       description: body.description || null,
70       videoChannelId: body.videoChannelId || null,
71       thumbnailfile: body.thumbnailfile || null
72     }
73
74     this.videoPlaylistService.createVideoPlaylist(videoPlaylistCreate).subscribe(
75       () => {
76         this.notifier.success(
77           this.i18n('Playlist {{playlistName}} created.', { playlistName: videoPlaylistCreate.displayName })
78         )
79         this.router.navigate([ '/my-account', 'video-playlists' ])
80       },
81
82       err => this.error = err.message
83     )
84   }
85
86   isCreation () {
87     return true
88   }
89
90   getFormButtonTitle () {
91     return this.i18n('Create')
92   }
93 }