]>
Commit | Line | Data |
---|---|---|
1 | import { Subscription } from 'rxjs' | |
2 | import { HttpErrorResponse } from '@angular/common/http' | |
3 | import { AfterViewInit, Component, OnDestroy, OnInit } from '@angular/core' | |
4 | import { ActivatedRoute } from '@angular/router' | |
5 | import { AuthService, HooksService, Notifier, RedirectService, ServerService } from '@app/core' | |
6 | import { genericUploadErrorHandler } from '@app/helpers' | |
7 | import { | |
8 | VIDEO_CHANNEL_DESCRIPTION_VALIDATOR, | |
9 | VIDEO_CHANNEL_DISPLAY_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 { HTMLServerConfig, VideoChannelUpdate } from '@shared/models' | |
15 | import { VideoChannelEdit } from './video-channel-edit' | |
16 | import { shallowCopy } from '@shared/core-utils' | |
17 | ||
18 | @Component({ | |
19 | selector: 'my-video-channel-update', | |
20 | templateUrl: './video-channel-edit.component.html', | |
21 | styleUrls: [ './video-channel-edit.component.scss' ] | |
22 | }) | |
23 | export class VideoChannelUpdateComponent extends VideoChannelEdit implements OnInit, AfterViewInit, OnDestroy { | |
24 | error: string | |
25 | videoChannel: VideoChannel | |
26 | ||
27 | private paramsSub: Subscription | |
28 | private oldSupportField: string | |
29 | private serverConfig: HTMLServerConfig | |
30 | ||
31 | constructor ( | |
32 | protected formReactiveService: FormReactiveService, | |
33 | private authService: AuthService, | |
34 | private notifier: Notifier, | |
35 | private route: ActivatedRoute, | |
36 | private videoChannelService: VideoChannelService, | |
37 | private serverService: ServerService, | |
38 | private redirectService: RedirectService, | |
39 | private hooks: HooksService | |
40 | ) { | |
41 | super() | |
42 | } | |
43 | ||
44 | ngOnInit () { | |
45 | this.serverConfig = this.serverService.getHTMLConfig() | |
46 | ||
47 | this.buildForm({ | |
48 | 'display-name': VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR, | |
49 | 'description': VIDEO_CHANNEL_DESCRIPTION_VALIDATOR, | |
50 | 'support': VIDEO_CHANNEL_SUPPORT_VALIDATOR, | |
51 | 'bulkVideosSupportUpdate': null | |
52 | }) | |
53 | ||
54 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
55 | const videoChannelName = routeParams['videoChannelName'] | |
56 | ||
57 | this.videoChannelService.getVideoChannel(videoChannelName) | |
58 | .subscribe({ | |
59 | next: videoChannelToUpdate => { | |
60 | this.videoChannel = videoChannelToUpdate | |
61 | ||
62 | this.hooks.runAction('action:video-channel-update.video-channel.loaded', 'video-channel', { videoChannel: this.videoChannel }) | |
63 | ||
64 | this.oldSupportField = videoChannelToUpdate.support | |
65 | ||
66 | this.form.patchValue({ | |
67 | 'display-name': videoChannelToUpdate.displayName, | |
68 | 'description': videoChannelToUpdate.description, | |
69 | 'support': videoChannelToUpdate.support | |
70 | }) | |
71 | }, | |
72 | ||
73 | error: err => { | |
74 | this.error = err.message | |
75 | } | |
76 | }) | |
77 | }) | |
78 | } | |
79 | ||
80 | ngAfterViewInit () { | |
81 | this.hooks.runAction('action:video-channel-update.init', 'video-channel') | |
82 | } | |
83 | ||
84 | ngOnDestroy () { | |
85 | if (this.paramsSub) this.paramsSub.unsubscribe() | |
86 | } | |
87 | ||
88 | formValidated () { | |
89 | this.error = undefined | |
90 | ||
91 | const body = this.form.value | |
92 | const videoChannelUpdate: VideoChannelUpdate = { | |
93 | displayName: body['display-name'], | |
94 | description: body.description || null, | |
95 | support: body.support || null, | |
96 | bulkVideosSupportUpdate: body.bulkVideosSupportUpdate || false | |
97 | } | |
98 | ||
99 | this.videoChannelService.updateVideoChannel(this.videoChannel.name, videoChannelUpdate) | |
100 | .subscribe({ | |
101 | next: () => { | |
102 | this.authService.refreshUserInformation() | |
103 | ||
104 | this.notifier.success($localize`Video channel ${videoChannelUpdate.displayName} updated.`) | |
105 | ||
106 | this.redirectService.redirectToPreviousRoute('/c/' + this.videoChannel.name) | |
107 | }, | |
108 | ||
109 | error: err => { | |
110 | this.error = err.message | |
111 | } | |
112 | }) | |
113 | } | |
114 | ||
115 | onAvatarChange (formData: FormData) { | |
116 | this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'avatar') | |
117 | .subscribe({ | |
118 | next: data => { | |
119 | this.notifier.success($localize`Avatar changed.`) | |
120 | ||
121 | this.videoChannel.updateAvatar(data.avatars) | |
122 | ||
123 | // So my-actor-avatar component detects changes | |
124 | this.videoChannel = shallowCopy(this.videoChannel) | |
125 | }, | |
126 | ||
127 | error: (err: HttpErrorResponse) => genericUploadErrorHandler({ | |
128 | err, | |
129 | name: $localize`avatar`, | |
130 | notifier: this.notifier | |
131 | }) | |
132 | }) | |
133 | } | |
134 | ||
135 | onAvatarDelete () { | |
136 | this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'avatar') | |
137 | .subscribe({ | |
138 | next: () => { | |
139 | this.notifier.success($localize`Avatar deleted.`) | |
140 | ||
141 | this.videoChannel.resetAvatar() | |
142 | ||
143 | // So my-actor-avatar component detects changes | |
144 | this.videoChannel = shallowCopy(this.videoChannel) | |
145 | }, | |
146 | ||
147 | error: err => this.notifier.error(err.message) | |
148 | }) | |
149 | } | |
150 | ||
151 | onBannerChange (formData: FormData) { | |
152 | this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'banner') | |
153 | .subscribe({ | |
154 | next: data => { | |
155 | this.notifier.success($localize`Banner changed.`) | |
156 | ||
157 | this.videoChannel.updateBanner(data.banners) | |
158 | }, | |
159 | ||
160 | error: (err: HttpErrorResponse) => genericUploadErrorHandler({ | |
161 | err, | |
162 | name: $localize`banner`, | |
163 | notifier: this.notifier | |
164 | }) | |
165 | }) | |
166 | } | |
167 | ||
168 | onBannerDelete () { | |
169 | this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'banner') | |
170 | .subscribe({ | |
171 | next: () => { | |
172 | this.notifier.success($localize`Banner deleted.`) | |
173 | ||
174 | this.videoChannel.resetBanner() | |
175 | }, | |
176 | ||
177 | error: err => this.notifier.error(err.message) | |
178 | }) | |
179 | } | |
180 | ||
181 | get maxAvatarSize () { | |
182 | return this.serverConfig.avatar.file.size.max | |
183 | } | |
184 | ||
185 | get avatarExtensions () { | |
186 | return this.serverConfig.avatar.file.extensions.join(',') | |
187 | } | |
188 | ||
189 | isCreation () { | |
190 | return false | |
191 | } | |
192 | ||
193 | getFormButtonTitle () { | |
194 | return $localize`Update` | |
195 | } | |
196 | ||
197 | isBulkUpdateVideosDisplayed () { | |
198 | if (this.oldSupportField === undefined) return false | |
199 | ||
200 | return this.oldSupportField !== this.form.value['support'] | |
201 | } | |
202 | } |