created for validation and save button logic

This commit is contained in:
Joe Arndt 2026-02-14 10:20:23 -06:00
parent 2c0a8da60d
commit 627bdcbd80
3 changed files with 44 additions and 21 deletions

View file

@ -15,12 +15,12 @@
<mat-form-field appearance="outline"> <mat-form-field appearance="outline">
<mat-label>Cents</mat-label> <mat-label>Cents</mat-label>
<input type="number" matInput [formField]="expenseForm.amount"> <input type="number" matInput [formField]="expenseForm.cents">
</mat-form-field> </mat-form-field>
<mat-form-field appearance="outline"> <mat-form-field appearance="outline">
<mat-label>Category</mat-label> <mat-label>Category</mat-label>
<input type="text" matInput [matAutocomplete]="categoryAuto" [formField]="expenseForm.categoryId"> <input type="text" matInput [matAutocomplete]="categoryAuto" [formField]="expenseForm.category">
<mat-autocomplete autoActiveFirstOption #categoryAuto="matAutocomplete"> <mat-autocomplete autoActiveFirstOption #categoryAuto="matAutocomplete">
@for (category of categories(); track category.id) { @for (category of categories(); track category.id) {
<mat-option [value]="category.name">{{ category.name }}</mat-option> <mat-option [value]="category.name">{{ category.name }}</mat-option>
@ -30,7 +30,7 @@
<mat-form-field appearance="outline"> <mat-form-field appearance="outline">
<mat-label>Merchant</mat-label> <mat-label>Merchant</mat-label>
<input type="text" matInput [matAutocomplete]="merchantAuto" [formField]="expenseForm.merchantId"> <input type="text" matInput [matAutocomplete]="merchantAuto" [formField]="expenseForm.merchant">
<mat-autocomplete autoActiveFirstOption #merchantAuto="matAutocomplete"> <mat-autocomplete autoActiveFirstOption #merchantAuto="matAutocomplete">
@for (merchant of merchants(); track merchant.id) { @for (merchant of merchants(); track merchant.id) {
<mat-option [value]="merchant.name">{{ merchant.name }}</mat-option> <mat-option [value]="merchant.name">{{ merchant.name }}</mat-option>
@ -51,14 +51,14 @@
<mat-form-field appearance="outline" class="add-expense-note"> <mat-form-field appearance="outline" class="add-expense-note">
<mat-label>Note</mat-label> <mat-label>Note</mat-label>
<textarea rows="1" matInput></textarea> <textarea rows="1" matInput [formField]="expenseForm.note"></textarea>
</mat-form-field> </mat-form-field>
</div> </div>
</mat-card-content> </mat-card-content>
<mat-card-footer class="add-expense-footer"> <mat-card-footer class="add-expense-footer">
<mat-card-actions> <mat-card-actions>
<button matButton="tonal" disabled="true">Save</button> <button matButton="tonal" [disabled]="!enableSaveButton()" (click)="saveClick()">Save</button>
</mat-card-actions> </mat-card-actions>
</mat-card-footer> </mat-card-footer>
</mat-card> </mat-card>

View file

@ -8,13 +8,13 @@
.add-expense-dropdowns { .add-expense-dropdowns {
display: flex; display: flex;
gap: 0.5rem; gap: 1rem;
justify-content: space-between; justify-content: space-between;
} }
.add-expense-additional { .add-expense-additional {
display: flex; display: flex;
gap: 0.5rem; gap: 1rem;
justify-content: space-between; justify-content: space-between;
} }

View file

@ -1,8 +1,8 @@
import { Component, computed, signal } from '@angular/core'; import { Component, computed, signal } from '@angular/core';
import { CategoryService } from '../../services/category.service'; import { CategoryService } from '../../services/category.service';
import { MerchantService } from '../../services/merchant.service'; import { MerchantService } from '../../services/merchant.service';
import { Tag, TagService } from '../../services/tag.service'; import { TagService } from '../../services/tag.service';
import { form, FormField } from '@angular/forms/signals'; import { form, FormField, min, required } from '@angular/forms/signals';
import { MatCardModule } from '@angular/material/card'; import { MatCardModule } from '@angular/material/card';
import { MatInputModule } from '@angular/material/input'; import { MatInputModule } from '@angular/material/input';
import { MatDatepickerModule } from '@angular/material/datepicker'; import { MatDatepickerModule } from '@angular/material/datepicker';
@ -12,13 +12,13 @@ import { MatSelectModule } from '@angular/material/select';
import { MatButtonModule } from '@angular/material/button'; import { MatButtonModule } from '@angular/material/button';
import { provideNativeDateAdapter } from '@angular/material/core'; import { provideNativeDateAdapter } from '@angular/material/core';
interface AddExpenseForm { interface ExpenseForm {
date: Date, date: Date;
amount: number | string, cents: number;
categoryId: string, category: string;
merchantId: string, merchant: string;
note: string, note: string;
tags: Tag[] tags: string[];
} }
@Component({ @Component({
@ -32,17 +32,40 @@ export class AddExpenseComponent {
protected categories = computed(() => this.categoryService.categories()); protected categories = computed(() => this.categoryService.categories());
protected merchants = computed(() => this.merchantService.merchants()); protected merchants = computed(() => this.merchantService.merchants());
protected tags = computed(() => this.tagService.tags()); protected tags = computed(() => this.tagService.tags());
private addExpenseModel = signal<AddExpenseForm>({ protected enableSaveButton = computed(() => {
const dateValid = this.expenseForm.date().valid();
const centsValid = this.expenseForm.cents().valid();
const categoryValid = this.expenseForm.category().valid();
const merchantValid = this.expenseForm.merchant().valid();
const noteValid = this.expenseForm.note().valid();
return dateValid && centsValid && categoryValid && merchantValid && noteValid;
});
private defaultFormState: ExpenseForm = {
date: new Date(), date: new Date(),
amount: '', cents: NaN,
categoryId: '', category: '',
merchantId: '', merchant: '',
note: '', note: '',
tags: [] tags: []
};
private expenseModel = signal<ExpenseForm>(this.defaultFormState);
public expenseForm = form(this.expenseModel, (schema) => {
required(schema.date);
required(schema.cents);
min(schema.cents, 1);
required(schema.category);
}); });
public expenseForm = form(this.addExpenseModel);
public constructor(private readonly categoryService: CategoryService, public constructor(private readonly categoryService: CategoryService,
private readonly merchantService: MerchantService, private readonly merchantService: MerchantService,
private readonly tagService: TagService) { } private readonly tagService: TagService) { }
public saveClick(): void {
const saveExpense = this.expenseModel();
console.log(saveExpense);
this.expenseModel.set({ ...this.defaultFormState, date: saveExpense.date });
this.expenseForm().reset(this.expenseModel());
}
} }