|
1 | 1 | import { CommonModule } from '@angular/common';
|
2 |
| -import { Component } from '@angular/core'; |
| 2 | +import { Component, OnInit } from '@angular/core'; |
| 3 | +import { FormBuilder, ReactiveFormsModule } from '@angular/forms'; |
| 4 | +import { Project } from '@angular-cli-gui/shared/data'; |
| 5 | +import { JsonObject } from '@angular-devkit/core/src/json/utils'; |
| 6 | +import { FormlyFieldConfig, FormlyModule } from '@ngx-formly/core'; |
| 7 | +import { FormlyJsonschema } from '@ngx-formly/core/json-schema'; |
| 8 | +import { FormlyMaterialModule } from '@ngx-formly/material'; |
| 9 | +import { combineLatest, map, Subject, switchMap, tap } from 'rxjs'; |
| 10 | + |
| 11 | +import { WorkspaceSettingsService } from '../services'; |
3 | 12 |
|
4 | 13 | @Component({
|
5 | 14 | selector: 'cli-configuration',
|
6 | 15 | standalone: true,
|
7 |
| - imports: [CommonModule], |
| 16 | + imports: [ |
| 17 | + CommonModule, |
| 18 | + FormlyModule, |
| 19 | + FormlyMaterialModule, |
| 20 | + ReactiveFormsModule, |
| 21 | + ], |
8 | 22 | templateUrl: './configuration.component.html',
|
9 | 23 | styleUrls: ['./configuration.component.css'],
|
10 | 24 | })
|
11 |
| -export class ConfigurationComponent {} |
| 25 | +export class ConfigurationComponent implements OnInit { |
| 26 | + private formFields$ = new Subject<FormlyFieldConfig[]>(); |
| 27 | + private formData$ = new Subject<JsonObject>(); |
| 28 | + private projectName = ''; |
| 29 | + |
| 30 | + formly$ = combineLatest({ |
| 31 | + data: this.formData$, |
| 32 | + field: this.formFields$, |
| 33 | + }); |
| 34 | + |
| 35 | + form = this.fb.group({}); |
| 36 | + |
| 37 | + constructor( |
| 38 | + private workspaceSettingsService: WorkspaceSettingsService, |
| 39 | + private fb: FormBuilder, |
| 40 | + private schema: FormlyJsonschema |
| 41 | + ) {} |
| 42 | + |
| 43 | + ngOnInit(): void { |
| 44 | + this.workspaceSettingsService |
| 45 | + .readWorkspaceProjectConfiguration() |
| 46 | + .subscribe((configuration) => { |
| 47 | + const filedGroup = this.schema.toFieldConfig(configuration) |
| 48 | + .fieldGroup as FormlyFieldConfig[]; |
| 49 | + this.formFields$.next(filedGroup); |
| 50 | + }); |
| 51 | + this.workspaceSettingsService |
| 52 | + .readWorkspaceProjectNames() |
| 53 | + .pipe( |
| 54 | + map((names) => names[0]), |
| 55 | + tap((projectName) => (this.projectName = projectName)), |
| 56 | + switchMap((currentProjectName) => |
| 57 | + this.workspaceSettingsService.readWorkspaceProject( |
| 58 | + currentProjectName as string |
| 59 | + ) |
| 60 | + ) |
| 61 | + ) |
| 62 | + .subscribe((formData) => { |
| 63 | + this.formData$.next(formData); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + onSubmit(): void { |
| 68 | + this.workspaceSettingsService |
| 69 | + .updateWorkspaceProjectConfiguration( |
| 70 | + this.projectName, |
| 71 | + this.form.value as Project |
| 72 | + ) |
| 73 | + .subscribe(); |
| 74 | + } |
| 75 | +} |
0 commit comments