]>
Commit | Line | Data |
---|---|---|
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' | |
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' | |
13 | import { VideoChannel, VideoChannelService } from '@app/shared/shared-main' | |
14 | import { HttpStatusCode, VideoChannelCreate } from '@shared/models' | |
15 | import { VideoChannelEdit } from './video-channel-edit' | |
16 | ||
17 | @Component({ | |
18 | templateUrl: './video-channel-edit.component.html', | |
19 | styleUrls: [ './video-channel-edit.component.scss' ] | |
20 | }) | |
21 | export class VideoChannelCreateComponent extends VideoChannelEdit implements OnInit { | |
22 | error: string | |
23 | videoChannel = new VideoChannel({}) | |
24 | ||
25 | private avatar: FormData | |
26 | private banner: FormData | |
27 | ||
28 | constructor ( | |
29 | protected formValidatorService: FormValidatorService, | |
30 | private authService: AuthService, | |
31 | private notifier: Notifier, | |
32 | private router: Router, | |
33 | private videoChannelService: VideoChannelService | |
34 | ) { | |
35 | super() | |
36 | } | |
37 | ||
38 | ngOnInit () { | |
39 | this.buildForm({ | |
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 | |
44 | }) | |
45 | } | |
46 | ||
47 | formValidated () { | |
48 | this.error = undefined | |
49 | ||
50 | const body = this.form.value | |
51 | const videoChannelCreate: VideoChannelCreate = { | |
52 | name: body.name, | |
53 | displayName: body['display-name'], | |
54 | description: body.description || null, | |
55 | support: body.support || null | |
56 | } | |
57 | ||
58 | this.videoChannelService.createVideoChannel(videoChannelCreate) | |
59 | .pipe( | |
60 | switchMap(() => this.uploadAvatar()), | |
61 | switchMap(() => this.uploadBanner()) | |
62 | ).subscribe({ | |
63 | next: () => { | |
64 | this.authService.refreshUserInformation() | |
65 | ||
66 | this.notifier.success($localize`Video channel ${videoChannelCreate.displayName} created.`) | |
67 | this.router.navigate([ '/my-library', 'video-channels' ]) | |
68 | }, | |
69 | ||
70 | error: err => { | |
71 | if (err.status === HttpStatusCode.CONFLICT_409) { | |
72 | this.error = $localize`This name already exists on this instance.` | |
73 | return | |
74 | } | |
75 | ||
76 | this.error = err.message | |
77 | } | |
78 | }) | |
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 | } | |
92 | ||
93 | onBannerDelete () { | |
94 | this.banner = null | |
95 | } | |
96 | ||
97 | isCreation () { | |
98 | return true | |
99 | } | |
100 | ||
101 | getFormButtonTitle () { | |
102 | return $localize`Create` | |
103 | } | |
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 | } | |
120 | } |