1 import { Subscription } from 'rxjs'
2 import { Component, OnDestroy, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { AuthService, Notifier, ServerService } from '@app/core'
6 VIDEO_CHANNEL_DESCRIPTION_VALIDATOR,
7 VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
8 VIDEO_CHANNEL_SUPPORT_VALIDATOR
9 } from '@app/shared/form-validators/video-channel-validators'
10 import { FormValidatorService } from '@app/shared/shared-forms'
11 import { VideoChannel, VideoChannelService } from '@app/shared/shared-main'
12 import { ServerConfig, VideoChannelUpdate } from '@shared/models'
13 import { MyVideoChannelEdit } from './my-video-channel-edit'
14 import { HttpErrorResponse } from '@angular/common/http'
15 import { uploadErrorHandler } from '@app/helpers'
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 videoChannelToUpdate: 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.videoChannelToUpdate = 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.videoChannelToUpdate.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.changeVideoChannelAvatar(this.videoChannelToUpdate.name, formData)
107 this.notifier.success($localize`Avatar changed.`)
109 this.videoChannelToUpdate.updateAvatar(data.avatar)
112 (err: HttpErrorResponse) => uploadErrorHandler({
114 name: $localize`avatar`,
115 notifier: this.notifier
121 this.videoChannelService.deleteVideoChannelAvatar(this.videoChannelToUpdate.name)
124 this.notifier.success($localize`Avatar deleted.`)
126 this.videoChannelToUpdate.resetAvatar()
129 err => this.notifier.error(err.message)
133 get maxAvatarSize () {
134 return this.serverConfig.avatar.file.size.max
137 get avatarExtensions () {
138 return this.serverConfig.avatar.file.extensions.join(',')
145 getFormButtonTitle () {
146 return $localize`Update`
149 isBulkUpdateVideosDisplayed () {
150 if (this.oldSupportField === undefined) return false
152 return this.oldSupportField !== this.form.value['support']