1 import { of } from 'rxjs'
2 import { switchMap } from 'rxjs/operators'
3 import { Component, OnInit } from '@angular/core'
4 import { Router } from '@angular/router'
5 import { AuthService, Notifier } from '@app/core'
7 VIDEO_CHANNEL_DESCRIPTION_VALIDATOR,
8 VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
9 VIDEO_CHANNEL_NAME_VALIDATOR,
10 VIDEO_CHANNEL_SUPPORT_VALIDATOR
11 } from '@app/shared/form-validators/video-channel-validators'
12 import { FormValidatorService } from '@app/shared/shared-forms'
13 import { VideoChannel, VideoChannelService } from '@app/shared/shared-main'
14 import { HttpStatusCode, VideoChannelCreate } from '@shared/models'
15 import { MyVideoChannelEdit } from './my-video-channel-edit'
18 templateUrl: './my-video-channel-edit.component.html',
19 styleUrls: [ './my-video-channel-edit.component.scss' ]
21 export class MyVideoChannelCreateComponent extends MyVideoChannelEdit implements OnInit {
23 videoChannel = new VideoChannel({})
25 private avatar: FormData
26 private banner: FormData
29 protected formValidatorService: FormValidatorService,
30 private authService: AuthService,
31 private notifier: Notifier,
32 private router: Router,
33 private videoChannelService: VideoChannelService
40 name: VIDEO_CHANNEL_NAME_VALIDATOR,
41 'display-name': VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
42 description: VIDEO_CHANNEL_DESCRIPTION_VALIDATOR,
43 support: VIDEO_CHANNEL_SUPPORT_VALIDATOR
48 this.error = undefined
50 const body = this.form.value
51 const videoChannelCreate: VideoChannelCreate = {
53 displayName: body['display-name'],
54 description: body.description || null,
55 support: body.support || null
58 this.videoChannelService.createVideoChannel(videoChannelCreate)
60 switchMap(() => this.uploadAvatar()),
61 switchMap(() => this.uploadBanner())
64 this.authService.refreshUserInformation()
66 this.notifier.success($localize`Video channel ${videoChannelCreate.displayName} created.`)
67 this.router.navigate([ '/my-library', 'video-channels' ])
71 if (err.status === HttpStatusCode.CONFLICT_409) {
72 this.error = $localize`This name already exists on this instance.`
76 this.error = err.message
81 onAvatarChange (formData: FormData) {
82 this.avatar = formData
89 onBannerChange (formData: FormData) {
90 this.banner = formData
101 getFormButtonTitle () {
102 return $localize`Create`
106 return this.form.value.name
109 private uploadAvatar () {
110 if (!this.avatar) return of(undefined)
112 return this.videoChannelService.changeVideoChannelImage(this.getUsername(), this.avatar, 'avatar')
115 private uploadBanner () {
116 if (!this.banner) return of(undefined)
118 return this.videoChannelService.changeVideoChannelImage(this.getUsername(), this.banner, 'banner')