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'
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'
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, OnDestroy {
24 videoChannel: VideoChannel
26 private paramsSub: Subscription
27 private oldSupportField: string
28 private serverConfig: HTMLServerConfig
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
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.oldSupportField = videoChannelToUpdate.support
63 this.form.patchValue({
64 'display-name': videoChannelToUpdate.displayName,
65 description: videoChannelToUpdate.description,
66 support: videoChannelToUpdate.support
71 this.error = err.message
78 if (this.paramsSub) this.paramsSub.unsubscribe()
82 this.error = undefined
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
92 this.videoChannelService.updateVideoChannel(this.videoChannel.name, videoChannelUpdate)
95 this.authService.refreshUserInformation()
97 this.notifier.success($localize`Video channel ${videoChannelUpdate.displayName} updated.`)
99 this.redirectService.redirectToPreviousRoute('/c/' + this.videoChannel.name)
103 this.error = err.message
108 onAvatarChange (formData: FormData) {
109 this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'avatar')
112 this.notifier.success($localize`Avatar changed.`)
114 this.videoChannel.updateAvatar(data.avatars)
117 error: (err: HttpErrorResponse) => genericUploadErrorHandler({
119 name: $localize`avatar`,
120 notifier: this.notifier
126 this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'avatar')
129 this.notifier.success($localize`Avatar deleted.`)
131 this.videoChannel.resetAvatar()
134 error: err => this.notifier.error(err.message)
138 onBannerChange (formData: FormData) {
139 this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'banner')
142 this.notifier.success($localize`Banner changed.`)
144 this.videoChannel.updateBanner(data.banners)
147 error: (err: HttpErrorResponse) => genericUploadErrorHandler({
149 name: $localize`banner`,
150 notifier: this.notifier
156 this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'banner')
159 this.notifier.success($localize`Banner deleted.`)
161 this.videoChannel.resetBanner()
164 error: err => this.notifier.error(err.message)
168 get maxAvatarSize () {
169 return this.serverConfig.avatar.file.size.max
172 get avatarExtensions () {
173 return this.serverConfig.avatar.file.extensions.join(',')
180 getFormButtonTitle () {
181 return $localize`Update`
184 isBulkUpdateVideosDisplayed () {
185 if (this.oldSupportField === undefined) return false
187 return this.oldSupportField !== this.form.value['support']