From e5c694339ac434f1bc634e55f7dbbd36afd5c695 Mon Sep 17 00:00:00 2001 From: Kenrick Tandrian <60643640+KenTandrian@users.noreply.github.com> Date: Sun, 21 Jun 2026 22:33:49 +0700 Subject: [PATCH] Task/improve type safety in transfer balance dialog component (#7080) Improve type safety --- .../transfer-balance/interfaces/interfaces.ts | 7 ++ .../transfer-balance-dialog.component.ts | 78 ++++++++++--------- 2 files changed, 50 insertions(+), 35 deletions(-) diff --git a/apps/client/src/app/pages/accounts/transfer-balance/interfaces/interfaces.ts b/apps/client/src/app/pages/accounts/transfer-balance/interfaces/interfaces.ts index 3a0b921fd..51c42bc5d 100644 --- a/apps/client/src/app/pages/accounts/transfer-balance/interfaces/interfaces.ts +++ b/apps/client/src/app/pages/accounts/transfer-balance/interfaces/interfaces.ts @@ -1,5 +1,12 @@ +import { FormControl, FormGroup } from '@angular/forms'; import { Account } from '@prisma/client'; export interface TransferBalanceDialogParams { accounts: Account[]; } + +export type TransferBalanceForm = FormGroup<{ + balance: FormControl; + fromAccount: FormControl; + toAccount: FormControl; +}>; diff --git a/apps/client/src/app/pages/accounts/transfer-balance/transfer-balance-dialog.component.ts b/apps/client/src/app/pages/accounts/transfer-balance/transfer-balance-dialog.component.ts index 34a66b156..cbf0e460d 100644 --- a/apps/client/src/app/pages/accounts/transfer-balance/transfer-balance-dialog.component.ts +++ b/apps/client/src/app/pages/accounts/transfer-balance/transfer-balance-dialog.component.ts @@ -1,10 +1,9 @@ import { TransferBalanceDto } from '@ghostfolio/common/dtos'; import { GfEntityLogoComponent } from '@ghostfolio/ui/entity-logo'; -import { ChangeDetectionStrategy, Component, Inject } from '@angular/core'; +import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; import { - AbstractControl, - FormBuilder, + FormControl, FormGroup, ReactiveFormsModule, ValidationErrors, @@ -21,7 +20,10 @@ import { MatInputModule } from '@angular/material/input'; import { MatSelectModule } from '@angular/material/select'; import { Account } from '@prisma/client'; -import { TransferBalanceDialogParams } from './interfaces/interfaces'; +import { + TransferBalanceDialogParams, + TransferBalanceForm +} from './interfaces/interfaces'; @Component({ changeDetection: ChangeDetectionStrategy.OnPush, @@ -40,57 +42,63 @@ import { TransferBalanceDialogParams } from './interfaces/interfaces'; templateUrl: 'transfer-balance-dialog.html' }) export class GfTransferBalanceDialogComponent { - public accounts: Account[] = []; - public currency: string; - public transferBalanceForm: FormGroup; + protected readonly accounts: Account[] = + inject(MAT_DIALOG_DATA).accounts; + + protected currency: string; + + protected readonly transferBalanceForm: TransferBalanceForm = new FormGroup( + { + balance: new FormControl('', Validators.required), + fromAccount: new FormControl('', Validators.required), + toAccount: new FormControl('', Validators.required) + }, + { + validators: this.compareAccounts + } + ); - public constructor( - @Inject(MAT_DIALOG_DATA) public data: TransferBalanceDialogParams, - public dialogRef: MatDialogRef, - private formBuilder: FormBuilder - ) {} + private readonly dialogRef = + inject>(MatDialogRef); public ngOnInit() { - this.accounts = this.data.accounts; + this.transferBalanceForm.controls.fromAccount.valueChanges.subscribe( + (id) => { + const currency = this.accounts.find((account) => { + return account.id === id; + })?.currency; - this.transferBalanceForm = this.formBuilder.group( - { - balance: ['', Validators.required], - fromAccount: ['', Validators.required], - toAccount: ['', Validators.required] - }, - { - validators: this.compareAccounts + if (currency) { + this.currency = currency; + } } ); - - this.transferBalanceForm.get('fromAccount').valueChanges.subscribe((id) => { - this.currency = this.accounts.find((account) => { - return account.id === id; - }).currency; - }); } - public onCancel() { + protected onCancel() { this.dialogRef.close(); } - public onSubmit() { + protected onSubmit() { const account: TransferBalanceDto = { - accountIdFrom: this.transferBalanceForm.get('fromAccount').value, - accountIdTo: this.transferBalanceForm.get('toAccount').value, - balance: this.transferBalanceForm.get('balance').value + accountIdFrom: this.transferBalanceForm.controls.fromAccount.value ?? '', + accountIdTo: this.transferBalanceForm.controls.toAccount.value ?? '', + balance: Number(this.transferBalanceForm.controls.balance.value) }; this.dialogRef.close({ account }); } - private compareAccounts(control: AbstractControl): ValidationErrors { - const accountFrom = control.get('fromAccount'); - const accountTo = control.get('toAccount'); + private compareAccounts( + formGroup: TransferBalanceForm + ): ValidationErrors | null { + const accountFrom = formGroup.controls.fromAccount; + const accountTo = formGroup.controls.toAccount; if (accountFrom.value === accountTo.value) { return { invalid: true }; } + + return null; } }