1
0
Fork 0
peertube/client/src/app/videos/+video-edit/video-update.component.ts

156 lines
4.9 KiB
TypeScript
Raw Normal View History

2018-05-15 05:55:51 -04:00
import { map, switchMap } from 'rxjs/operators'
import { Component, OnInit } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router'
2018-02-16 11:24:47 -05:00
import { LoadingBarService } from '@ngx-loading-bar/core'
import { NotificationsService } from 'angular2-notifications'
2018-07-16 11:36:42 -04:00
import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
import { ServerService } from '../../core'
2017-12-20 08:29:55 -05:00
import { AuthService } from '../../core/auth'
2017-12-07 08:48:47 -05:00
import { FormReactive } from '../../shared'
2017-12-01 12:56:26 -05:00
import { VideoEdit } from '../../shared/video/video-edit.model'
2017-12-07 08:48:47 -05:00
import { VideoService } from '../../shared/video/video.service'
import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
2018-06-04 10:21:17 -04:00
import { I18n } from '@ngx-translate/i18n-polyfill'
2018-06-05 04:58:45 -04:00
import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
2018-07-12 13:02:00 -04:00
import { VideoCaptionService } from '@app/shared/video-caption'
2018-07-16 11:36:42 -04:00
import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
2016-03-14 08:50:19 -04:00
@Component({
selector: 'my-videos-update',
styleUrls: [ './shared/video-edit.component.scss' ],
templateUrl: './video-update.component.html'
2016-03-14 08:50:19 -04:00
})
export class VideoUpdateComponent extends FormReactive implements OnInit {
2017-10-25 10:43:19 -04:00
video: VideoEdit
2016-09-09 16:16:51 -04:00
2018-02-16 11:24:47 -05:00
isUpdatingVideo = false
2018-07-16 11:36:42 -04:00
videoPrivacies: VideoConstant<string>[] = []
userVideoChannels: { id: number, label: string, support: string }[] = []
schedulePublicationPossible = false
2018-07-16 11:36:42 -04:00
videoCaptions: VideoCaptionEdit[] = []
2016-03-14 08:50:19 -04:00
private updateDone = false
constructor (
2018-06-05 04:58:45 -04:00
protected formValidatorService: FormValidatorService,
private route: ActivatedRoute,
private router: Router,
2017-03-22 16:15:55 -04:00
private notificationsService: NotificationsService,
private serverService: ServerService,
2017-12-20 08:29:55 -05:00
private videoService: VideoService,
2018-02-16 11:24:47 -05:00
private authService: AuthService,
private loadingBar: LoadingBarService,
2018-06-04 10:21:17 -04:00
private videoChannelService: VideoChannelService,
2018-07-12 13:02:00 -04:00
private videoCaptionService: VideoCaptionService,
2018-06-04 10:21:17 -04:00
private i18n: I18n
2016-09-09 16:16:51 -04:00
) {
super()
2016-09-09 16:16:51 -04:00
}
2016-03-14 08:50:19 -04:00
ngOnInit () {
2018-06-05 04:58:45 -04:00
this.buildForm({})
2017-12-20 08:29:55 -05:00
this.serverService.videoPrivaciesLoaded
2018-07-16 12:09:31 -04:00
.subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
2017-12-20 08:29:55 -05:00
2018-07-16 12:09:31 -04:00
this.route.data
.pipe(map(data => data.videoData))
.subscribe(({ video, videoChannels, videoCaptions }) => {
this.video = new VideoEdit(video)
this.userVideoChannels = videoChannels
this.videoCaptions = videoCaptions
2018-05-15 05:55:51 -04:00
2018-07-16 12:09:31 -04:00
// We cannot set private a video that was not private
if (this.video.privacy !== VideoPrivacy.PRIVATE) {
this.videoPrivacies = this.videoPrivacies.filter(p => p.id.toString() !== VideoPrivacy.PRIVATE.toString())
} else { // We can schedule video publication only if it it is private
this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
}
2017-10-31 06:52:52 -04:00
// FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
2018-07-16 12:09:31 -04:00
setTimeout(() => this.hydrateFormFromVideo())
},
2018-07-16 12:09:31 -04:00
err => {
console.error(err)
this.notificationsService.error(this.i18n('Error'), err.message)
}
)
}
canDeactivate () {
if (this.updateDone === true) return { canDeactivate: true }
for (const caption of this.videoCaptions) {
if (caption.action) return { canDeactivate: false }
}
return { canDeactivate: this.formChanged === false }
}
checkForm () {
this.forceCheck()
return this.form.valid
}
update () {
if (this.checkForm() === false) {
return
}
this.video.patch(this.form.value)
2018-02-16 11:24:47 -05:00
this.loadingBar.start()
this.isUpdatingVideo = true
2018-07-12 13:02:00 -04:00
// Update the video
this.videoService.updateVideo(this.video)
2018-07-12 13:02:00 -04:00
.pipe(
// Then update captions
switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
)
.subscribe(
() => {
this.updateDone = true
2018-07-12 13:02:00 -04:00
this.isUpdatingVideo = false
this.loadingBar.complete()
this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.'))
this.router.navigate([ '/videos/watch', this.video.uuid ])
},
err => {
this.loadingBar.complete()
2018-07-12 13:02:00 -04:00
this.isUpdatingVideo = false
this.notificationsService.error(this.i18n('Error'), err.message)
console.error(err)
}
)
2016-03-14 08:50:19 -04:00
}
private hydrateFormFromVideo () {
this.form.patchValue(this.video.toFormPatch())
const objects = [
{
url: 'thumbnailUrl',
name: 'thumbnailfile'
},
{
url: 'previewUrl',
name: 'previewfile'
}
]
for (const obj of objects) {
fetch(this.video[obj.url])
.then(response => response.blob())
.then(data => {
this.form.patchValue({
[ obj.name ]: data
})
})
}
}
2016-03-14 08:50:19 -04:00
}