From e74e1c1cefdf9f51dc77b27fa327ea7a3d34a81a Mon Sep 17 00:00:00 2001 From: Yong Sheng Tan Date: Wed, 22 Nov 2023 18:15:37 +0800 Subject: [PATCH] Fix category map using lowest weight suggestion --- src/settings/templates.jsx | 10 +++++++--- src/transactions/add-transaction-dialog.jsx | 10 +++++++--- src/transactions/bulk-transaction-dialog.jsx | 10 +++++++--- src/transactions/transactions-import.jsx | 10 +++++++--- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/settings/templates.jsx b/src/settings/templates.jsx index 2e88a5c..55e06d1 100644 --- a/src/settings/templates.jsx +++ b/src/settings/templates.jsx @@ -150,9 +150,13 @@ const Templates = ({ isMobile }) => { useEffect(() => { setCategoryOptions(prepareOptions(categories, 'category')); setSubCategoryOptions(prepareOptions(categories, 'subCategory')); - setCategoryMap( - categories.reduce((o, c) => ({ ...o, [c.subCategory]: c.category }), {}) - ); + const tempMap = {}; + categories.forEach(({ category, subCategory }) => { + if (!tempMap[subCategory]) { + tempMap[subCategory] = category; + } + }); + setCategoryMap(tempMap); }, [ categories ]); const postProcess = (row, newRows, verb) => { diff --git a/src/transactions/add-transaction-dialog.jsx b/src/transactions/add-transaction-dialog.jsx index d06d4a0..6072d44 100644 --- a/src/transactions/add-transaction-dialog.jsx +++ b/src/transactions/add-transaction-dialog.jsx @@ -80,9 +80,13 @@ const AddTransactionDialog = ({ useEffect(() => { setCategoryOptions(prepareOptions(categories, 'category')); setSubCategoryOptions(prepareOptions(categories, 'subCategory')); - setCategoryMap( - categories.reduce((o, c) => ({ ...o, [c.subCategory]: c.category }), {}) - ); + const tempMap = {}; + categories.forEach(({ category, subCategory }) => { + if (!tempMap[subCategory]) { + tempMap[subCategory] = category; + } + }); + setCategoryMap(tempMap); }, [ categories ]); useEffect(() => { diff --git a/src/transactions/bulk-transaction-dialog.jsx b/src/transactions/bulk-transaction-dialog.jsx index 2ba6ae0..d73d743 100644 --- a/src/transactions/bulk-transaction-dialog.jsx +++ b/src/transactions/bulk-transaction-dialog.jsx @@ -53,9 +53,13 @@ const BulkTransactionDialog = ({ useEffect(() => { setCategoryOptions(prepareOptions(categories, 'category')); setSubCategoryOptions(prepareOptions(categories, 'subCategory')); - setCategoryMap( - categories.reduce((o, c) => ({ ...o, [c.subCategory]: c.category }), {}) - ); + const tempMap = {}; + categories.forEach(({ category, subCategory }) => { + if (!tempMap[subCategory]) { + tempMap[subCategory] = category; + } + }); + setCategoryMap(tempMap); }, [ categories ]); const submit = (event) => { diff --git a/src/transactions/transactions-import.jsx b/src/transactions/transactions-import.jsx index 1552d6c..62c6688 100644 --- a/src/transactions/transactions-import.jsx +++ b/src/transactions/transactions-import.jsx @@ -70,9 +70,13 @@ const TransactionsImport = ({ setImportMode, selectedAccount }) => { useEffect(() => { setCategoryOptions(prepareOptions(categories, 'category')); setSubCategoryOptions(prepareOptions(categories, 'subCategory')); - setCategoryMap( - categories.reduce((o, c) => ({ ...o, [c.subCategory]: c.category }), {}) - ); + const tempMap = {}; + categories.forEach(({ category, subCategory }) => { + if (!tempMap[subCategory]) { + tempMap[subCategory] = category; + } + }); + setCategoryMap(tempMap); }, [ categories ]); const maxGridSize = {