Browse Source

feat: migrate historical market data editor to form control

Implements #6488

- Migrated historical market data editor from ngModel to ReactiveFormsModule
- Disabled Save button when the form is not dirty
pull/6495/head
Sigmabrogz 3 weeks ago
parent
commit
ea469b7613
  1. 16
      libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor-dialog/historical-market-data-editor-dialog.component.ts
  2. 14
      libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor-dialog/historical-market-data-editor-dialog.html

16
libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor-dialog/historical-market-data-editor-dialog.component.ts

@ -11,7 +11,7 @@ import {
signal signal
} from '@angular/core'; } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { FormControl, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button'; import { MatButtonModule } from '@angular/material/button';
import { DateAdapter, MAT_DATE_LOCALE } from '@angular/material/core'; import { DateAdapter, MAT_DATE_LOCALE } from '@angular/material/core';
import { MatDatepickerModule } from '@angular/material/datepicker'; import { MatDatepickerModule } from '@angular/material/datepicker';
@ -50,7 +50,10 @@ export class GfHistoricalMarketDataEditorDialogComponent implements OnInit {
public readonly data = public readonly data =
inject<HistoricalMarketDataEditorDialogParams>(MAT_DIALOG_DATA); inject<HistoricalMarketDataEditorDialogParams>(MAT_DIALOG_DATA);
protected readonly marketPrice = signal(this.data.marketPrice); public readonly form = new FormGroup({
dateString: new FormControl<string | null>({ value: this.data.dateString, disabled: true }),
marketPrice: new FormControl<number | undefined>(this.data.marketPrice)
});
private readonly destroyRef = inject(DestroyRef); private readonly destroyRef = inject(DestroyRef);
private readonly locale = private readonly locale =
@ -83,14 +86,17 @@ export class GfHistoricalMarketDataEditorDialogComponent implements OnInit {
}) })
.pipe(takeUntilDestroyed(this.destroyRef)) .pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(({ marketPrice }) => { .subscribe(({ marketPrice }) => {
this.marketPrice.set(marketPrice); this.form.controls.marketPrice.setValue(marketPrice);
this.form.controls.marketPrice.markAsDirty();
this.changeDetectorRef.markForCheck(); this.changeDetectorRef.markForCheck();
}); });
} }
public onUpdate() { public onUpdate() {
if (this.marketPrice() === undefined) { const marketPrice = this.form.controls.marketPrice.value;
if (marketPrice === undefined || marketPrice === null) {
return; return;
} }
@ -101,7 +107,7 @@ export class GfHistoricalMarketDataEditorDialogComponent implements OnInit {
marketData: [ marketData: [
{ {
date: this.data.dateString, date: this.data.dateString,
marketPrice: this.marketPrice() marketPrice: marketPrice
} }
] ]
}, },

14
libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor-dialog/historical-market-data-editor-dialog.html

@ -1,4 +1,4 @@
<form class="d-flex flex-column h-100"> <form class="d-flex flex-column h-100" [formGroup]="form">
<h1 i18n mat-dialog-title>Details for {{ data.symbol }}</h1> <h1 i18n mat-dialog-title>Details for {{ data.symbol }}</h1>
<div class="flex-grow-1 py-3" mat-dialog-content> <div class="flex-grow-1 py-3" mat-dialog-content>
<div class="mb-3"> <div class="mb-3">
@ -9,7 +9,7 @@
matInput matInput
name="date" name="date"
[matDatepicker]="date" [matDatepicker]="date"
[(ngModel)]="data.dateString" formControlName="dateString"
/> />
<mat-datepicker-toggle class="mr-2" matSuffix [for]="date"> <mat-datepicker-toggle class="mr-2" matSuffix [for]="date">
<ion-icon <ion-icon
@ -28,8 +28,7 @@
matInput matInput
name="marketPrice" name="marketPrice"
type="number" type="number"
[ngModel]="marketPrice()" formControlName="marketPrice"
(ngModelChange)="marketPrice.set($event)"
/> />
<span class="ml-2" matTextSuffix>{{ data.currency }}</span> <span class="ml-2" matTextSuffix>{{ data.currency }}</span>
</mat-form-field> </mat-form-field>
@ -45,7 +44,12 @@
</div> </div>
<div class="justify-content-end" mat-dialog-actions> <div class="justify-content-end" mat-dialog-actions>
<button i18n mat-button (click)="onCancel()">Cancel</button> <button i18n mat-button (click)="onCancel()">Cancel</button>
<button color="primary" mat-flat-button (click)="onUpdate()"> <button
color="primary"
mat-flat-button
[disabled]="!form.dirty"
(click)="onUpdate()"
>
<ng-container i18n>Save</ng-container> <ng-container i18n>Save</ng-container>
</button> </button>
</div> </div>

Loading…
Cancel
Save