Merge branch 'release/v1.3.0' 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 { 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   private oldSupportField: string
24
25   constructor (
26     protected formValidatorService: FormValidatorService,
27     private authService: AuthService,
28     private videoChannelValidatorsService: VideoChannelValidatorsService,
29     private notifier: Notifier,
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       bulkVideosSupportUpdate: null
45     })
46
47     this.paramsSub = this.route.params.subscribe(routeParams => {
48       const videoChannelId = routeParams['videoChannelId']
49
50       this.videoChannelService.getVideoChannel(videoChannelId).subscribe(
51         videoChannelToUpdate => {
52           this.videoChannelToUpdate = videoChannelToUpdate
53
54           this.oldSupportField = videoChannelToUpdate.support
55
56           this.form.patchValue({
57             'display-name': videoChannelToUpdate.displayName,
58             description: videoChannelToUpdate.description,
59             support: videoChannelToUpdate.support
60           })
61         },
62
63         err => this.error = err.message
64       )
65     })
66   }
67
68   ngOnDestroy () {
69     if (this.paramsSub) this.paramsSub.unsubscribe()
70   }
71
72   formValidated () {
73     this.error = undefined
74
75     const body = this.form.value
76     const videoChannelUpdate: VideoChannelUpdate = {
77       displayName: body['display-name'],
78       description: body.description || null,
79       support: body.support || null,
80       bulkVideosSupportUpdate: body.bulkVideosSupportUpdate || false
81     }
82
83     this.videoChannelService.updateVideoChannel(this.videoChannelToUpdate.name, videoChannelUpdate).subscribe(
84       () => {
85         this.authService.refreshUserInformation()
86
87         this.notifier.success(
88           this.i18n('Video channel {{videoChannelName}} updated.', { videoChannelName: videoChannelUpdate.displayName })
89         )
90
91         this.router.navigate([ '/my-account', 'video-channels' ])
92       },
93
94       err => this.error = err.message
95     )
96   }
97
98   onAvatarChange (formData: FormData) {
99     this.videoChannelService.changeVideoChannelAvatar(this.videoChannelToUpdate.name, formData)
100         .subscribe(
101           data => {
102             this.notifier.success(this.i18n('Avatar changed.'))
103
104             this.videoChannelToUpdate.updateAvatar(data.avatar)
105           },
106
107           err => this.notifier.error(err.message)
108         )
109   }
110
111   get maxAvatarSize () {
112     return this.serverService.getConfig().avatar.file.size.max
113   }
114
115   get avatarExtensions () {
116     return this.serverService.getConfig().avatar.file.extensions.join(',')
117   }
118
119   isCreation () {
120     return false
121   }
122
123   getFormButtonTitle () {
124     return this.i18n('Update')
125   }
126
127   isBulkUpdateVideosDisplayed () {
128     if (this.oldSupportField === undefined) return false
129
130     return this.oldSupportField !== this.form.value['support']
131   }
132 }