]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-update.component.ts
01ab0a7163732d79492819f9651f8f6da63e220d
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-update.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { NotificationsService } from 'angular2-notifications'
5 import 'rxjs/add/observable/forkJoin'
6 import { VideoPrivacy } from '../../../../../shared/models/videos/video-privacy.enum'
7 import { ServerService } from '../../core'
8 import {
9 FormReactive,
10 VIDEO_CATEGORY,
11 VIDEO_DESCRIPTION,
12 VIDEO_LANGUAGE,
13 VIDEO_LICENCE,
14 VIDEO_NAME,
15 VIDEO_PRIVACY,
16 VIDEO_TAGS
17 } from '../../shared'
18 import { ValidatorMessage } from '../../shared/forms/form-validators'
19 import { VideoService } from '../../shared/video/video.service'
20 import { VideoEdit } from '../../shared/video/video-edit.model'
21
22 @Component({
23 selector: 'my-videos-update',
24 styleUrls: [ './shared/video-edit.component.scss' ],
25 templateUrl: './video-update.component.html'
26 })
27
28 export class VideoUpdateComponent extends FormReactive implements OnInit {
29 video: VideoEdit
30
31 error: string = null
32 form: FormGroup
33 formErrors: { [ id: string ]: string } = {}
34 validationMessages: ValidatorMessage = {}
35 videoPrivacies = []
36
37 fileError = ''
38
39 constructor (
40 private formBuilder: FormBuilder,
41 private route: ActivatedRoute,
42 private router: Router,
43 private notificationsService: NotificationsService,
44 private serverService: ServerService,
45 private videoService: VideoService
46 ) {
47 super()
48 }
49
50 buildForm () {
51 this.form = this.formBuilder.group({})
52 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
53 }
54
55 ngOnInit () {
56 this.buildForm()
57
58 this.videoPrivacies = this.serverService.getVideoPrivacies()
59
60 const uuid: string = this.route.snapshot.params['uuid']
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
72 // We cannot set private a video that was not private
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
82 this.hydrateFormFromVideo()
83 },
84
85 err => {
86 console.error(err)
87 this.error = 'Cannot fetch video.'
88 }
89 )
90 }
91
92 checkForm () {
93 this.forceCheck()
94
95 return this.form.valid
96 }
97
98 update () {
99 if (this.checkForm() === false) {
100 return
101 }
102
103 this.video.patch(this.form.value)
104
105 this.videoService.updateVideo(this.video)
106 .subscribe(
107 () => {
108 this.notificationsService.success('Success', 'Video updated.')
109 this.router.navigate([ '/videos/watch', this.video.uuid ])
110 },
111
112 err => {
113 this.error = 'Cannot update the video.'
114 console.error(err)
115 }
116 )
117
118 }
119
120 private hydrateFormFromVideo () {
121 this.form.patchValue(this.video.toJSON())
122 }
123 }