1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, FormGroup, ValidatorFn } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
5 import { I18n } from '@ngx-translate/i18n-polyfill';
6 import * as _ from 'lodash';
8 import { ConfigurationService } from '../../../../shared/api/configuration.service';
9 import { ConfigFormModel } from '../../../../shared/components/config-option/config-option.model';
10 import { ConfigOptionTypes } from '../../../../shared/components/config-option/config-option.types';
11 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
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 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);
69 getValidators(configOption: any): ValidatorFn[] {
70 const typeValidators = ConfigOptionTypes.getTypeValidators(configOption);
72 this.patternHelpText = typeValidators.patternHelpText;
74 if ('max' in typeValidators && typeValidators.max !== '') {
75 this.maxValue = typeValidators.max;
78 if ('min' in typeValidators && typeValidators.min !== '') {
79 this.minValue = typeValidators.min;
82 return typeValidators.validators;
86 getStep(type: string, value: number): number | undefined {
87 return ConfigOptionTypes.getTypeStep(type, value);
90 setResponse(response: ConfigFormModel) {
91 this.response = response;
92 const validators = this.getValidators(response);
94 this.configForm.get('name').setValue(response.name);
95 this.configForm.get('desc').setValue(response.desc);
96 this.configForm.get('long_desc').setValue(response.long_desc);
97 this.configForm.get('default').setValue(response.default);
98 this.configForm.get('daemon_default').setValue(response.daemon_default);
99 this.configForm.get('services').setValue(response.services);
101 if (this.response.value) {
102 this.response.value.forEach((value) => {
103 // Check value type. If it's a boolean value we need to convert it because otherwise we
104 // would use the string representation. That would cause issues for e.g. checkboxes.
105 let sectionValue = null;
106 if (value.value === 'true') {
108 } else if (value.value === 'false') {
109 sectionValue = false;
111 sectionValue = value.value;
116 .setValue(sectionValue);
120 this.availSections.forEach((section) => {
124 .setValidators(validators);
127 const currentType = ConfigOptionTypes.getType(response.type);
128 this.type = currentType.name;
129 this.inputType = currentType.inputType;
130 this.humanReadableType = currentType.humanReadable;
133 createRequest(): ConfigFormCreateRequestModel | null {
136 this.availSections.forEach((section) => {
137 const sectionValue = this.configForm.getValue(section);
139 values.push({ section: section, value: sectionValue });
143 if (!_.isEqual(this.response.value, values)) {
144 const request = new ConfigFormCreateRequestModel();
145 request.name = this.configForm.getValue('name');
146 request.value = values;
154 const request = this.createRequest();
157 this.configService.create(request).subscribe(
159 this.notificationService.show(
160 NotificationType.success,
161 this.i18n('Updated config option {{name}}', { name: request.name })
163 this.router.navigate(['/configuration']);
166 this.configForm.setErrors({ cdSubmitButton: true });
171 this.router.navigate(['/configuration']);