Feature/improve preselected currency in create or update activity dialog (#2349)

* Preselect currency based on account's currency

* Update changelog
pull/2351/head^2
Thomas Kaul 1 year ago committed by GitHub
parent e23bf62859
commit c668d7b456
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
### Changed
- Improved the preselected currency based on the account's currency in the create or edit activity dialog
### Fixed
- Fixed a memory leak related to the server's timezone (behind UTC) in the data gathering

@ -20,7 +20,7 @@ import { translate } from '@ghostfolio/ui/i18n';
import { AssetClass, AssetSubClass, Tag, Type } from '@prisma/client';
import { isUUID } from 'class-validator';
import { EMPTY, Observable, Subject, lastValueFrom, of } from 'rxjs';
import { catchError, map, startWith, takeUntil } from 'rxjs/operators';
import { catchError, delay, map, startWith, takeUntil } from 'rxjs/operators';
import { CreateOrUpdateActivityDialogParams } from './interfaces/interfaces';
@ -139,7 +139,12 @@ export class CreateOrUpdateActivityDialog implements OnDestroy {
});
this.activityForm.valueChanges
.pipe(takeUntil(this.unsubscribeSubject))
.pipe(
// Slightly delay until the more specific form control value changes have
// completed
delay(300),
takeUntil(this.unsubscribeSubject)
)
.subscribe(async () => {
let exchangeRateOfFee = 1;
let exchangeRateOfUnitPrice = 1;
@ -234,6 +239,23 @@ export class CreateOrUpdateActivityDialog implements OnDestroy {
this.changeDetectorRef.markForCheck();
});
this.activityForm.controls['accountId'].valueChanges.subscribe(
(accountId) => {
const type = this.activityForm.controls['type'].value;
if (type === 'FEE' || type === 'ITEM' || type === 'LIABILITY') {
const currency =
this.data.accounts.find(({ id }) => {
return id === accountId;
})?.currency ?? this.data.user.settings.baseCurrency;
this.activityForm.controls['currency'].setValue(currency);
this.activityForm.controls['currencyOfFee'].setValue(currency);
this.activityForm.controls['currencyOfUnitPrice'].setValue(currency);
}
}
);
this.activityForm.controls['searchSymbol'].valueChanges.subscribe(() => {
if (this.activityForm.controls['searchSymbol'].invalid) {
this.data.activity.SymbolProfile = null;
@ -269,19 +291,21 @@ export class CreateOrUpdateActivityDialog implements OnDestroy {
Validators.required
);
this.activityForm.controls['accountId'].updateValueAndValidity();
this.activityForm.controls['currency'].setValue(
this.data.user.settings.baseCurrency
);
this.activityForm.controls['currencyOfFee'].setValue(
this.data.user.settings.baseCurrency
);
this.activityForm.controls['currencyOfUnitPrice'].setValue(
this.data.user.settings.baseCurrency
);
const currency =
this.data.accounts.find(({ id }) => {
return id === this.activityForm.controls['accountId'].value;
})?.currency ?? this.data.user.settings.baseCurrency;
this.activityForm.controls['currency'].setValue(currency);
this.activityForm.controls['currencyOfFee'].setValue(currency);
this.activityForm.controls['currencyOfUnitPrice'].setValue(currency);
this.activityForm.controls['dataSource'].removeValidators(
Validators.required
);
this.activityForm.controls['dataSource'].updateValueAndValidity();
this.activityForm.controls['feeInCustomCurrency'].reset();
this.activityForm.controls['name'].setValidators(Validators.required);
this.activityForm.controls['name'].updateValueAndValidity();
this.activityForm.controls['quantity'].setValue(1);
@ -296,23 +320,25 @@ export class CreateOrUpdateActivityDialog implements OnDestroy {
Validators.required
);
this.activityForm.controls['accountId'].updateValueAndValidity();
this.activityForm.controls['currency'].setValue(
this.data.user.settings.baseCurrency
);
this.activityForm.controls['currencyOfFee'].setValue(
this.data.user.settings.baseCurrency
);
this.activityForm.controls['currencyOfUnitPrice'].setValue(
this.data.user.settings.baseCurrency
);
const currency =
this.data.accounts.find(({ id }) => {
return id === this.activityForm.controls['accountId'].value;
})?.currency ?? this.data.user.settings.baseCurrency;
this.activityForm.controls['currency'].setValue(currency);
this.activityForm.controls['currencyOfFee'].setValue(currency);
this.activityForm.controls['currencyOfUnitPrice'].setValue(currency);
this.activityForm.controls['dataSource'].removeValidators(
Validators.required
);
this.activityForm.controls['dataSource'].updateValueAndValidity();
if (
type === 'FEE' &&
this.activityForm.controls['feeInCustomCurrency'].value === 0
(type === 'FEE' &&
this.activityForm.controls['feeInCustomCurrency'].value === 0) ||
type === 'LIABILITY'
) {
this.activityForm.controls['feeInCustomCurrency'].reset();
}

Loading…
Cancel
Save