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

146 lines
4.3 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 { FormReactiveService } from '@app/shared/shared-forms/form-reactive.service'
import { HttpStatusCode, VideoChannelCreate } from '@peertube/peertube-models'
import { VideoChannelEdit } from './video-channel-edit'
2024-03-04 10:01:52 +01:00
import { PeertubeCheckboxComponent } from '../../shared/shared-forms/peertube-checkbox.component'
import { MarkdownTextareaComponent } from '../../shared/shared-forms/markdown-textarea.component'
import { HelpComponent } from '../../shared/shared-main/misc/help.component'
import { ActorAvatarEditComponent } from '../../shared/shared-actor-image-edit/actor-avatar-edit.component'
import { ActorBannerEditComponent } from '../../shared/shared-actor-image-edit/actor-banner-edit.component'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { NgIf, NgClass } from '@angular/common'
import { VideoChannel } from '@app/shared/shared-main/video-channel/video-channel.model'
import { VideoChannelService } from '@app/shared/shared-main/video-channel/video-channel.service'
2018-04-26 16:11:38 +02:00
@Component({
templateUrl: './video-channel-edit.component.html',
2024-03-04 10:01:52 +01:00
styleUrls: [ './video-channel-edit.component.scss' ],
standalone: true,
imports: [
NgIf,
FormsModule,
ReactiveFormsModule,
ActorBannerEditComponent,
ActorAvatarEditComponent,
NgClass,
HelpComponent,
MarkdownTextareaComponent,
PeertubeCheckboxComponent
]
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 (
2022-10-07 15:26:53 +02:00
protected formReactiveService: FormReactiveService,
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({
2023-05-24 15:27:15 +02:00
'name': VIDEO_CHANNEL_NAME_VALIDATOR,
'display-name': VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
2023-05-24 15:27:15 +02:00
'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 () {
2024-03-01 14:42:10 +01:00
return $localize`Create your channel`
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
}