Skip to content

Commit 3d36885

Browse files
committed
refactor: migrate UntypedForm* to typed Reactive Forms
1 parent 387be21 commit 3d36885

4 files changed

Lines changed: 32 additions & 22 deletions

File tree

src/app/pages/mapping/mapping.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Component, OnInit, AfterViewInit, ViewChild, ElementRef } from '@angula
22
import { MatTableDataSource } from '@angular/material/table';
33
import { MatSort } from '@angular/material/sort';
44
import { COMMA, ENTER } from '@angular/cdk/keycodes';
5-
import { UntypedFormControl } from '@angular/forms';
5+
import { FormControl } from '@angular/forms';
66
import * as XLSX from 'xlsx';
77
import { LoaderService } from 'src/app/service/loader/data-loader.service';
88
import {
@@ -75,7 +75,7 @@ export class MappingComponent implements OnInit, AfterViewInit {
7575
dataStore: DataStore = new DataStore();
7676

7777
searchTerms: string[] = [];
78-
searchCtrl = new UntypedFormControl('');
78+
searchCtrl = new FormControl('');
7979

8080
constructor(
8181
private loader: LoaderService,

src/app/pages/matrix/matrix.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, OnInit, ElementRef, ViewChild } from '@angular/core';
2-
import { UntypedFormControl } from '@angular/forms';
2+
import { FormControl } from '@angular/forms';
33
import { MatTableDataSource } from '@angular/material/table';
44
import { Router, NavigationExtras } from '@angular/router';
55
import { LoaderService } from 'src/app/service/loader/data-loader.service';
@@ -149,7 +149,7 @@ export class MatrixComponent implements OnInit {
149149
}
150150

151151
@ViewChild(MatChipList)
152-
chipsControl = new UntypedFormControl(['chipsControl']);
152+
chipsControl = new FormControl(['chipsControl']);
153153
chipList!: MatChipList;
154154

155155
toggleTagFilters(chip: MatChip) {

src/app/pages/settings/settings.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ <h2>Progress Definitions</h2>
101101
max="100"
102102
class="progress-score"
103103
formControlName="score"
104-
[disabled]="getDefinitionGroup(i).get('mandatory')?.value" />
104+
[disabled]="getDefinitionGroup(i).get('mandatory')?.value || false" />
105105
<span matSuffix>%</span>
106106
</mat-form-field>
107107

src/app/pages/settings/settings.component.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, OnInit } from '@angular/core';
2-
import { UntypedFormBuilder, UntypedFormGroup, UntypedFormArray, AbstractControl } from '@angular/forms';
2+
import { FormBuilder, FormGroup, FormArray, FormControl, AbstractControl } from '@angular/forms';
33
import { SettingsService } from '../../service/settings/settings.service';
44
import { GithubService, GithubReleaseInfo } from 'src/app/service/settings/github.service';
55
import { LoaderService } from 'src/app/service/loader/data-loader.service';
@@ -27,6 +27,14 @@ interface RemoteReleaseCheck {
2727
latestCheckError: string | null;
2828
}
2929

30+
interface ProgressDefinitionForm {
31+
pid: FormControl<number>;
32+
key: FormControl<string>;
33+
score: FormControl<number>;
34+
definition: FormControl<string>;
35+
mandatory?: FormControl<boolean>;
36+
}
37+
3038
@Component({
3139
selector: 'app-settings',
3240
templateUrl: './settings.component.html',
@@ -38,7 +46,9 @@ export class SettingsComponent implements OnInit {
3846
dataStoreMaxLevel!: number;
3947
selectedMaxLevel!: number;
4048
selectedMaxLevelCaption: String = '';
41-
progressDefinitionsForm!: UntypedFormGroup;
49+
progressDefinitionsForm!: FormGroup<{
50+
definitions: FormArray<FormGroup<ProgressDefinitionForm>>;
51+
}>;
4252
tempProgressDefinitions: ProgressDefinitions = {};
4353
editingProgressDefinitions: boolean = false;
4454
remoteReleaseCheck: RemoteReleaseCheck = {
@@ -72,7 +82,7 @@ export class SettingsComponent implements OnInit {
7282
constructor(
7383
private loader: LoaderService,
7484
private settings: SettingsService,
75-
private formBuilder: UntypedFormBuilder,
85+
private formBuilder: FormBuilder,
7686
public modal: ModalMessageComponent,
7787
private githubService: GithubService
7888
) {}
@@ -203,17 +213,17 @@ export class SettingsComponent implements OnInit {
203213
// === Progress Definitions ===
204214
private initProgressDefinitionsForm(): void {
205215
this.progressDefinitionsForm = this.formBuilder.group({
206-
definitions: this.formBuilder.array([]),
216+
definitions: this.formBuilder.array<FormGroup<ProgressDefinitionForm>>([]),
207217
});
208218
}
209219

210-
get definitionsFormArray(): UntypedFormArray {
211-
return this.progressDefinitionsForm.get('definitions') as UntypedFormArray;
220+
get definitionsFormArray(): FormArray<FormGroup<ProgressDefinitionForm>> {
221+
return this.progressDefinitionsForm.controls.definitions;
212222
}
213223

214224
// Return the FormGroup for a specific index in the definitions FormArray.
215-
getDefinitionGroup(index: number): UntypedFormGroup {
216-
return this.definitionsFormArray.at(index) as UntypedFormGroup;
225+
getDefinitionGroup(index: number): FormGroup<ProgressDefinitionForm> {
226+
return this.definitionsFormArray.at(index);
217227
}
218228

219229
private updateProgressDefinitionsForm(): void {
@@ -226,8 +236,8 @@ export class SettingsComponent implements OnInit {
226236
key: [key],
227237
score: [progDef.score * 100],
228238
definition: [progDef.definition],
229-
mandatory: progDef.score == 1 || progDef.score == 0,
230-
})
239+
mandatory: [progDef.score == 1 || progDef.score == 0],
240+
}) as unknown as FormGroup<ProgressDefinitionForm>
231241
);
232242
});
233243
}
@@ -244,7 +254,7 @@ export class SettingsComponent implements OnInit {
244254
key: [''],
245255
score: [score],
246256
definition: [''],
247-
})
257+
}) as unknown as FormGroup<ProgressDefinitionForm>
248258
);
249259
}
250260

@@ -273,11 +283,11 @@ export class SettingsComponent implements OnInit {
273283
const renamedItems: Array<{ originalKey: string; newKey: string; pid: number }> = [];
274284

275285
this.definitionsFormArray.controls.forEach(control => {
276-
const formGroup = control as UntypedFormGroup;
277-
const pid = formGroup.get('pid')?.value;
278-
const key = formGroup.get('key')?.value;
279-
const score = formGroup.get('score')?.value / 100; // Convert from percentage back to decimal
280-
const definition = formGroup.get('definition')?.value;
286+
const formGroup = control;
287+
const pid = formGroup.controls.pid.value;
288+
const key = formGroup.controls.key.value;
289+
const score = formGroup.controls.score.value / 100; // Convert from percentage back to decimal
290+
const definition = formGroup.controls.definition.value;
281291

282292
if (key && key.trim()) {
283293
// Only add if key is not empty
@@ -336,7 +346,7 @@ export class SettingsComponent implements OnInit {
336346
}
337347

338348
getFormGroupValue(control: AbstractControl, field: string): any {
339-
return (control as UntypedFormGroup).get(field)?.value;
349+
return (control as FormGroup).get(field)?.value;
340350
}
341351

342352
/**

0 commit comments

Comments
 (0)