1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { Router } from '@angular/router'
5 import { NotificationsService } from 'angular2-notifications'
18 import { AuthService, ServerService } from '../../core'
19 import { VideoService } from '../shared'
20 import { VideoCreate } from '../../../../../shared'
21 import { HttpEventType, HttpResponse } from '@angular/common/http'
24 selector: 'my-videos-add',
25 styleUrls: [ './video-edit.component.scss' ],
26 templateUrl: './video-add.component.html'
29 export class VideoAddComponent extends FormReactive implements OnInit {
30 @ViewChild('videofileInput') videofileInput
37 userVideoChannels = []
39 tagValidators = VIDEO_TAGS.VALIDATORS
40 tagValidatorsMessages = VIDEO_TAGS.MESSAGES
53 validationMessages = {
54 name: VIDEO_NAME.MESSAGES,
55 category: VIDEO_CATEGORY.MESSAGES,
56 licence: VIDEO_LICENCE.MESSAGES,
57 language: VIDEO_LANGUAGE.MESSAGES,
58 channelId: VIDEO_CHANNEL.MESSAGES,
59 description: VIDEO_DESCRIPTION.MESSAGES,
60 videofile: VIDEO_FILE.MESSAGES
64 private formBuilder: FormBuilder,
65 private router: Router,
66 private notificationsService: NotificationsService,
67 private authService: AuthService,
68 private serverService: ServerService,
69 private videoService: VideoService
75 return this.form.value['videofile']
79 this.form = this.formBuilder.group({
80 name: [ '', VIDEO_NAME.VALIDATORS ],
82 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
83 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
84 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
85 channelId: [ '', VIDEO_CHANNEL.VALIDATORS ],
86 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
87 videofile: [ '', VIDEO_FILE.VALIDATORS ],
91 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
95 this.videoCategories = this.serverService.getVideoCategories()
96 this.videoLicences = this.serverService.getVideoLicences()
97 this.videoLanguages = this.serverService.getVideoLanguages()
101 this.authService.userInformationLoaded
104 const user = this.authService.getUser()
107 const videoChannels = user.videoChannels
108 if (Array.isArray(videoChannels) === false) return
110 this.userVideoChannels = videoChannels.map(v => ({ id: v.id, label: v.name }))
112 this.form.patchValue({ channelId: this.userVideoChannels[0].id })
117 // The goal is to keep reactive form validation (required field)
118 // https://stackoverflow.com/a/44238894
119 fileChange ($event) {
120 this.form.controls['videofile'].setValue($event.target.files[0].name)
124 this.videofileInput.nativeElement.value = ''
125 this.form.controls['videofile'].setValue('')
131 return this.form.valid
135 if (this.checkForm() === false) {
139 const formValue: VideoCreate = this.form.value
141 const name = formValue.name
142 const nsfw = formValue.nsfw
143 const category = formValue.category
144 const licence = formValue.licence
145 const language = formValue.language
146 const channelId = formValue.channelId
147 const description = formValue.description
148 const tags = formValue.tags
149 const videofile = this.videofileInput.nativeElement.files[0]
151 const formData = new FormData()
152 formData.append('name', name)
153 formData.append('category', '' + category)
154 formData.append('nsfw', '' + nsfw)
155 formData.append('licence', '' + licence)
156 formData.append('channelId', '' + channelId)
157 formData.append('videofile', videofile)
159 // Language is optional
161 formData.append('language', '' + language)
164 formData.append('description', description)
166 for (let i = 0; i < tags.length; i++) {
167 formData.append(`tags[${i}]`, tags[i])
170 this.videoService.uploadVideo(formData).subscribe(
172 if (event.type === HttpEventType.UploadProgress) {
173 this.progressPercent = Math.round(100 * event.loaded / event.total)
174 } else if (event instanceof HttpResponse) {
175 console.log('Video uploaded.')
176 this.notificationsService.success('Success', 'Video uploaded.')
178 // Display all the videos once it's finished
179 this.router.navigate([ '/videos/list' ])
185 this.progressPercent = 0
186 this.error = err.message