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