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