PeerTube/client/src/app/+manage/video-channel-edit/video-channel-create.compon...

126 lines
3.4 KiB
TypeScript
Raw Normal View History

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'
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'
import { FormValidatorService } from '@app/shared/shared-forms'
import { VideoChannel, VideoChannelService } from '@app/shared/shared-main'
2021-07-16 10:42:24 +02:00
import { HttpStatusCode, VideoChannelCreate } from '@shared/models'
import { VideoChannelEdit } from './video-channel-edit'
2018-04-26 16:11:38 +02:00
@Component({
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
videoChannel = new VideoChannel({})
private avatar: FormData
private banner: FormData
2018-04-26 16:11:38 +02:00
constructor (
2018-06-05 10:58:45 +02:00
protected formValidatorService: FormValidatorService,
private authService: AuthService,
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({
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'],
description: body.description || null,
support: body.support || null
2018-04-26 16:11:38 +02:00
}
this.videoChannelService.createVideoChannel(videoChannelCreate)
.pipe(
switchMap(() => this.uploadAvatar()),
switchMap(() => this.uploadBanner())
2021-08-17 11:27:47 +02:00
).subscribe({
next: () => {
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-08-17 11:27:47 +02:00
error: err => {
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
this.error = err.message
}
2021-08-17 11:27:47 +02:00
})
}
onAvatarChange (formData: FormData) {
this.avatar = formData
}
onAvatarDelete () {
this.avatar = null
}
onBannerChange (formData: FormData) {
this.banner = formData
}
onBannerDelete () {
this.banner = null
2018-04-26 16:11:38 +02:00
}
isCreation () {
return true
}
getFormButtonTitle () {
return $localize`Create`
2018-04-26 16:11:38 +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
}