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'
8 VIDEO_CHANNEL_DESCRIPTION,
9 VIDEO_CHANNEL_DISPLAY_NAME,
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'
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' ]
23 export class MyAccountVideoChannelUpdateComponent extends MyAccountVideoChannelEdit implements OnInit, OnDestroy {
32 validationMessages = {
33 'display-name': VIDEO_CHANNEL_DISPLAY_NAME.MESSAGES,
34 'description': VIDEO_CHANNEL_DESCRIPTION.MESSAGES,
35 'support': VIDEO_CHANNEL_SUPPORT.MESSAGES
38 private videoChannelToUpdate: VideoChannel
39 private paramsSub: Subscription
42 private authService: AuthService,
43 private notificationsService: NotificationsService,
44 private router: Router,
45 private route: ActivatedRoute,
46 private formBuilder: FormBuilder,
47 private videoChannelService: VideoChannelService,
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 ]
60 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
66 this.paramsSub = this.route.params.subscribe(routeParams => {
67 const videoChannelId = routeParams['videoChannelId']
69 this.videoChannelService.getVideoChannel(videoChannelId).subscribe(
70 videoChannelToUpdate => {
71 this.videoChannelToUpdate = videoChannelToUpdate
73 this.form.patchValue({
74 'display-name': videoChannelToUpdate.displayName,
75 description: videoChannelToUpdate.description,
76 support: videoChannelToUpdate.support
80 err => this.error = err.message
86 if (this.paramsSub) this.paramsSub.unsubscribe()
90 this.error = undefined
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
99 this.videoChannelService.updateVideoChannel(this.videoChannelToUpdate.uuid, videoChannelUpdate).subscribe(
101 this.authService.refreshUserInformation()
102 this.notificationsService.success(
103 this.i18n('Success'),
104 this.i18n('Video channel {{videoChannelName}} updated.', { videoChannelName: videoChannelUpdate.displayName })
106 this.router.navigate([ '/my-account', 'video-channels' ])
109 err => this.error = err.message
117 getFormButtonTitle () {
118 return this.i18n('Update')