]>
Commit | Line | Data |
---|---|---|
c0e8b12e C |
1 | import { of } from 'rxjs' |
2 | import { switchMap } from 'rxjs/operators' | |
9ca0f688 | 3 | import { AfterViewInit, Component, OnInit } from '@angular/core' |
08c1efbe | 4 | import { Router } from '@angular/router' |
9ca0f688 | 5 | import { AuthService, HooksService, Notifier } from '@app/core' |
7ed1edbb C |
6 | import { |
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' | |
5c5bcea2 | 12 | import { FormReactiveService } from '@app/shared/shared-forms' |
27ec473f | 13 | import { VideoChannel, VideoChannelService } from '@app/shared/shared-main' |
c0e8b12e | 14 | import { HttpStatusCode, VideoChannelCreate } from '@shared/models' |
a37e9e74 | 15 | import { VideoChannelEdit } from './video-channel-edit' |
08c1efbe C |
16 | |
17 | @Component({ | |
a37e9e74 | 18 | templateUrl: './video-channel-edit.component.html', |
19 | styleUrls: [ './video-channel-edit.component.scss' ] | |
08c1efbe | 20 | }) |
9ca0f688 | 21 | export class VideoChannelCreateComponent extends VideoChannelEdit implements OnInit, AfterViewInit { |
08c1efbe | 22 | error: string |
27ec473f C |
23 | videoChannel = new VideoChannel({}) |
24 | ||
25 | private avatar: FormData | |
26 | private banner: FormData | |
08c1efbe | 27 | |
08c1efbe | 28 | constructor ( |
5c5bcea2 | 29 | protected formReactiveService: FormReactiveService, |
95166f9a | 30 | private authService: AuthService, |
f8b2c1b4 | 31 | private notifier: Notifier, |
08c1efbe | 32 | private router: Router, |
9ca0f688 C |
33 | private videoChannelService: VideoChannelService, |
34 | private hooks: HooksService | |
9df52d66 | 35 | ) { |
08c1efbe C |
36 | super() |
37 | } | |
38 | ||
08c1efbe | 39 | ngOnInit () { |
d18d6478 | 40 | this.buildForm({ |
d0fbc9fd | 41 | 'name': VIDEO_CHANNEL_NAME_VALIDATOR, |
7ed1edbb | 42 | 'display-name': VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR, |
d0fbc9fd C |
43 | 'description': VIDEO_CHANNEL_DESCRIPTION_VALIDATOR, |
44 | 'support': VIDEO_CHANNEL_SUPPORT_VALIDATOR | |
d18d6478 | 45 | }) |
08c1efbe C |
46 | } |
47 | ||
9ca0f688 C |
48 | ngAfterViewInit () { |
49 | this.hooks.runAction('action:video-channel-create.init', 'video-channel') | |
50 | } | |
51 | ||
08c1efbe C |
52 | formValidated () { |
53 | this.error = undefined | |
54 | ||
55 | const body = this.form.value | |
56 | const videoChannelCreate: VideoChannelCreate = { | |
8a19bee1 | 57 | name: body.name, |
08c1efbe | 58 | displayName: body['display-name'], |
360329cc C |
59 | description: body.description || null, |
60 | support: body.support || null | |
08c1efbe C |
61 | } |
62 | ||
27ec473f C |
63 | this.videoChannelService.createVideoChannel(videoChannelCreate) |
64 | .pipe( | |
65 | switchMap(() => this.uploadAvatar()), | |
66 | switchMap(() => this.uploadBanner()) | |
1378c0d3 C |
67 | ).subscribe({ |
68 | next: () => { | |
27ec473f C |
69 | this.authService.refreshUserInformation() |
70 | ||
71 | this.notifier.success($localize`Video channel ${videoChannelCreate.displayName} created.`) | |
9df52d66 | 72 | this.router.navigate([ '/my-library', 'video-channels' ]) |
27ec473f | 73 | }, |
f8b2c1b4 | 74 | |
1378c0d3 | 75 | error: err => { |
27ec473f C |
76 | if (err.status === HttpStatusCode.CONFLICT_409) { |
77 | this.error = $localize`This name already exists on this instance.` | |
78 | return | |
79 | } | |
08c1efbe | 80 | |
27ec473f | 81 | this.error = err.message |
601527d7 | 82 | } |
1378c0d3 | 83 | }) |
27ec473f C |
84 | } |
85 | ||
86 | onAvatarChange (formData: FormData) { | |
87 | this.avatar = formData | |
88 | } | |
89 | ||
90 | onAvatarDelete () { | |
91 | this.avatar = null | |
92 | } | |
93 | ||
94 | onBannerChange (formData: FormData) { | |
95 | this.banner = formData | |
96 | } | |
601527d7 | 97 | |
27ec473f C |
98 | onBannerDelete () { |
99 | this.banner = null | |
08c1efbe C |
100 | } |
101 | ||
102 | isCreation () { | |
103 | return true | |
104 | } | |
105 | ||
106 | getFormButtonTitle () { | |
66357162 | 107 | return $localize`Create` |
08c1efbe | 108 | } |
27ec473f C |
109 | |
110 | getUsername () { | |
111 | return this.form.value.name | |
112 | } | |
113 | ||
114 | private uploadAvatar () { | |
115 | if (!this.avatar) return of(undefined) | |
116 | ||
117 | return this.videoChannelService.changeVideoChannelImage(this.getUsername(), this.avatar, 'avatar') | |
118 | } | |
119 | ||
120 | private uploadBanner () { | |
121 | if (!this.banner) return of(undefined) | |
122 | ||
123 | return this.videoChannelService.changeVideoChannelImage(this.getUsername(), this.banner, 'banner') | |
124 | } | |
08c1efbe | 125 | } |