1 import { map, switchMap } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { FormBuilder, FormGroup } from '@angular/forms'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { LoadingBarService } from '@ngx-loading-bar/core'
6 import { NotificationsService } from 'angular2-notifications'
7 import { VideoPrivacy } from '../../../../../shared/models/videos'
8 import { ServerService } from '../../core'
9 import { AuthService } from '../../core/auth'
10 import { FormReactive } from '../../shared'
11 import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
12 import { VideoEdit } from '../../shared/video/video-edit.model'
13 import { VideoService } from '../../shared/video/video.service'
14 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
15 import { I18n } from '@ngx-translate/i18n-polyfill'
18 selector: 'my-videos-update',
19 styleUrls: [ './shared/video-edit.component.scss' ],
20 templateUrl: './video-update.component.html'
22 export class VideoUpdateComponent extends FormReactive implements OnInit {
25 isUpdatingVideo = false
27 formErrors: { [ id: string ]: string } = {}
28 validationMessages: ValidatorMessage = {}
30 userVideoChannels = []
33 private formBuilder: FormBuilder,
34 private route: ActivatedRoute,
35 private router: Router,
36 private notificationsService: NotificationsService,
37 private serverService: ServerService,
38 private videoService: VideoService,
39 private authService: AuthService,
40 private loadingBar: LoadingBarService,
41 private videoChannelService: VideoChannelService,
48 this.form = this.formBuilder.group({})
49 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
55 this.serverService.videoPrivaciesLoaded
56 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
58 const uuid: string = this.route.snapshot.params[ 'uuid' ]
59 this.videoService.getVideo(uuid)
62 return this.videoService
63 .loadCompleteDescription(video.descriptionPath)
64 .pipe(map(description => Object.assign(video, { description })))
67 return this.videoChannelService
68 .listAccountVideoChannels(video.account)
70 map(result => result.data),
71 map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName, support: c.support }))),
72 map(videoChannels => ({ video, videoChannels }))
77 ({ video, videoChannels }) => {
78 this.video = new VideoEdit(video)
79 this.userVideoChannels = videoChannels
81 // We cannot set private a video that was not private
82 if (video.privacy.id !== VideoPrivacy.PRIVATE) {
83 const newVideoPrivacies = []
84 for (const p of this.videoPrivacies) {
85 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
88 this.videoPrivacies = newVideoPrivacies
91 this.hydrateFormFromVideo()
96 this.notificationsService.error(this.i18n('Error'), err.message)
104 return this.form.valid
108 if (this.checkForm() === false) {
112 this.video.patch(this.form.value)
114 this.loadingBar.start()
115 this.isUpdatingVideo = true
116 this.videoService.updateVideo(this.video)
119 this.isUpdatingVideo = false
120 this.loadingBar.complete()
121 this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.'))
122 this.router.navigate([ '/videos/watch', this.video.uuid ])
126 this.isUpdatingVideo = false
127 this.notificationsService.error(this.i18n('Error'), err.message)
134 private hydrateFormFromVideo () {
135 this.form.patchValue(this.video.toJSON())
140 name: 'thumbnailfile'
148 for (const obj of objects) {
149 fetch(this.video[obj.url])
150 .then(response => response.blob())
152 this.form.patchValue({