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