From 6b6f709ffe56708b4f8412887683fff02bad31e3 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sat, 7 Oct 2023 19:21:44 +0200 Subject: [PATCH] Refactoring --- ...eate-or-update-account-dialog.component.ts | 56 ++++++++++--------- .../create-or-update-account-dialog.html | 3 +- 2 files changed, 32 insertions(+), 27 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 7ce907186..e2c63f191 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 @@ -43,7 +43,7 @@ export class CreateOrUpdateAccountDialog implements OnDestroy { private formBuilder: FormBuilder ) {} - ngOnInit() { + public ngOnInit() { const { currencies, platforms } = this.dataService.fetchInfo(); this.currencies = currencies; @@ -60,20 +60,39 @@ export class CreateOrUpdateAccountDialog implements OnDestroy { this.platforms.find(({ id }) => { return id === this.data.account.platformId; }), - this._autocompleteObjectValidator() + this.autocompleteObjectValidator() ] }); + this.filteredPlatforms = this.accountForm .get('platformId') .valueChanges.pipe( startWith(''), map((value) => { const name = typeof value === 'string' ? value : value?.name; - return name ? this._filter(name as string) : this.platforms.slice(); + return name ? this.filter(name as string) : this.platforms.slice(); }) ); } + public autoCompleteCheck() { + const inputValue = this.accountForm.controls['platformId'].value; + + if (typeof inputValue === 'string') { + const matchingEntry = this.platforms.find(({ name }) => { + return name === inputValue; + }); + + if (matchingEntry) { + this.accountForm.controls['platformId'].setValue(matchingEntry); + } + } + } + + public displayFn(platform: Platform) { + return platform?.name ?? ''; + } + public onCancel() { this.dialogRef.close(); } @@ -88,6 +107,7 @@ export class CreateOrUpdateAccountDialog implements OnDestroy { name: this.accountForm.controls['name'].value, platformId: this.accountForm.controls['platformId'].value?.id ?? null }; + if (this.data.account.id) { (account as UpdateAccountDto).id = this.data.account.id; } else { @@ -102,35 +122,21 @@ export class CreateOrUpdateAccountDialog implements OnDestroy { this.unsubscribeSubject.complete(); } - public autoCompleteCheck() { - const inputValue = this.accountForm.controls['platformId'].value; - if (typeof inputValue === 'string') { - const matchingEntry = this.platforms.find(({ name }) => { - return name === inputValue; - }); - if (matchingEntry) { - this.accountForm.controls['platformId'].setValue(matchingEntry); + private autocompleteObjectValidator(): ValidatorFn { + return (control: AbstractControl) => { + if (control.value && typeof control.value === 'string') { + return { invalidAutocompleteObject: { value: control.value } }; } - } - } - public displayFn(platform: Platform) { - return platform?.name ?? ''; + return null; + }; } - private _filter(value: string): Platform[] { + private filter(value: string): Platform[] { const filterValue = value.toLowerCase(); + return this.platforms.filter(({ name }) => { return name.toLowerCase().startsWith(filterValue); }); } - - private _autocompleteObjectValidator(): ValidatorFn { - return (control: AbstractControl) => { - if (control.value && typeof control.value === 'string') { - return { invalidAutocompleteObject: { value: control.value } }; - } - return null; - }; - } } 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 bfd065410..91efd0972 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 @@ -42,14 +42,13 @@