]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add.component.ts
Display error message in signup page (#128)
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add.component.ts
CommitLineData
bfb3a98f 1import { Component, OnInit, ViewChild } from '@angular/core'
df98563e
C
2import { FormBuilder, FormGroup } from '@angular/forms'
3import { Router } from '@angular/router'
dc8bc31b 4
df98563e 5import { NotificationsService } from 'angular2-notifications'
8140a704 6
6e07c3de
C
7import {
8 FormReactive,
9 VIDEO_NAME,
10 VIDEO_CATEGORY,
d07137b9 11 VIDEO_LICENCE,
db216afd 12 VIDEO_LANGUAGE,
6e07c3de 13 VIDEO_DESCRIPTION,
db7af09b 14 VIDEO_TAGS,
bcd9f81e 15 VIDEO_CHANNEL,
fd45e8f4
C
16 VIDEO_FILE,
17 VIDEO_PRIVACY
df98563e 18} from '../../shared'
bcd9f81e 19import { AuthService, ServerService } from '../../core'
df98563e 20import { VideoService } from '../shared'
4771e000 21import { VideoCreate } from '../../../../../shared'
bfb3a98f 22import { HttpEventType, HttpResponse } from '@angular/common/http'
1553e15d 23
dc8bc31b
C
24@Component({
25 selector: 'my-videos-add',
d8e689b8 26 styleUrls: [ './video-edit.component.scss' ],
ec8d8440 27 templateUrl: './video-add.component.html'
dc8bc31b
C
28})
29
4b2f33f3 30export class VideoAddComponent extends FormReactive implements OnInit {
bfb3a98f
C
31 @ViewChild('videofileInput') videofileInput
32
33 progressPercent = 0
df98563e 34 tags: string[] = []
df98563e
C
35 videoCategories = []
36 videoLicences = []
37 videoLanguages = []
fd45e8f4 38 videoPrivacies = []
bcd9f81e 39 userVideoChannels = []
4b2f33f3 40
df98563e
C
41 tagValidators = VIDEO_TAGS.VALIDATORS
42 tagValidatorsMessages = VIDEO_TAGS.MESSAGES
3758da94 43
bfb3a98f 44 error: string
df98563e 45 form: FormGroup
4b2f33f3 46 formErrors = {
e822fdae 47 name: '',
fd45e8f4 48 privacy: '',
6e07c3de 49 category: '',
d07137b9 50 licence: '',
db216afd 51 language: '',
bcd9f81e 52 channelId: '',
bfb3a98f
C
53 description: '',
54 videofile: ''
df98563e 55 }
4b2f33f3
C
56 validationMessages = {
57 name: VIDEO_NAME.MESSAGES,
fd45e8f4 58 privacy: VIDEO_PRIVACY.MESSAGES,
6e07c3de 59 category: VIDEO_CATEGORY.MESSAGES,
d07137b9 60 licence: VIDEO_LICENCE.MESSAGES,
db216afd 61 language: VIDEO_LANGUAGE.MESSAGES,
bcd9f81e 62 channelId: VIDEO_CHANNEL.MESSAGES,
bfb3a98f
C
63 description: VIDEO_DESCRIPTION.MESSAGES,
64 videofile: VIDEO_FILE.MESSAGES
df98563e 65 }
dc8bc31b 66
df98563e 67 constructor (
4b2f33f3 68 private formBuilder: FormBuilder,
7ddd02c9 69 private router: Router,
6e07c3de 70 private notificationsService: NotificationsService,
bcd9f81e 71 private authService: AuthService,
db7af09b 72 private serverService: ServerService,
6e07c3de 73 private videoService: VideoService
4b2f33f3 74 ) {
df98563e 75 super()
4b2f33f3 76 }
dc8bc31b 77
df98563e 78 get filename () {
bfb3a98f 79 return this.form.value['videofile']
e822fdae
C
80 }
81
df98563e 82 buildForm () {
4b2f33f3
C
83 this.form = this.formBuilder.group({
84 name: [ '', VIDEO_NAME.VALIDATORS ],
92fb909c 85 nsfw: [ false ],
fd45e8f4 86 privacy: [ '', VIDEO_PRIVACY.VALIDATORS ],
6e07c3de 87 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
d07137b9 88 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
db216afd 89 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
2de96f4d 90 channelId: [ '', VIDEO_CHANNEL.VALIDATORS ],
4b2f33f3 91 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
bfb3a98f
C
92 videofile: [ '', VIDEO_FILE.VALIDATORS ],
93 tags: [ '' ]
df98563e 94 })
4b2f33f3 95
df98563e 96 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
e822fdae
C
97 }
98
df98563e 99 ngOnInit () {
db7af09b
C
100 this.videoCategories = this.serverService.getVideoCategories()
101 this.videoLicences = this.serverService.getVideoLicences()
102 this.videoLanguages = this.serverService.getVideoLanguages()
fd45e8f4 103 this.videoPrivacies = this.serverService.getVideoPrivacies()
6e07c3de 104
df98563e 105 this.buildForm()
2de96f4d
C
106
107 this.authService.userInformationLoaded
108 .subscribe(
109 () => {
110 const user = this.authService.getUser()
111 if (!user) return
112
113 const videoChannels = user.videoChannels
114 if (Array.isArray(videoChannels) === false) return
115
116 this.userVideoChannels = videoChannels.map(v => ({ id: v.id, label: v.name }))
117
118 this.form.patchValue({ channelId: this.userVideoChannels[0].id })
119 }
120 )
e822fdae
C
121 }
122
bfb3a98f
C
123 // The goal is to keep reactive form validation (required field)
124 // https://stackoverflow.com/a/44238894
125 fileChange ($event) {
126 this.form.controls['videofile'].setValue($event.target.files[0].name)
bf57d5ee
C
127 }
128
bfb3a98f
C
129 removeFile () {
130 this.videofileInput.nativeElement.value = ''
131 this.form.controls['videofile'].setValue('')
bf57d5ee
C
132 }
133
bfb3a98f
C
134 checkForm () {
135 this.forceCheck()
136
137 return this.form.valid
e822fdae
C
138 }
139
df98563e 140 upload () {
bf57d5ee 141 if (this.checkForm() === false) {
df98563e 142 return
bf57d5ee
C
143 }
144
bfb3a98f
C
145 const formValue: VideoCreate = this.form.value
146
147 const name = formValue.name
fd45e8f4 148 const privacy = formValue.privacy
bfb3a98f
C
149 const nsfw = formValue.nsfw
150 const category = formValue.category
151 const licence = formValue.licence
152 const language = formValue.language
bcd9f81e 153 const channelId = formValue.channelId
bfb3a98f
C
154 const description = formValue.description
155 const tags = formValue.tags
156 const videofile = this.videofileInput.nativeElement.files[0]
157
158 const formData = new FormData()
159 formData.append('name', name)
fd45e8f4 160 formData.append('privacy', privacy.toString())
bfb3a98f
C
161 formData.append('category', '' + category)
162 formData.append('nsfw', '' + nsfw)
163 formData.append('licence', '' + licence)
bcd9f81e 164 formData.append('channelId', '' + channelId)
bfb3a98f
C
165 formData.append('videofile', videofile)
166
167 // Language is optional
168 if (language) {
169 formData.append('language', '' + language)
df98563e 170 }
e822fdae 171
bfb3a98f
C
172 formData.append('description', description)
173
174 for (let i = 0; i < tags.length; i++) {
175 formData.append(`tags[${i}]`, tags[i])
df98563e 176 }
e822fdae 177
bfb3a98f
C
178 this.videoService.uploadVideo(formData).subscribe(
179 event => {
180 if (event.type === HttpEventType.UploadProgress) {
181 this.progressPercent = Math.round(100 * event.loaded / event.total)
182 } else if (event instanceof HttpResponse) {
183 console.log('Video uploaded.')
184 this.notificationsService.success('Success', 'Video uploaded.')
185
186 // Display all the videos once it's finished
315cc0cc 187 this.router.navigate([ '/videos/list' ])
bfb3a98f
C
188 }
189 },
190
315cc0cc
C
191 err => {
192 // Reset progress
193 this.progressPercent = 0
194 this.error = err.message
195 }
bfb3a98f 196 )
dc8bc31b
C
197 }
198}