]>
Commit | Line | Data |
---|---|---|
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' | |
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 { 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' | |
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, AfterViewInit { | |
22 | error: string | |
23 | videoChannel = new VideoChannel({}) | |
24 | ||
25 | private avatar: FormData | |
26 | private banner: FormData | |
27 | ||
28 | constructor ( | |
29 | protected formReactiveService: FormReactiveService, | |
30 | private authService: AuthService, | |
31 | private notifier: Notifier, | |
32 | private router: Router, | |
33 | private videoChannelService: VideoChannelService, | |
34 | private hooks: HooksService | |
35 | ) { | |
36 | super() | |
37 | } | |
38 | ||
39 | ngOnInit () { | |
40 | this.buildForm({ | |
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 | |
45 | }) | |
46 | } | |
47 | ||
48 | ngAfterViewInit () { | |
49 | this.hooks.runAction('action:video-channel-create.init', 'video-channel') | |
50 | } | |
51 | ||
52 | formValidated () { | |
53 | this.error = undefined | |
54 | ||
55 | const body = this.form.value | |
56 | const videoChannelCreate: VideoChannelCreate = { | |
57 | name: body.name, | |
58 | displayName: body['display-name'], | |
59 | description: body.description || null, | |
60 | support: body.support || null | |
61 | } | |
62 | ||
63 | this.videoChannelService.createVideoChannel(videoChannelCreate) | |
64 | .pipe( | |
65 | switchMap(() => this.uploadAvatar()), | |
66 | switchMap(() => this.uploadBanner()) | |
67 | ).subscribe({ | |
68 | next: () => { | |
69 | this.authService.refreshUserInformation() | |
70 | ||
71 | this.notifier.success($localize`Video channel ${videoChannelCreate.displayName} created.`) | |
72 | this.router.navigate([ '/my-library', 'video-channels' ]) | |
73 | }, | |
74 | ||
75 | error: err => { | |
76 | if (err.status === HttpStatusCode.CONFLICT_409) { | |
77 | this.error = $localize`This name already exists on this instance.` | |
78 | return | |
79 | } | |
80 | ||
81 | this.error = err.message | |
82 | } | |
83 | }) | |
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 | } | |
97 | ||
98 | onBannerDelete () { | |
99 | this.banner = null | |
100 | } | |
101 | ||
102 | isCreation () { | |
103 | return true | |
104 | } | |
105 | ||
106 | getFormButtonTitle () { | |
107 | return $localize`Create` | |
108 | } | |
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 | } | |
125 | } |