Skip to content

Commit

Permalink
Bugfix/fix state handling of currency selector component (#2795) (#3429)
Browse files Browse the repository at this point in the history
* Fix state handling of currency selector component

* Update changelog
  • Loading branch information
SirZemar committed Jun 1, 2024
1 parent b12ac1f commit 60ef46a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 57 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fixed an issue with the initial annual interest rate in the _FIRE_ calculator
- Fixed the state handling in the currency selector

## 2.83.0 - 2024-05-30

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { ghostfolioScraperApiSymbolPrefix } from '@ghostfolio/common/config';
import { DATE_FORMAT } from '@ghostfolio/common/helper';
import {
AdminMarketDataDetails,
Currency,
UniqueAsset
} from '@ghostfolio/common/interfaces';
import { translate } from '@ghostfolio/ui/i18n';
Expand Down Expand Up @@ -73,7 +72,7 @@ export class AssetProfileDialog implements OnDestroy, OnInit {
public countries: {
[code: string]: { name: string; value: number };
};
public currencies: Currency[] = [];
public currencies: string[] = [];
public ghostfolioScraperApiSymbolPrefix = ghostfolioScraperApiSymbolPrefix;
public isBenchmark = false;
public marketDataDetails: MarketData[] = [];
Expand Down Expand Up @@ -102,10 +101,7 @@ export class AssetProfileDialog implements OnDestroy, OnInit {
const { benchmarks, currencies } = this.dataService.fetchInfo();

this.benchmarks = benchmarks;
this.currencies = currencies.map((currency) => ({
label: currency,
value: currency
}));
this.currencies = currencies;

this.initialize();
}
Expand Down Expand Up @@ -293,9 +289,7 @@ export class AssetProfileDialog implements OnDestroy, OnInit {
assetClass: this.assetProfileForm.get('assetClass').value,
assetSubClass: this.assetProfileForm.get('assetSubClass').value,
comment: this.assetProfileForm.get('comment').value || null,
currency: (<Currency>(
(<unknown>this.assetProfileForm.get('currency').value)
))?.value,
currency: this.assetProfileForm.get('currency').value,
name: this.assetProfileForm.get('name').value,
url: this.assetProfileForm.get('url').value || null
};
Expand Down Expand Up @@ -343,8 +337,7 @@ export class AssetProfileDialog implements OnDestroy, OnInit {
' ' +
price +
' ' +
(<Currency>(<unknown>this.assetProfileForm.get('currency').value))
?.value
this.assetProfileForm.get('currency').value
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { CreateAccountDto } from '@ghostfolio/api/app/account/create-account.dto
import { UpdateAccountDto } from '@ghostfolio/api/app/account/update-account.dto';
import { DataService } from '@ghostfolio/client/services/data.service';
import { validateObjectForForm } from '@ghostfolio/client/util/form.util';
import { Currency } from '@ghostfolio/common/interfaces';

import {
ChangeDetectionStrategy,
Expand Down Expand Up @@ -33,7 +32,7 @@ import { CreateOrUpdateAccountDialogParams } from './interfaces/interfaces';
})
export class CreateOrUpdateAccountDialog implements OnDestroy {
public accountForm: FormGroup;
public currencies: Currency[] = [];
public currencies: string[] = [];
public filteredPlatforms: Observable<Platform[]>;
public platforms: Platform[];

Expand All @@ -49,10 +48,7 @@ export class CreateOrUpdateAccountDialog implements OnDestroy {
public ngOnInit() {
const { currencies, platforms } = this.dataService.fetchInfo();

this.currencies = currencies.map((currency) => ({
label: currency,
value: currency
}));
this.currencies = currencies;
this.platforms = platforms;

this.accountForm = this.formBuilder.group({
Expand Down Expand Up @@ -107,7 +103,7 @@ export class CreateOrUpdateAccountDialog implements OnDestroy {
const account: CreateAccountDto | UpdateAccountDto = {
balance: this.accountForm.get('balance').value,
comment: this.accountForm.get('comment').value || null,
currency: this.accountForm.get('currency').value?.value,
currency: this.accountForm.get('currency').value,
id: this.accountForm.get('accountId').value,
isExcluded: this.accountForm.get('isExcluded').value,
name: this.accountForm.get('name').value,
Expand Down
4 changes: 0 additions & 4 deletions libs/common/src/lib/interfaces/currency.interface.ts

This file was deleted.

2 changes: 0 additions & 2 deletions libs/common/src/lib/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { BenchmarkMarketDataDetails } from './benchmark-market-data-details
import type { BenchmarkProperty } from './benchmark-property.interface';
import type { Benchmark } from './benchmark.interface';
import type { Coupon } from './coupon.interface';
import type { Currency } from './currency.interface';
import type { DataProviderInfo } from './data-provider-info.interface';
import type { EnhancedSymbolProfile } from './enhanced-symbol-profile.interface';
import type { Export } from './export.interface';
Expand Down Expand Up @@ -66,7 +65,6 @@ export {
BenchmarkProperty,
BenchmarkResponse,
Coupon,
Currency,
DataProviderInfo,
EnhancedSymbolProfile,
Export,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@

<mat-autocomplete
#currencyAutocomplete="matAutocomplete"
[displayWith]="displayFn"
(optionSelected)="onUpdateCurrency($event)"
>
@for (currencyItem of filteredCurrencies; track currencyItem) {
<mat-option class="line-height-1" [value]="currencyItem">
{{ currencyItem.label }}
@for (currency of filteredCurrencies; track currency) {
<mat-option class="line-height-1" [value]="currency">
{{ currency }}
</mat-option>
}
</mat-autocomplete>
43 changes: 14 additions & 29 deletions libs/ui/src/lib/currency-selector/currency-selector.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Currency } from '@ghostfolio/common/interfaces';
import { AbstractMatFormField } from '@ghostfolio/ui/shared/abstract-mat-form-field';

import { FocusMonitor } from '@angular/cdk/a11y';
Expand Down Expand Up @@ -59,10 +58,10 @@ import { map, startWith, takeUntil } from 'rxjs/operators';
templateUrl: 'currency-selector.component.html'
})
export class GfCurrencySelectorComponent
extends AbstractMatFormField<Currency>
extends AbstractMatFormField<string>
implements OnInit, OnDestroy
{
@Input() private currencies: Currency[] = [];
@Input() private currencies: string[] = [];
@Input() private formControlName: string;

@ViewChild(MatInput) private input: MatInput;
Expand All @@ -71,7 +70,7 @@ export class GfCurrencySelectorComponent
public currencyAutocomplete: MatAutocomplete;

public control = new FormControl();
public filteredCurrencies: Currency[] = [];
public filteredCurrencies: string[] = [];

private unsubscribeSubject = new Subject<void>();

Expand All @@ -98,7 +97,7 @@ export class GfCurrencySelectorComponent
const control = formGroup.get(this.formControlName);

if (control) {
this.value = this.currencies.find(({ value }) => {
this.value = this.currencies.find((value) => {
return value === control.value;
});
}
Expand All @@ -107,8 +106,8 @@ export class GfCurrencySelectorComponent
this.control.valueChanges
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
if (super.value?.value) {
super.value.value = null;
if (super.value) {
super.value = null;
}
});

Expand All @@ -125,10 +124,6 @@ export class GfCurrencySelectorComponent
});
}

public displayFn(currency: Currency) {
return currency?.label ?? '';
}

public get empty() {
return this.input?.empty;
}
Expand All @@ -146,17 +141,12 @@ export class GfCurrencySelectorComponent
}

public onUpdateCurrency(event: MatAutocompleteSelectedEvent) {
super.value = {
label: event.option.value.label,
value: event.option.value.value
} as Currency;
super.value = event.option.value;
}

public set value(value: Currency) {
const newValue =
typeof value === 'object' && value !== null ? { ...value } : value;
this.control.setValue(newValue);
super.value = newValue;
public set value(value: string) {
this.control.setValue(value);
super.value = value;
}

public ngOnDestroy() {
Expand All @@ -166,21 +156,16 @@ export class GfCurrencySelectorComponent
this.unsubscribeSubject.complete();
}

private filter(value: Currency | string) {
const filterValue =
typeof value === 'string'
? value?.toLowerCase()
: value?.value.toLowerCase();
private filter(value: string) {
const filterValue = value?.toLowerCase();

return this.currencies.filter((currency) => {
return currency.value.toLowerCase().startsWith(filterValue);
return currency.toLowerCase().startsWith(filterValue);
});
}

private validateRequired() {
const requiredCheck = super.required
? !super.value?.label || !super.value?.value
: false;
const requiredCheck = super.required ? !super.value : false;

if (requiredCheck) {
this.ngControl.control.setErrors({ invalidData: true });
Expand Down

0 comments on commit 60ef46a

Please sign in to comment.