]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-update.component.ts
Move video form inside a component
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-update.component.ts
CommitLineData
db7af09b 1import { Component, OnInit } from '@angular/core'
df98563e
C
2import { FormBuilder, FormGroup } from '@angular/forms'
3import { ActivatedRoute, Router } from '@angular/router'
df98563e 4import { NotificationsService } from 'angular2-notifications'
202f6b6c
C
5import 'rxjs/add/observable/forkJoin'
6import { VideoPrivacy } from '../../../../../shared/models/videos/video-privacy.enum'
db7af09b 7import { ServerService } from '../../core'
6e07c3de
C
8import {
9 FormReactive,
6e07c3de
C
10 VIDEO_CATEGORY,
11 VIDEO_DESCRIPTION,
202f6b6c
C
12 VIDEO_LANGUAGE,
13 VIDEO_LICENCE,
14 VIDEO_NAME,
15 VIDEO_PRIVACY,
16 VIDEO_TAGS
df98563e 17} from '../../shared'
ff249f49 18import { ValidatorMessage } from '../../shared/forms/form-validators'
202f6b6c
C
19import { VideoService } from '../../shared/video/video.service'
20import { VideoEdit } from '../../shared/video/video-edit.model'
1553e15d 21
dc8bc31b 22@Component({
d8e689b8 23 selector: 'my-videos-update',
80958c78 24 styleUrls: [ './shared/video-edit.component.scss' ],
d8e689b8 25 templateUrl: './video-update.component.html'
dc8bc31b
C
26})
27
d8e689b8 28export class VideoUpdateComponent extends FormReactive implements OnInit {
404b54e1 29 video: VideoEdit
4b2f33f3 30
df98563e
C
31 error: string = null
32 form: FormGroup
ff249f49
C
33 formErrors: { [ id: string ]: string } = {}
34 validationMessages: ValidatorMessage = {}
35 videoPrivacies = []
dc8bc31b 36
df98563e 37 fileError = ''
bf57d5ee 38
df98563e 39 constructor (
4b2f33f3 40 private formBuilder: FormBuilder,
d8e689b8 41 private route: ActivatedRoute,
7ddd02c9 42 private router: Router,
6e07c3de 43 private notificationsService: NotificationsService,
db7af09b 44 private serverService: ServerService,
6e07c3de 45 private videoService: VideoService
4b2f33f3 46 ) {
df98563e 47 super()
4b2f33f3 48 }
dc8bc31b 49
df98563e 50 buildForm () {
ff249f49 51 this.form = this.formBuilder.group({})
df98563e 52 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
e822fdae
C
53 }
54
df98563e
C
55 ngOnInit () {
56 this.buildForm()
d8e689b8 57
fd45e8f4 58 this.videoPrivacies = this.serverService.getVideoPrivacies()
6e07c3de 59
0a6658fd 60 const uuid: string = this.route.snapshot.params['uuid']
2de96f4d
C
61 this.videoService.getVideo(uuid)
62 .switchMap(video => {
63 return this.videoService
64 .loadCompleteDescription(video.descriptionPath)
65 .do(description => video.description = description)
66 .map(() => video)
67 })
68 .subscribe(
69 video => {
70 this.video = new VideoEdit(video)
71
ff249f49 72 // We cannot set private a video that was not private
fd45e8f4
C
73 if (video.privacy !== VideoPrivacy.PRIVATE) {
74 const newVideoPrivacies = []
75 for (const p of this.videoPrivacies) {
76 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
77 }
78
79 this.videoPrivacies = newVideoPrivacies
80 }
81
2de96f4d
C
82 this.hydrateFormFromVideo()
83 },
84
85 err => {
86 console.error(err)
87 this.error = 'Cannot fetch video.'
88 }
89 )
e822fdae
C
90 }
91
df98563e
C
92 checkForm () {
93 this.forceCheck()
c24ac1c1 94
df98563e 95 return this.form.valid
c24ac1c1
C
96 }
97
df98563e 98 update () {
c24ac1c1 99 if (this.checkForm() === false) {
df98563e 100 return
c24ac1c1
C
101 }
102
df98563e 103 this.video.patch(this.form.value)
d8e689b8
C
104
105 this.videoService.updateVideo(this.video)
106 .subscribe(
107 () => {
df98563e 108 this.notificationsService.success('Success', 'Video updated.')
0a6658fd 109 this.router.navigate([ '/videos/watch', this.video.uuid ])
d8e689b8
C
110 },
111
112 err => {
df98563e
C
113 this.error = 'Cannot update the video.'
114 console.error(err)
d8e689b8 115 }
df98563e 116 )
e822fdae 117
dc8bc31b 118 }
e54163c2 119
df98563e
C
120 private hydrateFormFromVideo () {
121 this.form.patchValue(this.video.toJSON())
d8e689b8 122 }
dc8bc31b 123}