1 import { Component, Input, OnInit } from '@angular/core'
2 import { FormGroup } from '@angular/forms'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { VIDEO_SUPPORT } from '@app/shared'
5 import { NotificationsService } from 'angular2-notifications'
6 import { ServerService } from '../../../core/server'
7 import { VIDEO_CHANNEL } from '../../../shared/forms/form-validators'
8 import { ValidatorMessage } from '../../../shared/forms/form-validators/validator-message'
17 } from '../../../shared/forms/form-validators/video'
18 import { VideoEdit } from '../../../shared/video/video-edit.model'
19 import { map } from 'rxjs/operators'
20 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
23 selector: 'my-video-edit',
24 styleUrls: [ './video-edit.component.scss' ],
25 templateUrl: './video-edit.component.html'
28 export class VideoEditComponent implements OnInit {
29 @Input() form: FormGroup
30 @Input() formErrors: { [ id: string ]: string } = {}
31 @Input() validationMessages: ValidatorMessage = {}
32 @Input() videoPrivacies = []
33 @Input() userVideoChannels: { id: number, label: string, support: string }[] = []
40 tagValidators = VIDEO_TAGS.VALIDATORS
41 tagValidatorsMessages = VIDEO_TAGS.MESSAGES
46 private formValidatorService: FormValidatorService,
47 private route: ActivatedRoute,
48 private router: Router,
49 private notificationsService: NotificationsService,
50 private serverService: ServerService
54 const defaultValues = {
56 commentsEnabled: 'true',
61 privacy: VIDEO_PRIVACY,
62 channelId: VIDEO_CHANNEL,
64 commentsEnabled: null,
65 category: VIDEO_CATEGORY,
66 licence: VIDEO_LICENCE,
67 language: VIDEO_LANGUAGE,
68 description: VIDEO_DESCRIPTION,
72 support: VIDEO_SUPPORT
75 this.formValidatorService.updateForm(
78 this.validationMessages,
83 // We will update the "support" field depending on the channel
84 this.form.controls['channelId']
86 .pipe(map(res => parseInt(res.toString(), 10)))
89 const oldChannelId = parseInt(this.form.value['channelId'], 10)
90 const currentSupport = this.form.value['support']
92 // Not initialized yet
93 if (isNaN(newChannelId)) return
94 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
95 if (!newChannel) return
97 // First time we set the channel?
98 if (isNaN(oldChannelId)) return this.updateSupportField(newChannel.support)
99 const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId)
101 if (!newChannel || !oldChannel) {
102 console.error('Cannot find new or old channel.')
106 // If the current support text is not the same than the old channel, the user updated it.
107 // We don't want the user to lose his text, so stop here
108 if (currentSupport && currentSupport !== oldChannel.support) return
110 // Update the support text with our new channel
111 this.updateSupportField(newChannel.support)
119 this.videoCategories = this.serverService.getVideoCategories()
120 this.videoLicences = this.serverService.getVideoLicences()
121 this.videoLanguages = this.serverService.getVideoLanguages()
124 private updateSupportField (support: string) {
125 return this.form.patchValue({ support: support || '' })