diff --git a/src/app/categories/category-form/category-form.component.ts b/src/app/categories/category-form/category-form.component.ts index f6ebd8b..12a3675 100644 --- a/src/app/categories/category-form/category-form.component.ts +++ b/src/app/categories/category-form/category-form.component.ts @@ -2,6 +2,7 @@ import { Component, OnInit, Input, Inject } from '@angular/core'; import { Category } from '../category'; import { AppComponent } from 'src/app/app.component'; import { TWIGS_SERVICE, TwigsService } from 'src/app/shared/twigs.service'; +import { decimalToInteger } from 'src/app/shared/utils'; @Component({ selector: 'app-category-form', @@ -27,6 +28,7 @@ export class CategoryFormComponent implements OnInit { save(): void { let promise; + this.currentCategory.amount = decimalToInteger(String(this.currentCategory.amount)) if (this.create) { // This is a new category, save it promise = this.twigsService.createCategory( @@ -34,14 +36,13 @@ export class CategoryFormComponent implements OnInit { this.budgetId, this.currentCategory.title, this.currentCategory.description, - this.currentCategory.amount * 100, + this.currentCategory.amount, this.currentCategory.expense ); } else { // This is an existing category, update it const updatedCategory: Category = { ...this.currentCategory, - amount: this.currentCategory.amount * 100 } promise = this.twigsService.updateCategory( this.currentCategory.id, diff --git a/src/app/shared/utils.ts b/src/app/shared/utils.ts index f96bd0d..e13714b 100644 --- a/src/app/shared/utils.ts +++ b/src/app/shared/utils.ts @@ -5,3 +5,11 @@ export function randomId(): string { window.crypto.getRandomValues(bytes) return Array.from(bytes, (byte) => CHARACTERS[byte % CHARACTERS.length]).join('') } + +export function decimalToInteger(amount: string): number { + if (amount[amount.length - 3] === "." || amount[amount.length - 3] === ",") { + return Number(amount.replace(/[,.]/g, "")) + } else { + return Number(amount + "00") + } +} \ No newline at end of file diff --git a/src/app/transactions/add-edit-transaction/add-edit-transaction.component.ts b/src/app/transactions/add-edit-transaction/add-edit-transaction.component.ts index 93282b2..13cca39 100644 --- a/src/app/transactions/add-edit-transaction/add-edit-transaction.component.ts +++ b/src/app/transactions/add-edit-transaction/add-edit-transaction.component.ts @@ -5,6 +5,7 @@ import { Category } from 'src/app/categories/category'; import { AppComponent } from 'src/app/app.component'; import { TWIGS_SERVICE, TwigsService } from 'src/app/shared/twigs.service'; import { MatRadioChange } from '@angular/material/radio'; +import { decimalToInteger } from 'src/app/shared/utils'; @Component({ selector: 'app-add-edit-transaction', @@ -60,9 +61,8 @@ export class AddEditTransactionComponent implements OnInit, OnChanges { } save(): void { - // The amount will be input as a decimal value so we need to convert it - // to an integer let promise; + this.currentTransaction.amount = decimalToInteger(String(this.currentTransaction.amount)) this.currentTransaction.date = new Date(); const dateParts = this.transactionDate.split('-'); this.currentTransaction.date.setFullYear(parseInt(dateParts[0], 10)); @@ -78,7 +78,7 @@ export class AddEditTransactionComponent implements OnInit, OnChanges { this.budgetId, this.currentTransaction.title, this.currentTransaction.description, - Math.round(this.currentTransaction.amount * 100), + this.currentTransaction.amount, this.currentTransaction.date, this.currentTransaction.expense, this.currentTransaction.categoryId, @@ -87,7 +87,6 @@ export class AddEditTransactionComponent implements OnInit, OnChanges { // This is an existing transaction, update it const updatedTransaction: Transaction = { ...this.currentTransaction, - amount: Math.round(this.currentTransaction.amount * 100) } promise = this.twigsService.updateTransaction( this.currentTransaction.id,