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

157 lines
4.8 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 { FormBuilder, FormGroup } from '@angular/forms'
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'
2017-12-11 11:36:46 -05:00
import { 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-11 11:36:46 -05:00
import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
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'
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
form: FormGroup
2017-12-07 05:15:19 -05:00
formErrors: { [ id: string ]: string } = {}
validationMessages: ValidatorMessage = {}
videoPrivacies = []
2017-12-20 08:29:55 -05:00
userVideoChannels = []
2016-03-14 08:50:19 -04:00
constructor (
2016-09-09 16:16:51 -04:00
private formBuilder: FormBuilder,
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,
private videoChannelService: VideoChannelService
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
buildForm () {
2017-12-07 05:15:19 -05:00
this.form = this.formBuilder.group({})
this.form.valueChanges.subscribe(data => this.onValueChanged(data))
}
ngOnInit () {
this.buildForm()
2017-12-20 08:29:55 -05:00
this.serverService.videoPrivaciesLoaded
.subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
2017-12-20 08:29:55 -05:00
2018-05-15 05:55:51 -04:00
const uuid: string = this.route.snapshot.params[ 'uuid' ]
this.videoService.getVideo(uuid)
2018-05-15 05:55:51 -04:00
.pipe(
switchMap(video => {
return this.videoService
.loadCompleteDescription(video.descriptionPath)
.pipe(map(description => Object.assign(video, { description })))
}),
switchMap(video => {
return this.videoChannelService
2018-05-25 03:57:16 -04:00
.listAccountVideoChannels(video.account)
.pipe(
map(result => result.data),
map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName, support: c.support }))),
map(videoChannels => ({ video, videoChannels }))
)
2018-05-15 05:55:51 -04:00
})
)
.subscribe(
({ video, videoChannels }) => {
2018-05-15 05:55:51 -04:00
this.video = new VideoEdit(video)
this.userVideoChannels = videoChannels
2018-05-15 05:55:51 -04:00
// We cannot set private a video that was not private
if (video.privacy.id !== VideoPrivacy.PRIVATE) {
const newVideoPrivacies = []
for (const p of this.videoPrivacies) {
if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
}
this.videoPrivacies = newVideoPrivacies
2017-10-31 06:52:52 -04:00
}
2018-05-15 05:55:51 -04:00
this.hydrateFormFromVideo()
},
2018-05-15 05:55:51 -04:00
err => {
console.error(err)
this.notificationsService.error('Error', err.message)
}
)
}
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
this.videoService.updateVideo(this.video)
.subscribe(
() => {
2018-02-16 11:24:47 -05:00
this.isUpdatingVideo = false
this.loadingBar.complete()
this.notificationsService.success('Success', 'Video updated.')
this.router.navigate([ '/videos/watch', this.video.uuid ])
},
err => {
2018-02-16 11:24:47 -05:00
this.isUpdatingVideo = false
2018-01-08 06:53:09 -05:00
this.notificationsService.error('Error', err.message)
console.error(err)
}
)
2016-03-14 08:50:19 -04:00
}
private hydrateFormFromVideo () {
this.form.patchValue(this.video.toJSON())
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
}