1 import { map, switchMap } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { LoadingBarService } from '@ngx-loading-bar/core'
5 import { NotificationsService } from 'angular2-notifications'
6 import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
7 import { ServerService } from '../../core'
8 import { FormReactive } from '../../shared'
9 import { VideoEdit } from '../../shared/video/video-edit.model'
10 import { VideoService } from '../../shared/video/video.service'
11 import { I18n } from '@ngx-translate/i18n-polyfill'
12 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
13 import { VideoCaptionService } from '@app/shared/video-caption'
14 import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
17 selector: 'my-videos-update',
18 styleUrls: [ './shared/video-edit.component.scss' ],
19 templateUrl: './video-update.component.html'
21 export class VideoUpdateComponent extends FormReactive implements OnInit {
24 isUpdatingVideo = false
25 videoPrivacies: VideoConstant<VideoPrivacy>[] = []
26 userVideoChannels: { id: number, label: string, support: string }[] = []
27 schedulePublicationPossible = false
28 videoCaptions: VideoCaptionEdit[] = []
30 private updateDone = false
33 protected formValidatorService: FormValidatorService,
34 private route: ActivatedRoute,
35 private router: Router,
36 private notificationsService: NotificationsService,
37 private serverService: ServerService,
38 private videoService: VideoService,
39 private loadingBar: LoadingBarService,
40 private videoCaptionService: VideoCaptionService,
49 this.serverService.videoPrivaciesLoaded
50 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
53 .pipe(map(data => data.videoData))
54 .subscribe(({ video, videoChannels, videoCaptions }) => {
55 this.video = new VideoEdit(video)
56 this.userVideoChannels = videoChannels
57 this.videoCaptions = videoCaptions
59 // We cannot set private a video that was not private
60 if (this.video.privacy !== VideoPrivacy.PRIVATE) {
61 this.videoPrivacies = this.videoPrivacies.filter(p => p.id !== VideoPrivacy.PRIVATE)
62 } else { // We can schedule video publication only if it it is private
63 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
66 this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
68 // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
69 setTimeout(() => this.hydrateFormFromVideo())
74 this.notificationsService.error(this.i18n('Error'), err.message)
80 if (this.updateDone === true) return { canDeactivate: true }
82 for (const caption of this.videoCaptions) {
83 if (caption.action) return { canDeactivate: false }
86 return { canDeactivate: this.formChanged === false }
92 return this.form.valid
96 if (this.checkForm() === false) {
100 this.video.patch(this.form.value)
102 this.loadingBar.start()
103 this.isUpdatingVideo = true
106 this.videoService.updateVideo(this.video)
108 // Then update captions
109 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
113 this.updateDone = true
114 this.isUpdatingVideo = false
115 this.loadingBar.complete()
116 this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.'))
117 this.router.navigate([ '/videos/watch', this.video.uuid ])
121 this.loadingBar.complete()
122 this.isUpdatingVideo = false
123 this.notificationsService.error(this.i18n('Error'), err.message)
129 private hydrateFormFromVideo () {
130 this.form.patchValue(this.video.toFormPatch())
135 name: 'thumbnailfile'
143 for (const obj of objects) {
144 fetch(this.video[obj.url])
145 .then(response => response.blob())
147 this.form.patchValue({