Form validators refractoring
[oweals/peertube.git] / client / src / app / videos / +video-edit / shared / video-edit.component.ts
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'
9 import {
10   VIDEO_CATEGORY,
11   VIDEO_DESCRIPTION,
12   VIDEO_LANGUAGE,
13   VIDEO_LICENCE,
14   VIDEO_NAME,
15   VIDEO_PRIVACY,
16   VIDEO_TAGS
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'
21
22 @Component({
23   selector: 'my-video-edit',
24   styleUrls: [ './video-edit.component.scss' ],
25   templateUrl: './video-edit.component.html'
26 })
27
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 }[] = []
34
35   videoCategories = []
36   videoLicences = []
37   videoLanguages = []
38   video: VideoEdit
39
40   tagValidators = VIDEO_TAGS.VALIDATORS
41   tagValidatorsMessages = VIDEO_TAGS.MESSAGES
42
43   error: string = null
44
45   constructor (
46     private formValidatorService: FormValidatorService,
47     private route: ActivatedRoute,
48     private router: Router,
49     private notificationsService: NotificationsService,
50     private serverService: ServerService
51   ) { }
52
53   updateForm () {
54     const defaultValues = {
55       nsfw: 'false',
56       commentsEnabled: 'true',
57       tags: []
58     }
59     const obj = {
60       name: VIDEO_NAME,
61       privacy: VIDEO_PRIVACY,
62       channelId: VIDEO_CHANNEL,
63       nsfw: null,
64       commentsEnabled: null,
65       category: VIDEO_CATEGORY,
66       licence: VIDEO_LICENCE,
67       language: VIDEO_LANGUAGE,
68       description: VIDEO_DESCRIPTION,
69       tags: null,
70       thumbnailfile: null,
71       previewfile: null,
72       support: VIDEO_SUPPORT
73     }
74
75     this.formValidatorService.updateForm(
76       this.form,
77       this.formErrors,
78       this.validationMessages,
79       obj,
80       defaultValues
81     )
82
83     // We will update the "support" field depending on the channel
84     this.form.controls['channelId']
85       .valueChanges
86       .pipe(map(res => parseInt(res.toString(), 10)))
87       .subscribe(
88         newChannelId => {
89           const oldChannelId = parseInt(this.form.value['channelId'], 10)
90           const currentSupport = this.form.value['support']
91
92           // Not initialized yet
93           if (isNaN(newChannelId)) return
94           const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
95           if (!newChannel) return
96
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)
100
101           if (!newChannel || !oldChannel) {
102             console.error('Cannot find new or old channel.')
103             return
104           }
105
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
109
110           // Update the support text with our new channel
111           this.updateSupportField(newChannel.support)
112         }
113       )
114   }
115
116   ngOnInit () {
117     this.updateForm()
118
119     this.videoCategories = this.serverService.getVideoCategories()
120     this.videoLicences = this.serverService.getVideoLicences()
121     this.videoLanguages = this.serverService.getVideoLanguages()
122   }
123
124   private updateSupportField (support: string) {
125     return this.form.patchValue({ support: support || '' })
126   }
127 }