1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, FormGroup, ValidatorFn } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
5 import * as _ from 'lodash';
7 import { ConfigurationService } from '../../../../shared/api/configuration.service';
8 import { ConfigFormModel } from '../../../../shared/components/config-option/config-option.model';
9 import { ConfigOptionTypes } from '../../../../shared/components/config-option/config-option.types';
10 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
11 import { CdForm } from '../../../../shared/forms/cd-form';
12 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
13 import { NotificationService } from '../../../../shared/services/notification.service';
14 import { ConfigFormCreateRequestModel } from './configuration-form-create-request.model';
17 selector: 'cd-configuration-form',
18 templateUrl: './configuration-form.component.html',
19 styleUrls: ['./configuration-form.component.scss']
21 export class ConfigurationFormComponent extends CdForm implements OnInit {
22 configForm: CdFormGroup;
23 response: ConfigFormModel;
26 humanReadableType: string;
29 patternHelpText: string;
30 availSections = ['global', 'mon', 'mgr', 'osd', 'mds', 'client'];
33 private route: ActivatedRoute,
34 private router: Router,
35 private configService: ConfigurationService,
36 private notificationService: NotificationService
43 const formControls = {
44 name: new FormControl({ value: null }),
45 desc: new FormControl({ value: null }),
46 long_desc: new FormControl({ value: null }),
47 values: new FormGroup({}),
48 default: new FormControl({ value: null }),
49 daemon_default: new FormControl({ value: null }),
50 services: new FormControl([])
53 this.availSections.forEach((section) => {
54 formControls.values.addControl(section, new FormControl(null));
57 this.configForm = new CdFormGroup(formControls);
61 this.route.params.subscribe((params: { name: string }) => {
62 const configName = params.name;
63 this.configService.get(configName).subscribe((resp: ConfigFormModel) => {
64 this.setResponse(resp);
70 getValidators(configOption: any): ValidatorFn[] {
71 const typeValidators = ConfigOptionTypes.getTypeValidators(configOption);
73 this.patternHelpText = typeValidators.patternHelpText;
75 if ('max' in typeValidators && typeValidators.max !== '') {
76 this.maxValue = typeValidators.max;
79 if ('min' in typeValidators && typeValidators.min !== '') {
80 this.minValue = typeValidators.min;
83 return typeValidators.validators;
89 getStep(type: string, value: number): number | undefined {
90 return ConfigOptionTypes.getTypeStep(type, value);
93 setResponse(response: ConfigFormModel) {
94 this.response = response;
95 const validators = this.getValidators(response);
97 this.configForm.get('name').setValue(response.name);
98 this.configForm.get('desc').setValue(response.desc);
99 this.configForm.get('long_desc').setValue(response.long_desc);
100 this.configForm.get('default').setValue(response.default);
101 this.configForm.get('daemon_default').setValue(response.daemon_default);
102 this.configForm.get('services').setValue(response.services);
104 if (this.response.value) {
105 this.response.value.forEach((value) => {
106 // Check value type. If it's a boolean value we need to convert it because otherwise we
107 // would use the string representation. That would cause issues for e.g. checkboxes.
108 let sectionValue = null;
109 if (value.value === 'true') {
111 } else if (value.value === 'false') {
112 sectionValue = false;
114 sectionValue = value.value;
116 this.configForm.get('values').get(value.section).setValue(sectionValue);
120 this.availSections.forEach((section) => {
121 this.configForm.get('values').get(section).setValidators(validators);
124 const currentType = ConfigOptionTypes.getType(response.type);
125 this.type = currentType.name;
126 this.inputType = currentType.inputType;
127 this.humanReadableType = currentType.humanReadable;
130 createRequest(): ConfigFormCreateRequestModel | null {
131 const values: any[] = [];
133 this.availSections.forEach((section) => {
134 const sectionValue = this.configForm.getValue(section);
135 if (sectionValue !== null && sectionValue !== '') {
136 values.push({ section: section, value: sectionValue });
140 if (!_.isEqual(this.response.value, values)) {
141 const request = new ConfigFormCreateRequestModel();
142 request.name = this.configForm.getValue('name');
143 request.value = values;
151 const request = this.createRequest();
154 this.configService.create(request).subscribe(
156 this.notificationService.show(
157 NotificationType.success,
158 $localize`Updated config option ${request.name}`
160 this.router.navigate(['/configuration']);
163 this.configForm.setErrors({ cdSubmitButton: true });
168 this.router.navigate(['/configuration']);