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