2021-07-16 10:42:24 +02:00
|
|
|
import { of } from 'rxjs'
|
|
|
|
import { switchMap } from 'rxjs/operators'
|
2022-08-03 10:39:40 +02:00
|
|
|
import { AfterViewInit, Component, OnInit } from '@angular/core'
|
2018-04-26 16:11:38 +02:00
|
|
|
import { Router } from '@angular/router'
|
2022-08-03 10:39:40 +02:00
|
|
|
import { AuthService, HooksService, Notifier } from '@app/core'
|
2020-08-17 11:47:04 +02:00
|
|
|
import {
|
|
|
|
VIDEO_CHANNEL_DESCRIPTION_VALIDATOR,
|
|
|
|
VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
|
|
|
|
VIDEO_CHANNEL_NAME_VALIDATOR,
|
|
|
|
VIDEO_CHANNEL_SUPPORT_VALIDATOR
|
|
|
|
} from '@app/shared/form-validators/video-channel-validators'
|
2022-10-07 15:26:53 +02:00
|
|
|
import { FormReactiveService } from '@app/shared/shared-forms'
|
2021-04-08 12:09:54 +02:00
|
|
|
import { VideoChannel, VideoChannelService } from '@app/shared/shared-main'
|
2021-07-16 10:42:24 +02:00
|
|
|
import { HttpStatusCode, VideoChannelCreate } from '@shared/models'
|
2021-12-13 15:29:13 +01:00
|
|
|
import { VideoChannelEdit } from './video-channel-edit'
|
2018-04-26 16:11:38 +02:00
|
|
|
|
|
|
|
@Component({
|
2021-12-13 15:29:13 +01:00
|
|
|
templateUrl: './video-channel-edit.component.html',
|
|
|
|
styleUrls: [ './video-channel-edit.component.scss' ]
|
2018-04-26 16:11:38 +02:00
|
|
|
})
|
2022-08-03 10:39:40 +02:00
|
|
|
export class VideoChannelCreateComponent extends VideoChannelEdit implements OnInit, AfterViewInit {
|
2018-04-26 16:11:38 +02:00
|
|
|
error: string
|
2021-04-08 12:09:54 +02:00
|
|
|
videoChannel = new VideoChannel({})
|
|
|
|
|
|
|
|
private avatar: FormData
|
|
|
|
private banner: FormData
|
2018-04-26 16:11:38 +02:00
|
|
|
|
|
|
|
constructor (
|
2022-10-07 15:26:53 +02:00
|
|
|
protected formReactiveService: FormReactiveService,
|
2018-05-07 11:20:58 +02:00
|
|
|
private authService: AuthService,
|
2018-12-19 16:04:34 +01:00
|
|
|
private notifier: Notifier,
|
2018-04-26 16:11:38 +02:00
|
|
|
private router: Router,
|
2022-08-03 10:39:40 +02:00
|
|
|
private videoChannelService: VideoChannelService,
|
|
|
|
private hooks: HooksService
|
2021-08-17 14:42:53 +02:00
|
|
|
) {
|
2018-04-26 16:11:38 +02:00
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit () {
|
2018-06-05 10:58:45 +02:00
|
|
|
this.buildForm({
|
2020-08-17 11:47:04 +02:00
|
|
|
name: VIDEO_CHANNEL_NAME_VALIDATOR,
|
|
|
|
'display-name': VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
|
|
|
|
description: VIDEO_CHANNEL_DESCRIPTION_VALIDATOR,
|
|
|
|
support: VIDEO_CHANNEL_SUPPORT_VALIDATOR
|
2018-06-05 10:58:45 +02:00
|
|
|
})
|
2018-04-26 16:11:38 +02:00
|
|
|
}
|
|
|
|
|
2022-08-03 10:39:40 +02:00
|
|
|
ngAfterViewInit () {
|
|
|
|
this.hooks.runAction('action:video-channel-create.init', 'video-channel')
|
|
|
|
}
|
|
|
|
|
2018-04-26 16:11:38 +02:00
|
|
|
formValidated () {
|
|
|
|
this.error = undefined
|
|
|
|
|
|
|
|
const body = this.form.value
|
|
|
|
const videoChannelCreate: VideoChannelCreate = {
|
2018-08-17 15:45:42 +02:00
|
|
|
name: body.name,
|
2018-04-26 16:11:38 +02:00
|
|
|
displayName: body['display-name'],
|
2018-05-09 11:23:14 +02:00
|
|
|
description: body.description || null,
|
|
|
|
support: body.support || null
|
2018-04-26 16:11:38 +02:00
|
|
|
}
|
|
|
|
|
2021-04-08 12:09:54 +02:00
|
|
|
this.videoChannelService.createVideoChannel(videoChannelCreate)
|
|
|
|
.pipe(
|
|
|
|
switchMap(() => this.uploadAvatar()),
|
|
|
|
switchMap(() => this.uploadBanner())
|
2021-08-17 11:27:47 +02:00
|
|
|
).subscribe({
|
|
|
|
next: () => {
|
2021-04-08 12:09:54 +02:00
|
|
|
this.authService.refreshUserInformation()
|
|
|
|
|
|
|
|
this.notifier.success($localize`Video channel ${videoChannelCreate.displayName} created.`)
|
2021-08-17 14:42:53 +02:00
|
|
|
this.router.navigate([ '/my-library', 'video-channels' ])
|
2021-04-08 12:09:54 +02:00
|
|
|
},
|
2018-12-19 16:04:34 +01:00
|
|
|
|
2021-08-17 11:27:47 +02:00
|
|
|
error: err => {
|
2021-04-08 12:09:54 +02:00
|
|
|
if (err.status === HttpStatusCode.CONFLICT_409) {
|
|
|
|
this.error = $localize`This name already exists on this instance.`
|
|
|
|
return
|
|
|
|
}
|
2018-04-26 16:11:38 +02:00
|
|
|
|
2021-04-08 12:09:54 +02:00
|
|
|
this.error = err.message
|
2018-10-01 15:18:07 +02:00
|
|
|
}
|
2021-08-17 11:27:47 +02:00
|
|
|
})
|
2021-04-08 12:09:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onAvatarChange (formData: FormData) {
|
|
|
|
this.avatar = formData
|
|
|
|
}
|
|
|
|
|
|
|
|
onAvatarDelete () {
|
|
|
|
this.avatar = null
|
|
|
|
}
|
|
|
|
|
|
|
|
onBannerChange (formData: FormData) {
|
|
|
|
this.banner = formData
|
|
|
|
}
|
2018-10-01 15:18:07 +02:00
|
|
|
|
2021-04-08 12:09:54 +02:00
|
|
|
onBannerDelete () {
|
|
|
|
this.banner = null
|
2018-04-26 16:11:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isCreation () {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
getFormButtonTitle () {
|
2020-08-12 10:40:04 +02:00
|
|
|
return $localize`Create`
|
2018-04-26 16:11:38 +02:00
|
|
|
}
|
2021-04-08 12:09:54 +02:00
|
|
|
|
|
|
|
getUsername () {
|
|
|
|
return this.form.value.name
|
|
|
|
}
|
|
|
|
|
|
|
|
private uploadAvatar () {
|
|
|
|
if (!this.avatar) return of(undefined)
|
|
|
|
|
|
|
|
return this.videoChannelService.changeVideoChannelImage(this.getUsername(), this.avatar, 'avatar')
|
|
|
|
}
|
|
|
|
|
|
|
|
private uploadBanner () {
|
|
|
|
if (!this.banner) return of(undefined)
|
|
|
|
|
|
|
|
return this.videoChannelService.changeVideoChannelImage(this.getUsername(), this.banner, 'banner')
|
|
|
|
}
|
2018-04-26 16:11:38 +02:00
|
|
|
}
|