Merge branch 'master' into develop
[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 { NotificationsService } from 'angular2-notifications'
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 { AuthService, ServerService } from '@app/core'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
12 import { VideoChannelValidatorsService } from '@app/shared/forms/form-validators/video-channel-validators.service'
13
14 @Component({
15   selector: 'my-account-video-channel-update',
16   templateUrl: './my-account-video-channel-edit.component.html',
17   styleUrls: [ './my-account-video-channel-edit.component.scss' ]
18 })
19 export class MyAccountVideoChannelUpdateComponent extends MyAccountVideoChannelEdit implements OnInit, OnDestroy {
20   error: string
21   videoChannelToUpdate: VideoChannel
22
23   private paramsSub: Subscription
24
25   constructor (
26     protected formValidatorService: FormValidatorService,
27     private authService: AuthService,
28     private videoChannelValidatorsService: VideoChannelValidatorsService,
29     private notificationsService: NotificationsService,
30     private router: Router,
31     private route: ActivatedRoute,
32     private videoChannelService: VideoChannelService,
33     private i18n: I18n,
34     private serverService: ServerService
35   ) {
36     super()
37   }
38
39   ngOnInit () {
40     this.buildForm({
41       'display-name': this.videoChannelValidatorsService.VIDEO_CHANNEL_DISPLAY_NAME,
42       description: this.videoChannelValidatorsService.VIDEO_CHANNEL_DESCRIPTION,
43       support: this.videoChannelValidatorsService.VIDEO_CHANNEL_SUPPORT
44     })
45
46     this.paramsSub = this.route.params.subscribe(routeParams => {
47       const videoChannelId = routeParams['videoChannelId']
48
49       this.videoChannelService.getVideoChannel(videoChannelId).subscribe(
50         videoChannelToUpdate => {
51           this.videoChannelToUpdate = videoChannelToUpdate
52
53           this.form.patchValue({
54             'display-name': videoChannelToUpdate.displayName,
55             description: videoChannelToUpdate.description,
56             support: videoChannelToUpdate.support
57           })
58         },
59
60         err => this.error = err.message
61       )
62     })
63   }
64
65   ngOnDestroy () {
66     if (this.paramsSub) this.paramsSub.unsubscribe()
67   }
68
69   formValidated () {
70     this.error = undefined
71
72     const body = this.form.value
73     const videoChannelUpdate: VideoChannelUpdate = {
74       displayName: body['display-name'],
75       description: body.description || null,
76       support: body.support || null
77     }
78
79     this.videoChannelService.updateVideoChannel(this.videoChannelToUpdate.name, videoChannelUpdate).subscribe(
80       () => {
81         this.authService.refreshUserInformation()
82         this.notificationsService.success(
83           this.i18n('Success'),
84           this.i18n('Video channel {{videoChannelName}} updated.', { videoChannelName: videoChannelUpdate.displayName })
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.notificationsService.success(this.i18n('Success'), this.i18n('Avatar changed.'))
98
99             this.videoChannelToUpdate.updateAvatar(data.avatar)
100           },
101
102           err => this.notificationsService.error(this.i18n('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 }