1 import { HttpEventType, HttpResponse } from '@angular/common/http'
2 import { Component, OnInit, ViewChild } from '@angular/core'
3 import { FormBuilder, FormGroup } from '@angular/forms'
4 import { Router } from '@angular/router'
5 import { NotificationsService } from 'angular2-notifications'
6 import { VideoService } from 'app/shared/video/video.service'
7 import { VideoCreate } from '../../../../../shared'
8 import { AuthService, ServerService } from '../../core'
9 import { FormReactive } from '../../shared'
10 import { ValidatorMessage } from '../../shared/forms/form-validators'
11 import { VideoEdit } from '../../shared/video/video-edit.model'
14 selector: 'my-videos-add',
15 templateUrl: './video-add.component.html',
17 './shared/video-edit.component.scss',
18 './video-add.component.scss'
22 export class VideoAddComponent extends FormReactive implements OnInit {
23 @ViewChild('videofileInput') videofileInput
25 isUploadingVideo = false
31 formErrors: { [ id: string ]: string } = {}
32 validationMessages: ValidatorMessage = {}
34 userVideoChannels = []
40 private formBuilder: FormBuilder,
41 private router: Router,
42 private notificationsService: NotificationsService,
43 private authService: AuthService,
44 private serverService: ServerService,
45 private videoService: VideoService
51 this.form = this.formBuilder.group({})
52 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
58 this.serverService.videoCategoriesLoaded
61 this.videoPrivacies = this.serverService.getVideoPrivacies()
62 this.firstStepPrivacy = this.videoPrivacies[0].id
65 this.authService.userInformationLoaded
68 const user = this.authService.getUser()
71 const videoChannels = user.videoChannels
72 if (Array.isArray(videoChannels) === false) return
74 this.userVideoChannels = videoChannels.map(v => ({ id: v.id, label: v.name }))
75 this.firstStepChannel = this.userVideoChannels[0].id
81 this.uploadFirstStep()
87 return this.form.valid
91 const videofile = this.videofileInput.nativeElement.files[0]
92 const name = videofile.name
93 const privacy = this.firstStepPrivacy.toString()
95 const channelId = this.firstStepChannel.toString()
97 const formData = new FormData()
98 formData.append('name', name)
99 formData.append('privacy', privacy.toString())
100 formData.append('nsfw', '' + nsfw)
101 formData.append('channelId', '' + channelId)
102 formData.append('videofile', videofile)
104 this.isUploadingVideo = true
105 this.form.patchValue({
112 this.videoService.uploadVideo(formData).subscribe(
114 if (event.type === HttpEventType.UploadProgress) {
115 this.progressPercent = Math.round(100 * event.loaded / event.total)
116 } else if (event instanceof HttpResponse) {
117 console.log('Video uploaded.')
119 this.videoUploaded = true
125 this.progressPercent = 0
126 this.error = err.message
131 updateSecondStep () {
132 if (this.checkForm() === false) {
136 const video = new VideoEdit(this.form.value)
138 this.videoService.updateVideo(video)
141 this.notificationsService.success('Success', 'Video published.')
142 this.router.navigate([ '/videos/watch', video.uuid ])
146 this.error = 'Cannot update the video.'