From c668d7b45623e532eb60da04a54d095c1ab41eba Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Mon, 18 Sep 2023 19:45:02 +0200 Subject: [PATCH] Feature/improve preselected currency in create or update activity dialog (#2349) * Preselect currency based on account's currency * Update changelog --- CHANGELOG.md | 4 ++ ...ate-or-update-activity-dialog.component.ts | 70 +++++++++++++------ 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ab47a335..c05617082 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts b/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts index ee7bbcaaa..72f308315 100644 --- a/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts +++ b/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts @@ -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(); }