]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-update.component.ts
Begin videos of an account
[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 { VideoService } from '../../shared/video/video.service'
19 import { VideoEdit } from '../../shared/video/video-edit.model'
20
21 @Component({
22 selector: 'my-videos-update',
23 styleUrls: [ './shared/video-edit.component.scss' ],
24 templateUrl: './video-update.component.html'
25 })
26
27 export class VideoUpdateComponent extends FormReactive implements OnInit {
28 tags: string[] = []
29 videoCategories = []
30 videoLicences = []
31 videoLanguages = []
32 videoPrivacies = []
33 video: VideoEdit
34
35 tagValidators = VIDEO_TAGS.VALIDATORS
36 tagValidatorsMessages = VIDEO_TAGS.MESSAGES
37
38 error: string = null
39 form: FormGroup
40 formErrors = {
41 name: '',
42 privacy: '',
43 category: '',
44 licence: '',
45 language: '',
46 description: ''
47 }
48 validationMessages = {
49 name: VIDEO_NAME.MESSAGES,
50 privacy: VIDEO_PRIVACY.MESSAGES,
51 category: VIDEO_CATEGORY.MESSAGES,
52 licence: VIDEO_LICENCE.MESSAGES,
53 language: VIDEO_LANGUAGE.MESSAGES,
54 description: VIDEO_DESCRIPTION.MESSAGES
55 }
56
57 fileError = ''
58
59 constructor (
60 private formBuilder: FormBuilder,
61 private route: ActivatedRoute,
62 private router: Router,
63 private notificationsService: NotificationsService,
64 private serverService: ServerService,
65 private videoService: VideoService
66 ) {
67 super()
68 }
69
70 buildForm () {
71 this.form = this.formBuilder.group({
72 name: [ '', VIDEO_NAME.VALIDATORS ],
73 privacy: [ '', VIDEO_PRIVACY.VALIDATORS ],
74 nsfw: [ false ],
75 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
76 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
77 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
78 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
79 tags: [ '' ]
80 })
81
82 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
83 }
84
85 ngOnInit () {
86 this.buildForm()
87
88 this.videoCategories = this.serverService.getVideoCategories()
89 this.videoLicences = this.serverService.getVideoLicences()
90 this.videoLanguages = this.serverService.getVideoLanguages()
91 this.videoPrivacies = this.serverService.getVideoPrivacies()
92
93 const uuid: string = this.route.snapshot.params['uuid']
94
95 this.videoService.getVideo(uuid)
96 .switchMap(video => {
97 return this.videoService
98 .loadCompleteDescription(video.descriptionPath)
99 .do(description => video.description = description)
100 .map(() => video)
101 })
102 .subscribe(
103 video => {
104 this.video = new VideoEdit(video)
105
106 // We cannot set private a video that was not private anymore
107 if (video.privacy !== VideoPrivacy.PRIVATE) {
108 const newVideoPrivacies = []
109 for (const p of this.videoPrivacies) {
110 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
111 }
112
113 this.videoPrivacies = newVideoPrivacies
114 }
115
116 this.hydrateFormFromVideo()
117 },
118
119 err => {
120 console.error(err)
121 this.error = 'Cannot fetch video.'
122 }
123 )
124 }
125
126 checkForm () {
127 this.forceCheck()
128
129 return this.form.valid
130 }
131
132 update () {
133 if (this.checkForm() === false) {
134 return
135 }
136
137 this.video.patch(this.form.value)
138
139 this.videoService.updateVideo(this.video)
140 .subscribe(
141 () => {
142 this.notificationsService.success('Success', 'Video updated.')
143 this.router.navigate([ '/videos/watch', this.video.uuid ])
144 },
145
146 err => {
147 this.error = 'Cannot update the video.'
148 console.error(err)
149 }
150 )
151
152 }
153
154 private hydrateFormFromVideo () {
155 this.form.patchValue(this.video.toJSON())
156 }
157 }