1 import { of } from 'rxjs'
2 import { switchMap } from 'rxjs/operators'
3 import { AfterViewInit, Component, OnInit } from '@angular/core'
4 import { Router } from '@angular/router'
5 import { AuthService, HooksService, 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 { FormReactiveService } from '@app/shared/shared-forms'
13 import { VideoChannel, VideoChannelService } from '@app/shared/shared-main'
14 import { HttpStatusCode, VideoChannelCreate } from '@shared/models'
15 import { VideoChannelEdit } from './video-channel-edit'
18 templateUrl: './video-channel-edit.component.html',
19 styleUrls: [ './video-channel-edit.component.scss' ]
21 export class VideoChannelCreateComponent extends VideoChannelEdit implements OnInit, AfterViewInit {
23 videoChannel = new VideoChannel({})
25 private avatar: FormData
26 private banner: FormData
29 protected formReactiveService: FormReactiveService,
30 private authService: AuthService,
31 private notifier: Notifier,
32 private router: Router,
33 private videoChannelService: VideoChannelService,
34 private hooks: HooksService
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.hooks.runAction('action:video-channel-create.init', 'video-channel')
53 this.error = undefined
55 const body = this.form.value
56 const videoChannelCreate: VideoChannelCreate = {
58 displayName: body['display-name'],
59 description: body.description || null,
60 support: body.support || null
63 this.videoChannelService.createVideoChannel(videoChannelCreate)
65 switchMap(() => this.uploadAvatar()),
66 switchMap(() => this.uploadBanner())
69 this.authService.refreshUserInformation()
71 this.notifier.success($localize`Video channel ${videoChannelCreate.displayName} created.`)
72 this.router.navigate([ '/my-library', 'video-channels' ])
76 if (err.status === HttpStatusCode.CONFLICT_409) {
77 this.error = $localize`This name already exists on this instance.`
81 this.error = err.message
86 onAvatarChange (formData: FormData) {
87 this.avatar = formData
94 onBannerChange (formData: FormData) {
95 this.banner = formData
106 getFormButtonTitle () {
107 return $localize`Create`
111 return this.form.value.name
114 private uploadAvatar () {
115 if (!this.avatar) return of(undefined)
117 return this.videoChannelService.changeVideoChannelImage(this.getUsername(), this.avatar, 'avatar')
120 private uploadBanner () {
121 if (!this.banner) return of(undefined)
123 return this.videoChannelService.changeVideoChannelImage(this.getUsername(), this.banner, 'banner')