]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-update.component.ts
Fix support field validation in video edit
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-update.component.ts
CommitLineData
db7af09b 1import { Component, OnInit } from '@angular/core'
df98563e
C
2import { FormBuilder, FormGroup } from '@angular/forms'
3import { ActivatedRoute, Router } from '@angular/router'
68e24d72 4import { LoadingBarService } from '@ngx-loading-bar/core'
df98563e 5import { NotificationsService } from 'angular2-notifications'
202f6b6c 6import 'rxjs/add/observable/forkJoin'
63c4db6d 7import { VideoPrivacy } from '../../../../../shared/models/videos'
db7af09b 8import { ServerService } from '../../core'
15a7387d 9import { AuthService } from '../../core/auth'
4cc66133 10import { FormReactive } from '../../shared'
63c4db6d 11import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
15a7387d 12import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
202f6b6c 13import { VideoEdit } from '../../shared/video/video-edit.model'
4cc66133 14import { VideoService } from '../../shared/video/video.service'
1553e15d 15
dc8bc31b 16@Component({
d8e689b8 17 selector: 'my-videos-update',
80958c78 18 styleUrls: [ './shared/video-edit.component.scss' ],
d8e689b8 19 templateUrl: './video-update.component.html'
dc8bc31b
C
20})
21
d8e689b8 22export class VideoUpdateComponent extends FormReactive implements OnInit {
404b54e1 23 video: VideoEdit
4b2f33f3 24
68e24d72 25 isUpdatingVideo = false
df98563e 26 form: FormGroup
ff249f49
C
27 formErrors: { [ id: string ]: string } = {}
28 validationMessages: ValidatorMessage = {}
29 videoPrivacies = []
15a7387d 30 userVideoChannels = []
dc8bc31b 31
df98563e 32 constructor (
4b2f33f3 33 private formBuilder: FormBuilder,
d8e689b8 34 private route: ActivatedRoute,
7ddd02c9 35 private router: Router,
6e07c3de 36 private notificationsService: NotificationsService,
db7af09b 37 private serverService: ServerService,
15a7387d 38 private videoService: VideoService,
68e24d72
C
39 private authService: AuthService,
40 private loadingBar: LoadingBarService
4b2f33f3 41 ) {
df98563e 42 super()
4b2f33f3 43 }
dc8bc31b 44
df98563e 45 buildForm () {
ff249f49 46 this.form = this.formBuilder.group({})
df98563e 47 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
e822fdae
C
48 }
49
df98563e
C
50 ngOnInit () {
51 this.buildForm()
d8e689b8 52
15a7387d 53 this.serverService.videoPrivaciesLoaded
6de36768 54 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
15a7387d
C
55
56 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
6de36768 57 .catch(err => console.error('Cannot populate async user video channels.', err))
6e07c3de 58
0a6658fd 59 const uuid: string = this.route.snapshot.params['uuid']
2de96f4d
C
60 this.videoService.getVideo(uuid)
61 .switchMap(video => {
62 return this.videoService
63 .loadCompleteDescription(video.descriptionPath)
61b3e146 64 .map(description => Object.assign(video, { description }))
2de96f4d
C
65 })
66 .subscribe(
67 video => {
68 this.video = new VideoEdit(video)
69
ff249f49 70 // We cannot set private a video that was not private
fd45e8f4
C
71 if (video.privacy !== VideoPrivacy.PRIVATE) {
72 const newVideoPrivacies = []
73 for (const p of this.videoPrivacies) {
74 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
75 }
76
77 this.videoPrivacies = newVideoPrivacies
78 }
79
2de96f4d
C
80 this.hydrateFormFromVideo()
81 },
82
83 err => {
84 console.error(err)
ce5496d6 85 this.notificationsService.error('Error', err.message)
2de96f4d
C
86 }
87 )
e822fdae
C
88 }
89
df98563e
C
90 checkForm () {
91 this.forceCheck()
c24ac1c1 92
df98563e 93 return this.form.valid
c24ac1c1
C
94 }
95
df98563e 96 update () {
c24ac1c1 97 if (this.checkForm() === false) {
df98563e 98 return
c24ac1c1
C
99 }
100
df98563e 101 this.video.patch(this.form.value)
d8e689b8 102
68e24d72
C
103 this.loadingBar.start()
104 this.isUpdatingVideo = true
d8e689b8
C
105 this.videoService.updateVideo(this.video)
106 .subscribe(
107 () => {
68e24d72
C
108 this.isUpdatingVideo = false
109 this.loadingBar.complete()
df98563e 110 this.notificationsService.success('Success', 'Video updated.')
0a6658fd 111 this.router.navigate([ '/videos/watch', this.video.uuid ])
d8e689b8
C
112 },
113
114 err => {
68e24d72 115 this.isUpdatingVideo = false
ce5496d6 116 this.notificationsService.error('Error', err.message)
df98563e 117 console.error(err)
d8e689b8 118 }
df98563e 119 )
e822fdae 120
dc8bc31b 121 }
e54163c2 122
df98563e
C
123 private hydrateFormFromVideo () {
124 this.form.patchValue(this.video.toJSON())
6de36768
C
125
126 const objects = [
127 {
128 url: 'thumbnailUrl',
129 name: 'thumbnailfile'
130 },
131 {
132 url: 'previewUrl',
133 name: 'previewfile'
134 }
135 ]
136
137 for (const obj of objects) {
138 fetch(this.video[obj.url])
139 .then(response => response.blob())
140 .then(data => {
141 this.form.patchValue({
142 [ obj.name ]: data
143 })
144 })
145 }
d8e689b8 146 }
dc8bc31b 147}