Skip to content

Commit d4cdbfb

Browse files
guguclaude
andcommitted
Refactor country and language components: replace effect with computed for flag derivation
The selectedFlag was a writable signal updated via effect(), but it's purely derived from the form control value — computed() is the correct primitive here. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ff20385 commit d4cdbfb

File tree

2 files changed

+16
-31
lines changed

2 files changed

+16
-31
lines changed

frontend/src/app/components/ui-components/record-edit-fields/country/country.component.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CommonModule } from '@angular/common';
2-
import { Component, CUSTOM_ELEMENTS_SCHEMA, computed, effect, Input, signal } from '@angular/core';
2+
import { Component, CUSTOM_ELEMENTS_SCHEMA, computed, Input } from '@angular/core';
33
import { toSignal } from '@angular/core/rxjs-interop';
44
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
55
import { MatAutocompleteModule } from '@angular/material/autocomplete';
@@ -26,7 +26,13 @@ export class CountryEditComponent extends BaseEditFieldComponent {
2626

2727
public countries: CountryOption[] = [];
2828
public countryControl = new FormControl<CountryOption | string>('');
29-
public selectedCountryFlag = signal('');
29+
public selectedCountryFlag = computed(() => {
30+
const value = this._controlValue();
31+
if (typeof value === 'object' && value !== null) {
32+
return value.flag;
33+
}
34+
return '';
35+
});
3036

3137
public showFlag = computed(() => {
3238
if (this.widgetStructure?.widget_params) {
@@ -59,18 +65,6 @@ export class CountryEditComponent extends BaseEditFieldComponent {
5965

6066
getCountryFlag = getCountryFlag;
6167

62-
constructor() {
63-
super();
64-
effect(() => {
65-
const value = this._controlValue();
66-
if (typeof value === 'object' && value !== null) {
67-
this.selectedCountryFlag.set(value.flag);
68-
} else if (typeof value === 'string') {
69-
this.selectedCountryFlag.set('');
70-
}
71-
});
72-
}
73-
7468
ngOnInit(): void {
7569
super.ngOnInit();
7670
this.loadCountries();
@@ -79,7 +73,6 @@ export class CountryEditComponent extends BaseEditFieldComponent {
7973

8074
onCountrySelected(selectedCountry: CountryOption): void {
8175
this.value = selectedCountry.value;
82-
this.selectedCountryFlag.set(selectedCountry.flag);
8376
this.onFieldChange.emit(this.value);
8477
}
8578

@@ -93,7 +86,6 @@ export class CountryEditComponent extends BaseEditFieldComponent {
9386
const country = this.countries.find((c) => c.value === this.value);
9487
if (country) {
9588
this.countryControl.setValue(country);
96-
this.selectedCountryFlag.set(country.flag);
9789
}
9890
}
9991
}

frontend/src/app/components/ui-components/record-edit-fields/language/language.component.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CommonModule } from '@angular/common';
2-
import { Component, CUSTOM_ELEMENTS_SCHEMA, computed, effect, input, OnInit, output, signal } from '@angular/core';
2+
import { Component, CUSTOM_ELEMENTS_SCHEMA, computed, input, OnInit, output } from '@angular/core';
33
import { toSignal } from '@angular/core/rxjs-interop';
44
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
55
import { MatAutocompleteModule } from '@angular/material/autocomplete';
@@ -51,7 +51,13 @@ export class LanguageEditComponent implements OnInit {
5151

5252
public languages: LanguageOption[] = [];
5353
public languageControl = new FormControl<LanguageOption | string>('');
54-
public selectedLanguageFlag = signal('');
54+
public selectedLanguageFlag = computed(() => {
55+
const value = this._controlValue();
56+
if (typeof value === 'object' && value !== null) {
57+
return value.flag;
58+
}
59+
return '';
60+
});
5561

5662
private _controlValue = toSignal(this.languageControl.valueChanges, { initialValue: '' as LanguageOption | string });
5763

@@ -64,17 +70,6 @@ export class LanguageEditComponent implements OnInit {
6470
return 0;
6571
};
6672

67-
constructor() {
68-
effect(() => {
69-
const value = this._controlValue();
70-
if (typeof value === 'object' && value !== null) {
71-
this.selectedLanguageFlag.set(value.flag);
72-
} else if (typeof value === 'string') {
73-
this.selectedLanguageFlag.set('');
74-
}
75-
});
76-
}
77-
7873
ngOnInit(): void {
7974
this.loadLanguages();
8075
this.setInitialValue();
@@ -86,7 +81,6 @@ export class LanguageEditComponent implements OnInit {
8681
}
8782

8883
onLanguageSelected(selectedLanguage: LanguageOption): void {
89-
this.selectedLanguageFlag.set(selectedLanguage.flag);
9084
this.onFieldChange.emit(selectedLanguage.value);
9185
}
9286

@@ -96,7 +90,6 @@ export class LanguageEditComponent implements OnInit {
9690
const language = this.languages.find((l) => l.value && l.value.toLowerCase() === val.toLowerCase());
9791
if (language) {
9892
this.languageControl.setValue(language);
99-
this.selectedLanguageFlag.set(language.flag);
10093
}
10194
}
10295
}

0 commit comments

Comments
 (0)