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