From b41f7219b349e476ba7e937ac0633c1312679df4 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sat, 21 Oct 2023 18:06:51 +0200 Subject: [PATCH] Refactoring --- ...eate-or-update-account-dialog.component.ts | 5 ---- .../create-or-update-account-dialog.html | 1 - .../currency-selector.component.ts | 23 ++++++++++++------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/apps/client/src/app/pages/accounts/create-or-update-account-dialog/create-or-update-account-dialog.component.ts b/apps/client/src/app/pages/accounts/create-or-update-account-dialog/create-or-update-account-dialog.component.ts index 10726afdd..9e153d173 100644 --- a/apps/client/src/app/pages/accounts/create-or-update-account-dialog/create-or-update-account-dialog.component.ts +++ b/apps/client/src/app/pages/accounts/create-or-update-account-dialog/create-or-update-account-dialog.component.ts @@ -31,7 +31,6 @@ import { CreateOrUpdateAccountDialogParams } from './interfaces/interfaces'; }) export class CreateOrUpdateAccountDialog implements OnDestroy { public accountForm: FormGroup; - public accountCurrency: Currency; public currencies: Currency[] = []; public filteredPlatforms: Observable; public platforms: Platform[]; @@ -52,10 +51,6 @@ export class CreateOrUpdateAccountDialog implements OnDestroy { label: currency, value: currency })); - this.accountCurrency = { - label: this.data.account.currency, - value: this.data.account.currency - }; this.platforms = platforms; this.accountForm = this.formBuilder.group({ diff --git a/apps/client/src/app/pages/accounts/create-or-update-account-dialog/create-or-update-account-dialog.html b/apps/client/src/app/pages/accounts/create-or-update-account-dialog/create-or-update-account-dialog.html index 15afabc26..7c0214a0f 100644 --- a/apps/client/src/app/pages/accounts/create-or-update-account-dialog/create-or-update-account-dialog.html +++ b/apps/client/src/app/pages/accounts/create-or-update-account-dialog/create-or-update-account-dialog.html @@ -23,7 +23,6 @@ diff --git a/libs/ui/src/lib/currency-selector/currency-selector.component.ts b/libs/ui/src/lib/currency-selector/currency-selector.component.ts index 4e6b59d18..eda935caa 100644 --- a/libs/ui/src/lib/currency-selector/currency-selector.component.ts +++ b/libs/ui/src/lib/currency-selector/currency-selector.component.ts @@ -9,7 +9,7 @@ import { OnInit, ViewChild } from '@angular/core'; -import { FormControl, NgControl } from '@angular/forms'; +import { FormControl, FormGroupDirective, NgControl } from '@angular/forms'; import { MatAutocomplete, MatAutocompleteSelectedEvent @@ -42,7 +42,7 @@ export class CurrencySelectorComponent implements OnInit, OnDestroy { @Input() private currencies: Currency[] = []; - @Input() defaultValue: Currency; + @Input() private formControlName: string; @ViewChild(MatInput, { static: false }) private input: MatInput; @@ -58,6 +58,7 @@ export class CurrencySelectorComponent public readonly _elementRef: ElementRef, public readonly _focusMonitor: FocusMonitor, public readonly changeDetectorRef: ChangeDetectorRef, + private readonly formGroupDirective: FormGroupDirective, public readonly ngControl: NgControl ) { super(_elementRef, _focusMonitor, ngControl); @@ -65,17 +66,23 @@ export class CurrencySelectorComponent this.controlType = 'currency-autocomplete'; } - public ngOnChanges() { - if (this.defaultValue) { - this.value = this.defaultValue; - } - } - public ngOnInit() { if (this.disabled) { this.control.disable(); } + const formGroup = this.formGroupDirective.form; + + if (formGroup) { + const control = formGroup.get(this.formControlName); + + if (control) { + this.value = this.currencies.find(({ value }) => { + return value === control.value; + }); + } + } + this.control.valueChanges .pipe(takeUntil(this.unsubscribeSubject)) .subscribe(() => {