1 import { Component, OnInit } from '@angular/core';
2 import { ValidatorFn, Validators } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
5 import _ from 'lodash';
6 import { forkJoin as observableForkJoin } from 'rxjs';
8 import { MgrModuleService } from '~/app/shared/api/mgr-module.service';
9 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
10 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
11 import { CdForm } from '~/app/shared/forms/cd-form';
12 import { CdFormBuilder } from '~/app/shared/forms/cd-form-builder';
13 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
14 import { CdValidators } from '~/app/shared/forms/cd-validators';
15 import { NotificationService } from '~/app/shared/services/notification.service';
18 selector: 'cd-mgr-module-form',
19 templateUrl: './mgr-module-form.component.html',
20 styleUrls: ['./mgr-module-form.component.scss']
22 export class MgrModuleFormComponent extends CdForm implements OnInit {
23 mgrModuleForm: CdFormGroup;
25 moduleOptions: any[] = [];
28 public actionLabels: ActionLabelsI18n,
29 private route: ActivatedRoute,
30 private router: Router,
31 private formBuilder: CdFormBuilder,
32 private mgrModuleService: MgrModuleService,
33 private notificationService: NotificationService
39 this.route.params.subscribe((params: { name: string }) => {
40 this.moduleName = decodeURIComponent(params.name);
42 this.mgrModuleService.getOptions(this.moduleName),
43 this.mgrModuleService.getConfig(this.moduleName)
45 observableForkJoin(observables).subscribe(
47 this.moduleOptions = resp[0];
48 // Create the form dynamically.
50 // Set the form field values.
51 this.mgrModuleForm.setValue(resp[1]);
61 getValidators(moduleOption: any): ValidatorFn[] {
63 switch (moduleOption.type) {
65 result.push(CdValidators.ip());
71 result.push(CdValidators.number());
72 result.push(Validators.required);
73 if (_.isNumber(moduleOption.min)) {
74 result.push(Validators.min(moduleOption.min));
76 if (_.isNumber(moduleOption.max)) {
77 result.push(Validators.max(moduleOption.max));
81 if (_.isNumber(moduleOption.min)) {
82 result.push(Validators.minLength(moduleOption.min));
84 if (_.isNumber(moduleOption.max)) {
85 result.push(Validators.maxLength(moduleOption.max));
89 result.push(Validators.required);
90 result.push(CdValidators.decimalNumber());
93 result.push(CdValidators.uuid());
100 const controlsConfig = {};
101 _.forEach(this.moduleOptions, (moduleOption) => {
102 controlsConfig[moduleOption.name] = [
103 moduleOption.default_value,
104 this.getValidators(moduleOption)
107 this.mgrModuleForm = this.formBuilder.group(controlsConfig);
111 this.router.navigate(['/mgr-modules']);
115 // Exit immediately if the form isn't dirty.
116 if (this.mgrModuleForm.pristine) {
121 _.forEach(this.moduleOptions, (moduleOption) => {
122 const control = this.mgrModuleForm.get(moduleOption.name);
123 // Append the option only if the value has been modified.
124 if (control.dirty && control.valid) {
125 config[moduleOption.name] = control.value;
128 this.mgrModuleService.updateConfig(this.moduleName, config).subscribe(
130 this.notificationService.show(
131 NotificationType.success,
132 $localize`Updated options for module '${this.moduleName}'.`
137 // Reset the 'Submit' button.
138 this.mgrModuleForm.setErrors({ cdSubmitButton: true });