1 import { Component, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { NotificationsService } from 'angular2-notifications'
5 import 'rxjs/add/observable/forkJoin'
6 import { VideoPrivacy } from '../../../../../shared/models/videos'
7 import { ServerService } from '../../core'
8 import { AuthService } from '../../core/auth'
9 import { FormReactive } from '../../shared'
10 import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
11 import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
12 import { VideoEdit } from '../../shared/video/video-edit.model'
13 import { VideoService } from '../../shared/video/video.service'
16 selector: 'my-videos-update',
17 styleUrls: [ './shared/video-edit.component.scss' ],
18 templateUrl: './video-update.component.html'
21 export class VideoUpdateComponent extends FormReactive implements OnInit {
26 formErrors: { [ id: string ]: string } = {}
27 validationMessages: ValidatorMessage = {}
29 userVideoChannels = []
32 private formBuilder: FormBuilder,
33 private route: ActivatedRoute,
34 private router: Router,
35 private notificationsService: NotificationsService,
36 private serverService: ServerService,
37 private videoService: VideoService,
38 private authService: AuthService
44 this.form = this.formBuilder.group({})
45 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
51 this.serverService.videoPrivaciesLoaded
53 () => this.videoPrivacies = this.serverService.getVideoPrivacies()
56 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
58 const uuid: string = this.route.snapshot.params['uuid']
59 this.videoService.getVideo(uuid)
61 return this.videoService
62 .loadCompleteDescription(video.descriptionPath)
63 .do(description => video.description = description)
68 this.video = new VideoEdit(video)
70 // We cannot set private a video that was not private
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)
77 this.videoPrivacies = newVideoPrivacies
80 this.hydrateFormFromVideo()
85 this.error = 'Cannot fetch video.'
93 return this.form.valid
97 if (this.checkForm() === false) {
101 this.video.patch(this.form.value)
103 this.videoService.updateVideo(this.video)
106 this.notificationsService.success('Success', 'Video updated.')
107 this.router.navigate([ '/videos/watch', this.video.uuid ])
111 this.error = 'Cannot update the video.'
118 private hydrateFormFromVideo () {
119 this.form.patchValue(this.video.toJSON())