diff --git a/src/transactions/add-transaction-dialog.jsx b/src/transactions/add-transaction-dialog.jsx index 3c40f90..1cf4e93 100644 --- a/src/transactions/add-transaction-dialog.jsx +++ b/src/transactions/add-transaction-dialog.jsx @@ -111,10 +111,11 @@ const AddTransactionDialog = ({ ...Object.fromEntries(new FormData(event.target).entries()), }; - const parts = tx.category.split(':'); - tx.category = parts.shift().trim(); - tx.subCategory = parts.join(':').trim() || tx.category; - + if (tx.category) { + const parts = tx.category.split(':'); + tx.category = parts.shift().trim(); + tx.subCategory = parts.join(':').trim() || tx.category; + } if (tx.remarks) { tx.remarks = tx.remarks.trim(); } @@ -144,7 +145,13 @@ const AddTransactionDialog = ({ const endpoint = transactionToEdit ? editTransaction : addTransaction; const postProcess = (response, tx) => { - setTransactions(tx); + const processedTx = tx.map((o) => { + const category = o.category && o.category.indexOf(':') === -1 && o.subCategory && o.subCategory !== o.category + ? `${o.category}: ${o.subCategory}` + : o.category; + return { ...o, category }; + }); + setTransactions(processedTx); setSelectedRows(response.map(({ id }) => id)); setShowAddDialog(false); showStatus('success', 'Transaction ' + (transactionToEdit ? 'edited' : 'added')); @@ -230,13 +237,13 @@ const AddTransactionDialog = ({ onInput: (e) => e.target.value = restrictFormat(e.target.value, true), }; - const lookupCategory = (e, value) => { + const lookupCategory = (_, value) => { if (!value) { return; } const match = transactions.find((t) => t.remarks === value); if (match) { - setCategory(match.subCategory ? `${match.category}: ${match.subCategory}` : match.category); + setCategory(match.category); } }; diff --git a/src/transactions/bulk-transaction-dialog.jsx b/src/transactions/bulk-transaction-dialog.jsx index f3cd978..88b9a75 100644 --- a/src/transactions/bulk-transaction-dialog.jsx +++ b/src/transactions/bulk-transaction-dialog.jsx @@ -72,11 +72,16 @@ const BulkTransactionDialog = ({ } setLoading(true); bulkEditTransactions(values, () => { + const category = values.category && values.category.indexOf(':') === -1 + && values.subCategory && values.subCategory !== values.category + ? `${values.category}: ${values.subCategory}` + : values.category; + const revised = [ ...transactions ].map((t) => (transactionToEdit.indexOf(t.id) === -1) ? t : ({ ...t, billingMonth: values.billingMonth || t.billingMonth, - category: values.category || t.category, + category: category || t.category, subCategory: values.subCategory || t.subCategory, remarks: values.remarks || t.remarks, })