]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-library/+my-video-channels/my-video-channel-update.component.ts
Merge branch 'master' into release/3.3.0
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-library / +my-video-channels / my-video-channel-update.component.ts
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'
7 import {
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'
16
17 @Component({
18 selector: 'my-video-channel-update',
19 templateUrl: './my-video-channel-edit.component.html',
20 styleUrls: [ './my-video-channel-edit.component.scss' ]
21 })
22 export class MyVideoChannelUpdateComponent extends MyVideoChannelEdit implements OnInit, OnDestroy {
23 error: string
24 videoChannel: VideoChannel
25
26 private paramsSub: Subscription
27 private oldSupportField: string
28 private serverConfig: HTMLServerConfig
29
30 constructor (
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 ) {
39 super()
40 }
41
42 ngOnInit () {
43 this.serverConfig = this.serverService.getHTMLConfig()
44
45 this.buildForm({
46 'display-name': VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
47 description: VIDEO_CHANNEL_DESCRIPTION_VALIDATOR,
48 support: VIDEO_CHANNEL_SUPPORT_VALIDATOR,
49 bulkVideosSupportUpdate: null
50 })
51
52 this.paramsSub = this.route.params.subscribe(routeParams => {
53 const videoChannelId = routeParams['videoChannelId']
54
55 this.videoChannelService.getVideoChannel(videoChannelId).subscribe(
56 videoChannelToUpdate => {
57 this.videoChannel = videoChannelToUpdate
58
59 this.oldSupportField = videoChannelToUpdate.support
60
61 this.form.patchValue({
62 'display-name': videoChannelToUpdate.displayName,
63 description: videoChannelToUpdate.description,
64 support: videoChannelToUpdate.support
65 })
66 },
67
68 err => this.error = err.message
69 )
70 })
71 }
72
73 ngOnDestroy () {
74 if (this.paramsSub) this.paramsSub.unsubscribe()
75 }
76
77 formValidated () {
78 this.error = undefined
79
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
86 }
87
88 this.videoChannelService.updateVideoChannel(this.videoChannel.name, videoChannelUpdate).subscribe(
89 () => {
90 this.authService.refreshUserInformation()
91
92 this.notifier.success($localize`Video channel ${videoChannelUpdate.displayName} updated.`)
93
94 this.router.navigate([ '/my-library', 'video-channels' ])
95 },
96
97 err => this.error = err.message
98 )
99 }
100
101 onAvatarChange (formData: FormData) {
102 this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'avatar')
103 .subscribe(
104 data => {
105 this.notifier.success($localize`Avatar changed.`)
106
107 this.videoChannel.updateAvatar(data.avatar)
108 },
109
110 (err: HttpErrorResponse) => genericUploadErrorHandler({
111 err,
112 name: $localize`avatar`,
113 notifier: this.notifier
114 })
115 )
116 }
117
118 onAvatarDelete () {
119 this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'avatar')
120 .subscribe(
121 data => {
122 this.notifier.success($localize`Avatar deleted.`)
123
124 this.videoChannel.resetAvatar()
125 },
126
127 err => this.notifier.error(err.message)
128 )
129 }
130
131 onBannerChange (formData: FormData) {
132 this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'banner')
133 .subscribe(
134 data => {
135 this.notifier.success($localize`Banner changed.`)
136
137 this.videoChannel.updateBanner(data.banner)
138 },
139
140 (err: HttpErrorResponse) => genericUploadErrorHandler({
141 err,
142 name: $localize`banner`,
143 notifier: this.notifier
144 })
145 )
146 }
147
148 onBannerDelete () {
149 this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'banner')
150 .subscribe(
151 data => {
152 this.notifier.success($localize`Banner deleted.`)
153
154 this.videoChannel.resetBanner()
155 },
156
157 err => this.notifier.error(err.message)
158 )
159 }
160
161 get maxAvatarSize () {
162 return this.serverConfig.avatar.file.size.max
163 }
164
165 get avatarExtensions () {
166 return this.serverConfig.avatar.file.extensions.join(',')
167 }
168
169 isCreation () {
170 return false
171 }
172
173 getFormButtonTitle () {
174 return $localize`Update`
175 }
176
177 isBulkUpdateVideosDisplayed () {
178 if (this.oldSupportField === undefined) return false
179
180 return this.oldSupportField !== this.form.value['support']
181 }
182 }