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