1 import { Component, OnInit } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { AuthService, Notifier } from '@app/core'
5 VIDEO_CHANNEL_DESCRIPTION_VALIDATOR,
6 VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
7 VIDEO_CHANNEL_NAME_VALIDATOR,
8 VIDEO_CHANNEL_SUPPORT_VALIDATOR
9 } from '@app/shared/form-validators/video-channel-validators'
10 import { FormValidatorService } from '@app/shared/shared-forms'
11 import { VideoChannel, VideoChannelService } from '@app/shared/shared-main'
12 import { VideoChannelCreate } from '@shared/models'
13 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
14 import { MyVideoChannelEdit } from './my-video-channel-edit'
15 import { switchMap } from 'rxjs/operators'
16 import { of } from 'rxjs'
19 templateUrl: './my-video-channel-edit.component.html',
20 styleUrls: [ './my-video-channel-edit.component.scss' ]
22 export class MyVideoChannelCreateComponent extends MyVideoChannelEdit implements OnInit {
24 videoChannel = new VideoChannel({})
26 private avatar: FormData
27 private banner: FormData
30 protected formValidatorService: FormValidatorService,
31 private authService: AuthService,
32 private notifier: Notifier,
33 private router: Router,
34 private videoChannelService: VideoChannelService
41 name: VIDEO_CHANNEL_NAME_VALIDATOR,
42 'display-name': VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
43 description: VIDEO_CHANNEL_DESCRIPTION_VALIDATOR,
44 support: VIDEO_CHANNEL_SUPPORT_VALIDATOR
49 this.error = undefined
51 const body = this.form.value
52 const videoChannelCreate: VideoChannelCreate = {
54 displayName: body['display-name'],
55 description: body.description || null,
56 support: body.support || null
59 this.videoChannelService.createVideoChannel(videoChannelCreate)
61 switchMap(() => this.uploadAvatar()),
62 switchMap(() => this.uploadBanner())
65 this.authService.refreshUserInformation()
67 this.notifier.success($localize`Video channel ${videoChannelCreate.displayName} created.`)
68 this.router.navigate(['/my-library', 'video-channels'])
72 if (err.status === HttpStatusCode.CONFLICT_409) {
73 this.error = $localize`This name already exists on this instance.`
77 this.error = err.message
82 onAvatarChange (formData: FormData) {
83 this.avatar = formData
90 onBannerChange (formData: FormData) {
91 this.banner = formData
102 getFormButtonTitle () {
103 return $localize`Create`
107 return this.form.value.name
110 private uploadAvatar () {
111 if (!this.avatar) return of(undefined)
113 return this.videoChannelService.changeVideoChannelImage(this.getUsername(), this.avatar, 'avatar')
116 private uploadBanner () {
117 if (!this.banner) return of(undefined)
119 return this.videoChannelService.changeVideoChannelImage(this.getUsername(), this.banner, 'banner')