]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-forms/form-reactive.ts
Support ICU in TS components
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-forms / form-reactive.ts
CommitLineData
cc4bf76c 1
df98563e 2import { FormGroup } from '@angular/forms'
cc4bf76c 3import { wait } from '@root-helpers/utils'
7ed1edbb
C
4import { BuildFormArgument, BuildFormDefaultValues } from '../form-validators/form-validator.model'
5import { FormValidatorService } from './form-validator.service'
d18d6478 6
3866f1a0 7export type FormReactiveErrors = { [ id: string ]: string | FormReactiveErrors }
d18d6478 8export type FormReactiveValidationMessages = {
3866f1a0 9 [ id: string ]: { [ name: string ]: string } | FormReactiveValidationMessages
d18d6478 10}
4b2f33f3
C
11
12export abstract class FormReactive {
d18d6478 13 protected abstract formValidatorService: FormValidatorService
772d5642 14 protected formChanged = false
4b2f33f3 15
d18d6478 16 form: FormGroup
26a008fe 17 formErrors: any // To avoid casting in template because of string | FormReactiveErrors
d18d6478 18 validationMessages: FormReactiveValidationMessages
4b2f33f3 19
d18d6478
C
20 buildForm (obj: BuildFormArgument, defaultValues: BuildFormDefaultValues = {}) {
21 const { formErrors, validationMessages, form } = this.formValidatorService.buildForm(obj, defaultValues)
4b2f33f3 22
d18d6478
C
23 this.form = form
24 this.formErrors = formErrors
25 this.validationMessages = validationMessages
26
c729caf6 27 this.form.statusChanges.subscribe(async () => {
cc4bf76c
C
28 // FIXME: remove when https://github.com/angular/angular/issues/41519 is fixed
29 await this.waitPendingCheck()
30
31 this.onStatusChanged(this.form, this.formErrors, this.validationMessages)
32 })
3866f1a0
C
33 }
34
cc4bf76c
C
35 protected async waitPendingCheck () {
36 if (this.form.status !== 'PENDING') return
37
38 // FIXME: the following line does not work: https://github.com/angular/angular/issues/41519
39 // return firstValueFrom(this.form.statusChanges.pipe(filter(status => status !== 'PENDING')))
40 // So we have to fallback to active wait :/
41
42 do {
43 await wait(10)
44 } while (this.form.status === 'PENDING')
3866f1a0
C
45 }
46
cc4bf76c
C
47 protected forceCheck () {
48 this.onStatusChanged(this.form, this.formErrors, this.validationMessages, false)
4b2f33f3 49 }
bf57d5ee 50
cc4bf76c 51 private onStatusChanged (
3866f1a0
C
52 form: FormGroup,
53 formErrors: FormReactiveErrors,
54 validationMessages: FormReactiveValidationMessages,
cc4bf76c 55 onlyDirty = true
3866f1a0
C
56 ) {
57 for (const field of Object.keys(formErrors)) {
58 if (formErrors[field] && typeof formErrors[field] === 'object') {
cc4bf76c 59 this.onStatusChanged(
3866f1a0
C
60 form.controls[field] as FormGroup,
61 formErrors[field] as FormReactiveErrors,
cc4bf76c 62 validationMessages[field] as FormReactiveValidationMessages
3866f1a0
C
63 )
64 continue
65 }
66
bf57d5ee 67 // clear previous error message (if any)
9df52d66 68 formErrors[field] = ''
3866f1a0 69 const control = form.get(field)
bf57d5ee 70
772d5642
C
71 if (control.dirty) this.formChanged = true
72
cc4bf76c 73 if (!control || (onlyDirty && !control.dirty) || !control.enabled || !control.errors) continue
3c065fe3
C
74
75 const staticMessages = validationMessages[field]
76 for (const key of Object.keys(control.errors)) {
77 const formErrorValue = control.errors[key]
78
79 // Try to find error message in static validation messages first
80 // Then check if the validator returns a string that is the error
cc4bf76c 81 if (staticMessages[key]) formErrors[field] += staticMessages[key] + ' '
3c065fe3
C
82 else if (typeof formErrorValue === 'string') formErrors[field] += control.errors[key]
83 else throw new Error('Form error value of ' + field + ' is invalid')
bf57d5ee
C
84 }
85 }
86 }
4b2f33f3 87}