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