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 { uploadErrorHandler } 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 { ServerConfig, 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: ServerConfig
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.getTmpConfig()
44 this.serverService.getConfig()
45 .subscribe(config => this.serverConfig = config)
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 videoChannelId = routeParams['videoChannelId']
57 this.videoChannelService.getVideoChannel(videoChannelId).subscribe(
58 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
70 err => this.error = err.message
76 if (this.paramsSub) this.paramsSub.unsubscribe()
80 this.error = undefined
82 const body = this.form.value
83 const videoChannelUpdate: VideoChannelUpdate = {
84 displayName: body['display-name'],
85 description: body.description || null,
86 support: body.support || null,
87 bulkVideosSupportUpdate: body.bulkVideosSupportUpdate || false
90 this.videoChannelService.updateVideoChannel(this.videoChannel.name, videoChannelUpdate).subscribe(
92 this.authService.refreshUserInformation()
94 this.notifier.success($localize`Video channel ${videoChannelUpdate.displayName} updated.`)
96 this.router.navigate([ '/my-library', 'video-channels' ])
99 err => this.error = err.message
103 onAvatarChange (formData: FormData) {
104 this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'avatar')
107 this.notifier.success($localize`Avatar changed.`)
109 this.videoChannel.updateAvatar(data.avatar)
112 (err: HttpErrorResponse) => uploadErrorHandler({
114 name: $localize`avatar`,
115 notifier: this.notifier
121 this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'avatar')
124 this.notifier.success($localize`Avatar deleted.`)
126 this.videoChannel.resetAvatar()
129 err => this.notifier.error(err.message)
133 onBannerChange (formData: FormData) {
134 this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'banner')
137 this.notifier.success($localize`Banner changed.`)
139 this.videoChannel.updateBanner(data.banner)
142 (err: HttpErrorResponse) => uploadErrorHandler({
144 name: $localize`banner`,
145 notifier: this.notifier
151 this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'banner')
154 this.notifier.success($localize`Banner deleted.`)
156 this.videoChannel.resetBanner()
159 err => this.notifier.error(err.message)
163 get maxAvatarSize () {
164 return this.serverConfig.avatar.file.size.max
167 get avatarExtensions () {
168 return this.serverConfig.avatar.file.extensions.join(',')
175 getFormButtonTitle () {
176 return $localize`Update`
179 isBulkUpdateVideosDisplayed () {
180 if (this.oldSupportField === undefined) return false
182 return this.oldSupportField !== this.form.value['support']