Skip to content

Commit

Permalink
Merge pull request #42 from ya-erm/dev
Browse files Browse the repository at this point in the history
Fix transaction form
  • Loading branch information
ya-erm authored Nov 18, 2023
2 parents a21a764 + a792387 commit ed0a8d5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 51 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "client",
"version": "2.6.1",
"version": "2.6.2",
"private": true,
"scripts": {
"dev": "vite dev",
Expand Down
102 changes: 54 additions & 48 deletions src/routes/transactions/form/TransactionForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import InputLabel from '$lib/ui/InputLabel.svelte';
import Modal from '$lib/ui/Modal.svelte';
import { showErrorToast } from '$lib/ui/toasts';
import { formatMoney, getSearchParam, spreadIf } from '$lib/utils';
import { formatMoney, getSearchParam, handleError, spreadIf } from '$lib/utils';
import { replaceCalcExpressions } from '$lib/utils/calc';
import {
checkNumberFormParameter,
Expand Down Expand Up @@ -74,64 +74,70 @@
let anotherCurrency: string | null = transaction?.anotherCurrency ?? null;
const handleSubmit = async (e: Event) => {
const formData = new FormData(e.target as HTMLFormElement);
if (!formData.get('accountId')) {
showErrorToast($translate('transactions.account_is_required'), { testId: 'AccountIsRequiredErrorToast' });
return;
}
if ((type === 'IN' || type === 'OUT') && !formData.get('categoryId')) {
showErrorToast($translate('transactions.category_is_required'), { testId: 'CategoryIsRequiredErrorToast' });
return;
}
if (type === 'TRANSFER' && !formData.get('destinationAccountId')) {
showErrorToast($translate('transactions.destination_account_is_required'), {
testId: 'DestinationAccountIsRequiredErrorToast',
});
return;
}
if (type === 'TRANSFER' && formData.get('accountId') === formData.get('destinationAccountId')) {
showErrorToast($translate('transactions.accounts_must_be_different'), {
testId: 'AccountsMustBeDifferentErrorToast',
});
return;
}
const transactions: Transaction[] = [];
try {
const formData = new FormData(e.target as HTMLFormElement);
if (!formData.get('accountId')) {
showErrorToast($translate('transactions.account_is_required'), { testId: 'AccountIsRequiredErrorToast' });
return;
}
if ((type === 'IN' || type === 'OUT') && !formData.get('categoryId')) {
showErrorToast($translate('transactions.category_is_required'), { testId: 'CategoryIsRequiredErrorToast' });
return;
}
if (type === 'TRANSFER' && !formData.get('destinationAccountId')) {
showErrorToast($translate('transactions.destination_account_is_required'), {
testId: 'DestinationAccountIsRequiredErrorToast',
});
return;
}
if (type === 'TRANSFER' && formData.get('accountId') === formData.get('destinationAccountId')) {
showErrorToast($translate('transactions.accounts_must_be_different'), {
testId: 'AccountsMustBeDifferentErrorToast',
});
return;
}
transactions.push({
id: transaction?.id ?? uuid(),
accountId: checkStringFormParameter(formData, 'accountId'),
categoryId:
type === 'TRANSFER' ? SYSTEM_CATEGORY_TRANSFER_OUT.id : checkStringFormParameter(formData, 'categoryId'),
date: datetime,
amount: checkNumberFormParameter(formData, 'amount'),
comment: checkStringOptionalFormParameter(formData, 'comment'),
tagIds: selectedTags,
...spreadIf(!!anotherCurrency, {
anotherCurrency,
anotherCurrencyAmount: checkNumberFormParameter(formData, 'destinationAmount'),
}),
});
const transactions: Transaction[] = [];
if (type === 'TRANSFER') {
transactions.push({
id: transaction?.linkedTransactionId ?? uuid(),
accountId: checkStringFormParameter(formData, 'destinationAccountId'),
categoryId: SYSTEM_CATEGORY_TRANSFER_IN.id,
id: transaction?.id ?? uuid(),
accountId: checkStringFormParameter(formData, 'accountId'),
categoryId:
type === 'TRANSFER' ? SYSTEM_CATEGORY_TRANSFER_OUT.id : checkStringFormParameter(formData, 'categoryId'),
date: datetime,
amount: checkNumberFormParameter(formData, 'destinationAmount'),
amount: checkNumberFormParameter(formData, 'amount'),
comment: checkStringOptionalFormParameter(formData, 'comment'),
tagIds: selectedTags,
linkedTransactionId: transactions[0].id,
...(!!anotherCurrency
? {
anotherCurrency,
anotherCurrencyAmount: checkNumberFormParameter(formData, 'destinationAmount'),
}
: {}),
});
transactions[0].linkedTransactionId = transactions[1].id;
}
onSubmit(transactions);
if (type === 'TRANSFER') {
transactions.push({
id: transaction?.linkedTransactionId ?? uuid(),
accountId: checkStringFormParameter(formData, 'destinationAccountId'),
categoryId: SYSTEM_CATEGORY_TRANSFER_IN.id,
date: datetime,
amount: checkNumberFormParameter(formData, 'destinationAmount'),
comment: checkStringOptionalFormParameter(formData, 'comment'),
tagIds: selectedTags,
linkedTransactionId: transactions[0].id,
});
transactions[0].linkedTransactionId = transactions[1].id;
}
onSubmit(transactions);
} catch (e) {
handleError(e);
}
};
</script>

<form on:submit|preventDefault={(e) => handleSubmit(e)} data-testId="TransactionForm">
<form on:submit|preventDefault={handleSubmit} data-testId="TransactionForm">
<div class="flex-col gap-1 p-1">
<TypeSwitch bind:type disabled={isTransfer} />
{#if type === 'OUT'}
Expand Down

1 comment on commit ed0a8d5

@vercel
Copy link

@vercel vercel bot commented on ed0a8d5 Nov 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.