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'
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'
18 selector: 'my-video-channel-update',
19 templateUrl: './my-video-channel-edit.component.html',
20 styleUrls: [ './my-video-channel-edit.component.scss' ]
22 export class MyVideoChannelUpdateComponent extends MyVideoChannelEdit 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
43 this.serverConfig = this.serverService.getHTMLConfig()
46 'display-name': VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
47 description: VIDEO_CHANNEL_DESCRIPTION_VALIDATOR,
48 support: VIDEO_CHANNEL_SUPPORT_VALIDATOR,
49 bulkVideosSupportUpdate: null
52 this.paramsSub = this.route.params.subscribe(routeParams => {
53 const videoChannelId = routeParams['videoChannelId']
55 this.videoChannelService.getVideoChannel(videoChannelId).subscribe(
56 videoChannelToUpdate => {
57 this.videoChannel = videoChannelToUpdate
59 this.oldSupportField = videoChannelToUpdate.support
61 this.form.patchValue({
62 'display-name': videoChannelToUpdate.displayName,
63 description: videoChannelToUpdate.description,
64 support: videoChannelToUpdate.support
68 err => this.error = err.message
74 if (this.paramsSub) this.paramsSub.unsubscribe()
78 this.error = undefined
80 const body = this.form.value
81 const videoChannelUpdate: VideoChannelUpdate = {
82 displayName: body['display-name'],
83 description: body.description || null,
84 support: body.support || null,
85 bulkVideosSupportUpdate: body.bulkVideosSupportUpdate || false
88 this.videoChannelService.updateVideoChannel(this.videoChannel.name, videoChannelUpdate).subscribe(
90 this.authService.refreshUserInformation()
92 this.notifier.success($localize`Video channel ${videoChannelUpdate.displayName} updated.`)
94 this.router.navigate([ '/my-library', 'video-channels' ])
97 err => this.error = err.message
101 onAvatarChange (formData: FormData) {
102 this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'avatar')
105 this.notifier.success($localize`Avatar changed.`)
107 this.videoChannel.updateAvatar(data.avatar)
110 (err: HttpErrorResponse) => genericUploadErrorHandler({
112 name: $localize`avatar`,
113 notifier: this.notifier
119 this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'avatar')
122 this.notifier.success($localize`Avatar deleted.`)
124 this.videoChannel.resetAvatar()
127 err => this.notifier.error(err.message)
131 onBannerChange (formData: FormData) {
132 this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'banner')
135 this.notifier.success($localize`Banner changed.`)
137 this.videoChannel.updateBanner(data.banner)
140 (err: HttpErrorResponse) => genericUploadErrorHandler({
142 name: $localize`banner`,
143 notifier: this.notifier
149 this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'banner')
152 this.notifier.success($localize`Banner deleted.`)
154 this.videoChannel.resetBanner()
157 err => this.notifier.error(err.message)
161 get maxAvatarSize () {
162 return this.serverConfig.avatar.file.size.max
165 get avatarExtensions () {
166 return this.serverConfig.avatar.file.extensions.join(',')
173 getFormButtonTitle () {
174 return $localize`Update`
177 isBulkUpdateVideosDisplayed () {
178 if (this.oldSupportField === undefined) return false
180 return this.oldSupportField !== this.form.value['support']