]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
ef44df970e17801e03c426b5c65b634c782034e7
[ceph.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { ValidatorFn, Validators } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
4
5 import _ from 'lodash';
6 import { forkJoin as observableForkJoin } from 'rxjs';
7
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';
16
17 @Component({
18   selector: 'cd-mgr-module-form',
19   templateUrl: './mgr-module-form.component.html',
20   styleUrls: ['./mgr-module-form.component.scss']
21 })
22 export class MgrModuleFormComponent extends CdForm implements OnInit {
23   mgrModuleForm: CdFormGroup;
24   moduleName = '';
25   moduleOptions: any[] = [];
26
27   constructor(
28     public actionLabels: ActionLabelsI18n,
29     private route: ActivatedRoute,
30     private router: Router,
31     private formBuilder: CdFormBuilder,
32     private mgrModuleService: MgrModuleService,
33     private notificationService: NotificationService
34   ) {
35     super();
36   }
37
38   ngOnInit() {
39     this.route.params.subscribe((params: { name: string }) => {
40       this.moduleName = decodeURIComponent(params.name);
41       const observables = [
42         this.mgrModuleService.getOptions(this.moduleName),
43         this.mgrModuleService.getConfig(this.moduleName)
44       ];
45       observableForkJoin(observables).subscribe(
46         (resp: object) => {
47           this.moduleOptions = resp[0];
48           // Create the form dynamically.
49           this.createForm();
50           // Set the form field values.
51           this.mgrModuleForm.setValue(resp[1]);
52           this.loadingReady();
53         },
54         (_error) => {
55           this.loadingError();
56         }
57       );
58     });
59   }
60
61   getValidators(moduleOption: any): ValidatorFn[] {
62     const result = [];
63     switch (moduleOption.type) {
64       case 'addr':
65         result.push(CdValidators.ip());
66         break;
67       case 'uint':
68       case 'int':
69       case 'size':
70       case 'secs':
71         result.push(Validators.required);
72         break;
73       case 'str':
74         if (_.isNumber(moduleOption.min)) {
75           result.push(Validators.minLength(moduleOption.min));
76         }
77         if (_.isNumber(moduleOption.max)) {
78           result.push(Validators.maxLength(moduleOption.max));
79         }
80         break;
81       case 'float':
82         result.push(Validators.required);
83         result.push(CdValidators.decimalNumber());
84         break;
85       case 'uuid':
86         result.push(CdValidators.uuid());
87         break;
88     }
89     return result;
90   }
91
92   createForm() {
93     const controlsConfig = {};
94     _.forEach(this.moduleOptions, (moduleOption) => {
95       controlsConfig[moduleOption.name] = [
96         moduleOption.default_value,
97         this.getValidators(moduleOption)
98       ];
99     });
100     this.mgrModuleForm = this.formBuilder.group(controlsConfig);
101   }
102
103   goToListView() {
104     this.router.navigate(['/mgr-modules']);
105   }
106
107   onSubmit() {
108     // Exit immediately if the form isn't dirty.
109     if (this.mgrModuleForm.pristine) {
110       this.goToListView();
111       return;
112     }
113     const config = {};
114     _.forEach(this.moduleOptions, (moduleOption) => {
115       const control = this.mgrModuleForm.get(moduleOption.name);
116       // Append the option only if the value has been modified.
117       if (control.dirty && control.valid) {
118         config[moduleOption.name] = control.value;
119       }
120     });
121     this.mgrModuleService.updateConfig(this.moduleName, config).subscribe(
122       () => {
123         this.notificationService.show(
124           NotificationType.success,
125           $localize`Updated options for module '${this.moduleName}'.`
126         );
127         this.goToListView();
128       },
129       () => {
130         // Reset the 'Submit' button.
131         this.mgrModuleForm.setErrors({ cdSubmitButton: true });
132       }
133     );
134   }
135 }