Browse Source

Task/improve type safety in create or update account dialog (#6683)

* Improve type safety
pull/6682/head
Kenrick Tandrian 1 week ago
committed by GitHub
parent
commit
453d8d8818
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 58
      apps/client/src/app/pages/accounts/create-or-update-account-dialog/create-or-update-account-dialog.component.ts

58
apps/client/src/app/pages/accounts/create-or-update-account-dialog/create-or-update-account-dialog.component.ts

@ -5,7 +5,7 @@ import { GfEntityLogoComponent } from '@ghostfolio/ui/entity-logo';
import { DataService } from '@ghostfolio/ui/services'; import { DataService } from '@ghostfolio/ui/services';
import { CommonModule, NgClass } from '@angular/common'; import { CommonModule, NgClass } from '@angular/common';
import { ChangeDetectionStrategy, Component, Inject } from '@angular/core'; import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { import {
AbstractControl, AbstractControl,
FormBuilder, FormBuilder,
@ -51,17 +51,17 @@ import { CreateOrUpdateAccountDialogParams } from './interfaces/interfaces';
templateUrl: 'create-or-update-account-dialog.html' templateUrl: 'create-or-update-account-dialog.html'
}) })
export class GfCreateOrUpdateAccountDialogComponent { export class GfCreateOrUpdateAccountDialogComponent {
public accountForm: FormGroup; protected accountForm: FormGroup;
public currencies: string[] = []; protected currencies: string[] = [];
public filteredPlatforms: Observable<Platform[]>; protected filteredPlatforms: Observable<Platform[]> | undefined;
public platforms: Platform[] = []; protected platforms: Platform[] = [];
public constructor( protected readonly data =
@Inject(MAT_DIALOG_DATA) public data: CreateOrUpdateAccountDialogParams, inject<CreateOrUpdateAccountDialogParams>(MAT_DIALOG_DATA);
private dataService: DataService, private readonly dataService = inject(DataService);
public dialogRef: MatDialogRef<GfCreateOrUpdateAccountDialogComponent>, private readonly dialogRef =
private formBuilder: FormBuilder inject<MatDialogRef<GfCreateOrUpdateAccountDialogComponent>>(MatDialogRef);
) {} private readonly formBuilder = inject(FormBuilder);
public ngOnInit() { public ngOnInit() {
const { currencies } = this.dataService.fetchInfo(); const { currencies } = this.dataService.fetchInfo();
@ -93,18 +93,18 @@ export class GfCreateOrUpdateAccountDialogComponent {
this.filteredPlatforms = this.accountForm this.filteredPlatforms = this.accountForm
.get('platformId') .get('platformId')
.valueChanges.pipe( ?.valueChanges.pipe(
startWith(''), startWith(''),
map((value) => { map((value: Platform | string) => {
const name = typeof value === 'string' ? value : value?.name; const name = typeof value === 'string' ? value : value?.name;
return name ? this.filter(name as string) : this.platforms.slice(); return name ? this.filter(name) : this.platforms.slice();
}) })
); );
}); });
} }
public autoCompleteCheck() { protected autoCompleteCheck() {
const inputValue = this.accountForm.get('platformId').value; const inputValue = this.accountForm.get('platformId')?.value;
if (typeof inputValue === 'string') { if (typeof inputValue === 'string') {
const matchingEntry = this.platforms.find(({ name }) => { const matchingEntry = this.platforms.find(({ name }) => {
@ -112,28 +112,28 @@ export class GfCreateOrUpdateAccountDialogComponent {
}); });
if (matchingEntry) { if (matchingEntry) {
this.accountForm.get('platformId').setValue(matchingEntry); this.accountForm.get('platformId')?.setValue(matchingEntry);
} }
} }
} }
public displayFn(platform: Platform) { protected displayFn(platform: Platform) {
return platform?.name ?? ''; return platform?.name ?? '';
} }
public onCancel() { protected onCancel() {
this.dialogRef.close(); this.dialogRef.close();
} }
public async onSubmit() { protected async onSubmit() {
const account: CreateAccountDto | UpdateAccountDto = { const account: CreateAccountDto | UpdateAccountDto = {
balance: this.accountForm.get('balance').value, balance: this.accountForm.get('balance')?.value,
comment: this.accountForm.get('comment').value || null, comment: this.accountForm.get('comment')?.value || null,
currency: this.accountForm.get('currency').value, currency: this.accountForm.get('currency')?.value,
id: this.accountForm.get('accountId').value, id: this.accountForm.get('accountId')?.value,
isExcluded: this.accountForm.get('isExcluded').value, isExcluded: this.accountForm.get('isExcluded')?.value,
name: this.accountForm.get('name').value, name: this.accountForm.get('name')?.value,
platformId: this.accountForm.get('platformId').value?.id || null platformId: this.accountForm.get('platformId')?.value?.id || null
}; };
try { try {
@ -177,7 +177,7 @@ export class GfCreateOrUpdateAccountDialogComponent {
const filterValue = value.toLowerCase(); const filterValue = value.toLowerCase();
return this.platforms.filter(({ name }) => { return this.platforms.filter(({ name }) => {
return name.toLowerCase().startsWith(filterValue); return name?.toLowerCase().startsWith(filterValue);
}); });
} }
} }

Loading…
Cancel
Save