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'
16 import { shallowCopy } from '@shared/core-utils'
19 selector: 'my-video-channel-update',
20 templateUrl: './video-channel-edit.component.html',
21 styleUrls: [ './video-channel-edit.component.scss' ]
23 export class VideoChannelUpdateComponent extends VideoChannelEdit implements OnInit, AfterViewInit, OnDestroy {
25 videoChannel: VideoChannel
27 private paramsSub: Subscription
28 private oldSupportField: string
29 private serverConfig: HTMLServerConfig
32 protected formReactiveService: FormReactiveService,
33 private authService: AuthService,
34 private notifier: Notifier,
35 private route: ActivatedRoute,
36 private videoChannelService: VideoChannelService,
37 private serverService: ServerService,
38 private redirectService: RedirectService,
39 private hooks: HooksService
45 this.serverConfig = this.serverService.getHTMLConfig()
48 'display-name': VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
49 'description': VIDEO_CHANNEL_DESCRIPTION_VALIDATOR,
50 'support': VIDEO_CHANNEL_SUPPORT_VALIDATOR,
51 'bulkVideosSupportUpdate': null
54 this.paramsSub = this.route.params.subscribe(routeParams => {
55 const videoChannelName = routeParams['videoChannelName']
57 this.videoChannelService.getVideoChannel(videoChannelName)
59 next: videoChannelToUpdate => {
60 this.videoChannel = videoChannelToUpdate
62 this.hooks.runAction('action:video-channel-update.video-channel.loaded', 'video-channel', { videoChannel: this.videoChannel })
64 this.oldSupportField = videoChannelToUpdate.support
66 this.form.patchValue({
67 'display-name': videoChannelToUpdate.displayName,
68 'description': videoChannelToUpdate.description,
69 'support': videoChannelToUpdate.support
74 this.error = err.message
81 this.hooks.runAction('action:video-channel-update.init', 'video-channel')
85 if (this.paramsSub) this.paramsSub.unsubscribe()
89 this.error = undefined
91 const body = this.form.value
92 const videoChannelUpdate: VideoChannelUpdate = {
93 displayName: body['display-name'],
94 description: body.description || null,
95 support: body.support || null,
96 bulkVideosSupportUpdate: body.bulkVideosSupportUpdate || false
99 this.videoChannelService.updateVideoChannel(this.videoChannel.name, videoChannelUpdate)
102 this.authService.refreshUserInformation()
104 this.notifier.success($localize`Video channel ${videoChannelUpdate.displayName} updated.`)
106 this.redirectService.redirectToPreviousRoute('/c/' + this.videoChannel.name)
110 this.error = err.message
115 onAvatarChange (formData: FormData) {
116 this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'avatar')
119 this.notifier.success($localize`Avatar changed.`)
121 this.videoChannel.updateAvatar(data.avatars)
123 // So my-actor-avatar component detects changes
124 this.videoChannel = shallowCopy(this.videoChannel)
127 error: (err: HttpErrorResponse) => genericUploadErrorHandler({
129 name: $localize`avatar`,
130 notifier: this.notifier
136 this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'avatar')
139 this.notifier.success($localize`Avatar deleted.`)
141 this.videoChannel.resetAvatar()
143 // So my-actor-avatar component detects changes
144 this.videoChannel = shallowCopy(this.videoChannel)
147 error: err => this.notifier.error(err.message)
151 onBannerChange (formData: FormData) {
152 this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'banner')
155 this.notifier.success($localize`Banner changed.`)
157 this.videoChannel.updateBanner(data.banners)
160 error: (err: HttpErrorResponse) => genericUploadErrorHandler({
162 name: $localize`banner`,
163 notifier: this.notifier
169 this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'banner')
172 this.notifier.success($localize`Banner deleted.`)
174 this.videoChannel.resetBanner()
177 error: err => this.notifier.error(err.message)
181 get maxAvatarSize () {
182 return this.serverConfig.avatar.file.size.max
185 get avatarExtensions () {
186 return this.serverConfig.avatar.file.extensions.join(',')
193 getFormButtonTitle () {
194 return $localize`Update`
197 isBulkUpdateVideosDisplayed () {
198 if (this.oldSupportField === undefined) return false
200 return this.oldSupportField !== this.form.value['support']