From 2cd09a7f0141cd575b85cfe39630ab249d32fef6 Mon Sep 17 00:00:00 2001 From: Drishit Mitra Date: Thu, 21 Dec 2023 14:58:10 +0530 Subject: [PATCH] Fix: Create budget and remaining budget not working (#53) * Fix: Create budget and remaining budget not working --- app/components/TargetSetter.tsx | 15 +++++++++----- app/routes/transaction/create.tsx | 20 +++++++++---------- .../transaction/edit/$transactionId.tsx | 20 +++++++++---------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/app/components/TargetSetter.tsx b/app/components/TargetSetter.tsx index ee73bc9..6612368 100644 --- a/app/components/TargetSetter.tsx +++ b/app/components/TargetSetter.tsx @@ -145,15 +145,20 @@ export default function TargetSetter({ }, [categoryBudgets]); useEffect(() => { - if (previousBudgetFetcher.type === "done") { - if (previousBudgetFetcher.data && previousBudgetFetcher.data.length > 0) { - setCategoryBudgets([...categoryBudgets, ...previousBudgetFetcher.data]); - setMaxKey(previousBudgetFetcher.data.length); + if (previousBudgetFetcher.data != null) { + const budgetData = previousBudgetFetcher.data as { + index: number; + category: string; + budget: string; + }[]; + if (budgetData.length > 0) { + setCategoryBudgets([...categoryBudgets, ...budgetData]); + setMaxKey(budgetData.length); } else { context.setSnackBarMsg("No budget found for previous month"); } } - }, [previousBudgetFetcher.type]); + }, [previousBudgetFetcher.data]); return ( <> diff --git a/app/routes/transaction/create.tsx b/app/routes/transaction/create.tsx index 39182fb..83184de 100644 --- a/app/routes/transaction/create.tsx +++ b/app/routes/transaction/create.tsx @@ -279,18 +279,16 @@ export default function Create() { }, [selectedCategories]); useEffect(() => { - if (categoryRemainingBudgetFetcher.type === "done") { - if (categoryRemainingBudgetFetcher.data) { - categoryRemainingBudgetMap.current = { - ...categoryRemainingBudgetMap.current, - ...(categoryRemainingBudgetFetcher.data as { - [key: string]: number | null; - }), - }; - calculateRemainingBudget(amount); - } + if (categoryRemainingBudgetFetcher.data) { + categoryRemainingBudgetMap.current = { + ...categoryRemainingBudgetMap.current, + ...(categoryRemainingBudgetFetcher.data as { + [key: string]: number | null; + }), + }; + calculateRemainingBudget(amount); } - }, [amount, calculateRemainingBudget, categoryRemainingBudgetFetcher]); + }, [amount, calculateRemainingBudget, categoryRemainingBudgetFetcher.data]); useEffect(() => { calculateRemainingBudget(amount); diff --git a/app/routes/transaction/edit/$transactionId.tsx b/app/routes/transaction/edit/$transactionId.tsx index 710bd6a..9f83ef9 100644 --- a/app/routes/transaction/edit/$transactionId.tsx +++ b/app/routes/transaction/edit/$transactionId.tsx @@ -281,18 +281,16 @@ export default function EditTransaction() { }, [selectedCategories]); useEffect(() => { - if (categoryRemainingBudgetFetcher.type === "done") { - if (categoryRemainingBudgetFetcher.data) { - categoryRemainingBudgetMap.current = { - ...categoryRemainingBudgetMap.current, - ...(categoryRemainingBudgetFetcher.data as { - [key: string]: number | null; - }), - }; - calculateRemainingBudget(amount); - } + if (categoryRemainingBudgetFetcher.data) { + categoryRemainingBudgetMap.current = { + ...categoryRemainingBudgetMap.current, + ...(categoryRemainingBudgetFetcher.data as { + [key: string]: number | null; + }), + }; + calculateRemainingBudget(amount); } - }, [amount, calculateRemainingBudget, categoryRemainingBudgetFetcher]); + }, [amount, calculateRemainingBudget, categoryRemainingBudgetFetcher.data]); useEffect(() => { calculateRemainingBudget(amount);