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); + } }; -