From 406d28af1bf56577b34398fbd1ec5cacdc4d766d Mon Sep 17 00:00:00 2001 From: Yaroslav Ermilov <25526713+ya-erm@users.noreply.github.com> Date: Sat, 18 Nov 2023 05:52:58 +0300 Subject: [PATCH 1/2] Fix transaction form --- .../transactions/form/TransactionForm.svelte | 102 +++++++++--------- 1 file changed, 54 insertions(+), 48 deletions(-) diff --git a/src/routes/transactions/form/TransactionForm.svelte b/src/routes/transactions/form/TransactionForm.svelte index 7f92416..e03d7d0 100644 --- a/src/routes/transactions/form/TransactionForm.svelte +++ b/src/routes/transactions/form/TransactionForm.svelte @@ -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, @@ -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); + } }; -
handleSubmit(e)} data-testId="TransactionForm"> +
{#if type === 'OUT'} From a792387b57686c3b3889a80826c325b68a5f983a Mon Sep 17 00:00:00 2001 From: Yaroslav Ermilov <25526713+ya-erm@users.noreply.github.com> Date: Sat, 18 Nov 2023 05:53:19 +0300 Subject: [PATCH 2/2] Version 2.6.2 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index cc12b1c..e920172 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "client", - "version": "2.6.1", + "version": "2.6.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "client", - "version": "2.6.1", + "version": "2.6.2", "dependencies": { "@prisma/client": "^4.14.1", "@vercel/analytics": "^1.0.1", diff --git a/package.json b/package.json index c2cf8f1..7c5f5c6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "client", - "version": "2.6.1", + "version": "2.6.2", "private": true, "scripts": { "dev": "vite dev",