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'
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'
18 selector: 'my-video-channel-update',
19 templateUrl: './video-channel-edit.component.html',
20 styleUrls: [ './video-channel-edit.component.scss' ]
22 export class VideoChannelUpdateComponent extends VideoChannelEdit implements OnInit, AfterViewInit, OnDestroy {
24 videoChannel: VideoChannel
26 private paramsSub: Subscription
27 private oldSupportField: string
28 private serverConfig: HTMLServerConfig
31 protected formReactiveService: FormReactiveService,
32 private authService: AuthService,
33 private notifier: Notifier,
34 private route: ActivatedRoute,
35 private videoChannelService: VideoChannelService,
36 private serverService: ServerService,
37 private redirectService: RedirectService,
38 private hooks: HooksService
44 this.serverConfig = this.serverService.getHTMLConfig()
47 'display-name': VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
48 description: VIDEO_CHANNEL_DESCRIPTION_VALIDATOR,
49 support: VIDEO_CHANNEL_SUPPORT_VALIDATOR,
50 bulkVideosSupportUpdate: null
53 this.paramsSub = this.route.params.subscribe(routeParams => {
54 const videoChannelName = routeParams['videoChannelName']
56 this.videoChannelService.getVideoChannel(videoChannelName)
58 next: videoChannelToUpdate => {
59 this.videoChannel = videoChannelToUpdate
61 this.hooks.runAction('action:video-channel-update.video-channel.loaded', 'video-channel', { videoChannel: this.videoChannel })
63 this.oldSupportField = videoChannelToUpdate.support
65 this.form.patchValue({
66 'display-name': videoChannelToUpdate.displayName,
67 description: videoChannelToUpdate.description,
68 support: videoChannelToUpdate.support
73 this.error = err.message
80 this.hooks.runAction('action:video-channel-update.init', 'video-channel')
84 if (this.paramsSub) this.paramsSub.unsubscribe()
88 this.error = undefined
90 const body = this.form.value
91 const videoChannelUpdate: VideoChannelUpdate = {
92 displayName: body['display-name'],
93 description: body.description || null,
94 support: body.support || null,
95 bulkVideosSupportUpdate: body.bulkVideosSupportUpdate || false
98 this.videoChannelService.updateVideoChannel(this.videoChannel.name, videoChannelUpdate)
101 this.authService.refreshUserInformation()
103 this.notifier.success($localize`Video channel ${videoChannelUpdate.displayName} updated.`)
105 this.redirectService.redirectToPreviousRoute('/c/' + this.videoChannel.name)
109 this.error = err.message
114 onAvatarChange (formData: FormData) {
115 this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'avatar')
118 this.notifier.success($localize`Avatar changed.`)
120 this.videoChannel.updateAvatar(data.avatars)
123 error: (err: HttpErrorResponse) => genericUploadErrorHandler({
125 name: $localize`avatar`,
126 notifier: this.notifier
132 this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'avatar')
135 this.notifier.success($localize`Avatar deleted.`)
137 this.videoChannel.resetAvatar()
140 error: err => this.notifier.error(err.message)
144 onBannerChange (formData: FormData) {
145 this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'banner')
148 this.notifier.success($localize`Banner changed.`)
150 this.videoChannel.updateBanner(data.banners)
153 error: (err: HttpErrorResponse) => genericUploadErrorHandler({
155 name: $localize`banner`,
156 notifier: this.notifier
162 this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'banner')
165 this.notifier.success($localize`Banner deleted.`)
167 this.videoChannel.resetBanner()
170 error: err => this.notifier.error(err.message)
174 get maxAvatarSize () {
175 return this.serverConfig.avatar.file.size.max
178 get avatarExtensions () {
179 return this.serverConfig.avatar.file.extensions.join(',')
186 getFormButtonTitle () {
187 return $localize`Update`
190 isBulkUpdateVideosDisplayed () {
191 if (this.oldSupportField === undefined) return false
193 return this.oldSupportField !== this.form.value['support']