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 { VideoPrivacy } from '../../../../../shared/models/videos'
7 import { ServerService } from '../../core'
8 import { AuthService } from '../../core/auth'
9 import { FormReactive } from '../../shared'
10 import { VideoEdit } from '../../shared/video/video-edit.model'
11 import { VideoService } from '../../shared/video/video.service'
12 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
13 import { I18n } from '@ngx-translate/i18n-polyfill'
14 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
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
26 userVideoChannels = []
29 protected formValidatorService: FormValidatorService,
30 private route: ActivatedRoute,
31 private router: Router,
32 private notificationsService: NotificationsService,
33 private serverService: ServerService,
34 private videoService: VideoService,
35 private authService: AuthService,
36 private loadingBar: LoadingBarService,
37 private videoChannelService: VideoChannelService,
46 this.serverService.videoPrivaciesLoaded
47 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
49 const uuid: string = this.route.snapshot.params[ 'uuid' ]
50 this.videoService.getVideo(uuid)
53 return this.videoService
54 .loadCompleteDescription(video.descriptionPath)
55 .pipe(map(description => Object.assign(video, { description })))
58 return this.videoChannelService
59 .listAccountVideoChannels(video.account)
61 map(result => result.data),
62 map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName, support: c.support }))),
63 map(videoChannels => ({ video, videoChannels }))
68 ({ video, videoChannels }) => {
69 this.video = new VideoEdit(video)
70 this.userVideoChannels = videoChannels
72 // We cannot set private a video that was not private
73 if (video.privacy.id !== VideoPrivacy.PRIVATE) {
74 const newVideoPrivacies = []
75 for (const p of this.videoPrivacies) {
76 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
79 this.videoPrivacies = newVideoPrivacies
82 this.hydrateFormFromVideo()
87 this.notificationsService.error(this.i18n('Error'), err.message)
95 return this.form.valid
99 if (this.checkForm() === false) {
103 this.video.patch(this.form.value)
105 this.loadingBar.start()
106 this.isUpdatingVideo = true
107 this.videoService.updateVideo(this.video)
110 this.isUpdatingVideo = false
111 this.loadingBar.complete()
112 this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.'))
113 this.router.navigate([ '/videos/watch', this.video.uuid ])
117 this.isUpdatingVideo = false
118 this.notificationsService.error(this.i18n('Error'), err.message)
125 private hydrateFormFromVideo () {
126 this.form.patchValue(this.video.toJSON())
131 name: 'thumbnailfile'
139 for (const obj of objects) {
140 fetch(this.video[obj.url])
141 .then(response => response.blob())
143 this.form.patchValue({