Fix i18n in components
[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 { FormBuilder, FormGroup } from '@angular/forms'
6 import { VideoChannelUpdate } from '../../../../../shared/models/videos'
7 import {
8   VIDEO_CHANNEL_DESCRIPTION,
9   VIDEO_CHANNEL_DISPLAY_NAME,
10   VIDEO_CHANNEL_SUPPORT
11 } from '@app/shared/forms/form-validators/video-channel'
12 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
13 import { Subscription } from 'rxjs'
14 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
15 import { AuthService } from '@app/core'
16 import { I18n } from '@ngx-translate/i18n-polyfill'
17
18 @Component({
19   selector: 'my-account-video-channel-update',
20   templateUrl: './my-account-video-channel-edit.component.html',
21   styleUrls: [ './my-account-video-channel-edit.component.scss' ]
22 })
23 export class MyAccountVideoChannelUpdateComponent extends MyAccountVideoChannelEdit implements OnInit, OnDestroy {
24   error: string
25
26   form: FormGroup
27   formErrors = {
28     'display-name': '',
29     'description': '',
30     'support': ''
31   }
32   validationMessages = {
33     'display-name': VIDEO_CHANNEL_DISPLAY_NAME.MESSAGES,
34     'description': VIDEO_CHANNEL_DESCRIPTION.MESSAGES,
35     'support': VIDEO_CHANNEL_SUPPORT.MESSAGES
36   }
37
38   private videoChannelToUpdate: VideoChannel
39   private paramsSub: Subscription
40
41   constructor (
42     private authService: AuthService,
43     private notificationsService: NotificationsService,
44     private router: Router,
45     private route: ActivatedRoute,
46     private formBuilder: FormBuilder,
47     private videoChannelService: VideoChannelService,
48     private i18n: I18n
49   ) {
50     super()
51   }
52
53   buildForm () {
54     this.form = this.formBuilder.group({
55       'display-name': [ '', VIDEO_CHANNEL_DISPLAY_NAME.VALIDATORS ],
56       description: [ '', VIDEO_CHANNEL_DESCRIPTION.VALIDATORS ],
57       support: [ '', VIDEO_CHANNEL_SUPPORT.VALIDATORS ]
58     })
59
60     this.form.valueChanges.subscribe(data => this.onValueChanged(data))
61   }
62
63   ngOnInit () {
64     this.buildForm()
65
66     this.paramsSub = this.route.params.subscribe(routeParams => {
67       const videoChannelId = routeParams['videoChannelId']
68
69       this.videoChannelService.getVideoChannel(videoChannelId).subscribe(
70         videoChannelToUpdate => {
71           this.videoChannelToUpdate = videoChannelToUpdate
72
73           this.form.patchValue({
74             'display-name': videoChannelToUpdate.displayName,
75             description: videoChannelToUpdate.description,
76             support: videoChannelToUpdate.support
77           })
78         },
79
80         err => this.error = err.message
81       )
82     })
83   }
84
85   ngOnDestroy () {
86     if (this.paramsSub) this.paramsSub.unsubscribe()
87   }
88
89   formValidated () {
90     this.error = undefined
91
92     const body = this.form.value
93     const videoChannelUpdate: VideoChannelUpdate = {
94       displayName: body['display-name'],
95       description: body.description || null,
96       support: body.support || null
97     }
98
99     this.videoChannelService.updateVideoChannel(this.videoChannelToUpdate.uuid, videoChannelUpdate).subscribe(
100       () => {
101         this.authService.refreshUserInformation()
102         this.notificationsService.success(
103           this.i18n('Success'),
104           this.i18n('Video channel {{videoChannelName}} updated.', { videoChannelName: videoChannelUpdate.displayName })
105         )
106         this.router.navigate([ '/my-account', 'video-channels' ])
107       },
108
109       err => this.error = err.message
110     )
111   }
112
113   isCreation () {
114     return false
115   }
116
117   getFormButtonTitle () {
118     return this.i18n('Update')
119   }
120 }