2018-05-15 05:55:51 -04:00
|
|
|
import { map, switchMap } from 'rxjs/operators'
|
2017-10-09 13:12:40 -04:00
|
|
|
import { Component, OnInit } from '@angular/core'
|
2017-06-16 08:32:15 -04:00
|
|
|
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'
|
2017-06-16 08:32:15 -04:00
|
|
|
import { NotificationsService } from 'angular2-notifications'
|
2017-12-11 11:36:46 -05:00
|
|
|
import { VideoPrivacy } from '../../../../../shared/models/videos'
|
2017-10-09 13:12:40 -04:00
|
|
|
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'
|
2018-05-11 09:10:13 -04:00
|
|
|
import { populateAsyncUserVideoChannels } from '@app/shared/misc/utils'
|
2016-04-14 16:07:46 -04:00
|
|
|
|
2016-03-14 08:50:19 -04:00
|
|
|
@Component({
|
2017-04-10 15:15:28 -04:00
|
|
|
selector: 'my-videos-update',
|
2017-11-28 09:40:53 -05:00
|
|
|
styleUrls: [ './shared/video-edit.component.scss' ],
|
2017-04-10 15:15:28 -04:00
|
|
|
templateUrl: './video-update.component.html'
|
2016-03-14 08:50:19 -04:00
|
|
|
})
|
2017-04-10 15:15:28 -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
|
2017-06-16 08:32:15 -04:00
|
|
|
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
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
constructor (
|
2016-09-09 16:16:51 -04:00
|
|
|
private formBuilder: FormBuilder,
|
2017-04-10 15:15:28 -04:00
|
|
|
private route: ActivatedRoute,
|
2017-01-27 10:14:11 -05:00
|
|
|
private router: Router,
|
2017-03-22 16:15:55 -04:00
|
|
|
private notificationsService: NotificationsService,
|
2017-10-09 13:12:40 -04:00
|
|
|
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
|
2016-09-09 16:16:51 -04:00
|
|
|
) {
|
2017-06-16 08:32:15 -04:00
|
|
|
super()
|
2016-09-09 16:16:51 -04:00
|
|
|
}
|
2016-03-14 08:50:19 -04:00
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
buildForm () {
|
2017-12-07 05:15:19 -05:00
|
|
|
this.form = this.formBuilder.group({})
|
2017-06-16 08:32:15 -04:00
|
|
|
this.form.valueChanges.subscribe(data => this.onValueChanged(data))
|
2016-06-07 16:34:02 -04:00
|
|
|
}
|
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
ngOnInit () {
|
|
|
|
this.buildForm()
|
2017-04-10 15:15:28 -04:00
|
|
|
|
2017-12-20 08:29:55 -05:00
|
|
|
this.serverService.videoPrivaciesLoaded
|
2018-02-16 10:35:32 -05:00
|
|
|
.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' ]
|
2017-10-30 15:26:06 -04:00
|
|
|
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 })))
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.subscribe(
|
|
|
|
video => {
|
|
|
|
this.video = new VideoEdit(video)
|
|
|
|
|
|
|
|
populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
|
|
|
|
.catch(err => console.error(err))
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
},
|
2017-10-30 15:26:06 -04:00
|
|
|
|
2018-05-15 05:55:51 -04:00
|
|
|
err => {
|
|
|
|
console.error(err)
|
|
|
|
this.notificationsService.error('Error', err.message)
|
|
|
|
}
|
|
|
|
)
|
2016-06-07 16:34:02 -04:00
|
|
|
}
|
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
checkForm () {
|
|
|
|
this.forceCheck()
|
2017-05-05 08:29:58 -04:00
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
return this.form.valid
|
2017-05-05 08:29:58 -04:00
|
|
|
}
|
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
update () {
|
2017-05-05 08:29:58 -04:00
|
|
|
if (this.checkForm() === false) {
|
2017-06-16 08:32:15 -04:00
|
|
|
return
|
2017-05-05 08:29:58 -04:00
|
|
|
}
|
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
this.video.patch(this.form.value)
|
2017-04-10 15:15:28 -04:00
|
|
|
|
2018-02-16 11:24:47 -05:00
|
|
|
this.loadingBar.start()
|
|
|
|
this.isUpdatingVideo = true
|
2017-04-10 15:15:28 -04:00
|
|
|
this.videoService.updateVideo(this.video)
|
|
|
|
.subscribe(
|
|
|
|
() => {
|
2018-02-16 11:24:47 -05:00
|
|
|
this.isUpdatingVideo = false
|
|
|
|
this.loadingBar.complete()
|
2017-06-16 08:32:15 -04:00
|
|
|
this.notificationsService.success('Success', 'Video updated.')
|
2017-07-11 10:01:56 -04:00
|
|
|
this.router.navigate([ '/videos/watch', this.video.uuid ])
|
2017-04-10 15:15:28 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
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)
|
2017-06-16 08:32:15 -04:00
|
|
|
console.error(err)
|
2017-04-10 15:15:28 -04:00
|
|
|
}
|
2017-06-16 08:32:15 -04:00
|
|
|
)
|
2016-06-07 16:34:02 -04:00
|
|
|
|
2016-03-14 08:50:19 -04:00
|
|
|
}
|
2017-03-22 16:47:05 -04:00
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
private hydrateFormFromVideo () {
|
|
|
|
this.form.patchValue(this.video.toJSON())
|
2018-02-16 10:35:32 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2017-04-10 15:15:28 -04:00
|
|
|
}
|
2016-03-14 08:50:19 -04:00
|
|
|
}
|