Merge branch 'release/v1.2.0'
[oweals/peertube.git] / client / src / app / +my-account / my-account-video-channels / my-account-video-channel-update.component.ts
1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { AuthService, Notifier, ServerService } from '@app/core'
4 import { MyAccountVideoChannelEdit } from './my-account-video-channel-edit'
5 import { VideoChannelUpdate } from '../../../../../shared/models/videos'
6 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
7 import { Subscription } from 'rxjs'
8 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
9 import { I18n } from '@ngx-translate/i18n-polyfill'
10 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
11 import { VideoChannelValidatorsService } from '@app/shared/forms/form-validators/video-channel-validators.service'
12
13 @Component({
14   selector: 'my-account-video-channel-update',
15   templateUrl: './my-account-video-channel-edit.component.html',
16   styleUrls: [ './my-account-video-channel-edit.component.scss' ]
17 })
18 export class MyAccountVideoChannelUpdateComponent extends MyAccountVideoChannelEdit implements OnInit, OnDestroy {
19   error: string
20   videoChannelToUpdate: VideoChannel
21
22   private paramsSub: Subscription
23
24   constructor (
25     protected formValidatorService: FormValidatorService,
26     private authService: AuthService,
27     private videoChannelValidatorsService: VideoChannelValidatorsService,
28     private notifier: Notifier,
29     private router: Router,
30     private route: ActivatedRoute,
31     private videoChannelService: VideoChannelService,
32     private i18n: I18n,
33     private serverService: ServerService
34   ) {
35     super()
36   }
37
38   ngOnInit () {
39     this.buildForm({
40       'display-name': this.videoChannelValidatorsService.VIDEO_CHANNEL_DISPLAY_NAME,
41       description: this.videoChannelValidatorsService.VIDEO_CHANNEL_DESCRIPTION,
42       support: this.videoChannelValidatorsService.VIDEO_CHANNEL_SUPPORT
43     })
44
45     this.paramsSub = this.route.params.subscribe(routeParams => {
46       const videoChannelId = routeParams['videoChannelId']
47
48       this.videoChannelService.getVideoChannel(videoChannelId).subscribe(
49         videoChannelToUpdate => {
50           this.videoChannelToUpdate = videoChannelToUpdate
51
52           this.form.patchValue({
53             'display-name': videoChannelToUpdate.displayName,
54             description: videoChannelToUpdate.description,
55             support: videoChannelToUpdate.support
56           })
57         },
58
59         err => this.error = err.message
60       )
61     })
62   }
63
64   ngOnDestroy () {
65     if (this.paramsSub) this.paramsSub.unsubscribe()
66   }
67
68   formValidated () {
69     this.error = undefined
70
71     const body = this.form.value
72     const videoChannelUpdate: VideoChannelUpdate = {
73       displayName: body['display-name'],
74       description: body.description || null,
75       support: body.support || null
76     }
77
78     this.videoChannelService.updateVideoChannel(this.videoChannelToUpdate.name, videoChannelUpdate).subscribe(
79       () => {
80         this.authService.refreshUserInformation()
81
82         this.notifier.success(
83           this.i18n('Video channel {{videoChannelName}} updated.', { videoChannelName: videoChannelUpdate.displayName })
84         )
85
86         this.router.navigate([ '/my-account', 'video-channels' ])
87       },
88
89       err => this.error = err.message
90     )
91   }
92
93   onAvatarChange (formData: FormData) {
94     this.videoChannelService.changeVideoChannelAvatar(this.videoChannelToUpdate.name, formData)
95         .subscribe(
96           data => {
97             this.notifier.success(this.i18n('Avatar changed.'))
98
99             this.videoChannelToUpdate.updateAvatar(data.avatar)
100           },
101
102           err => this.notifier.error(err.message)
103         )
104   }
105
106   get maxAvatarSize () {
107     return this.serverService.getConfig().avatar.file.size.max
108   }
109
110   get avatarExtensions () {
111     return this.serverService.getConfig().avatar.file.extensions.join(',')
112   }
113
114   isCreation () {
115     return false
116   }
117
118   getFormButtonTitle () {
119     return this.i18n('Update')
120   }
121 }