Skip to content

Commit

Permalink
Fix sub-category bugs in post-tx-edit and remarks lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
ystxn committed Mar 16, 2024
1 parent 14c1e6f commit 0612085
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/transactions/add-transaction-dialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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);
}
};

Expand Down
7 changes: 6 additions & 1 deletion src/transactions/bulk-transaction-dialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down

0 comments on commit 0612085

Please sign in to comment.