Fix data display issues
Addresses issues where budget and expense data were not displaying correctly on the summary cards, category bar graph, and transaction/analytics pages. Also fixes a bug where data was disappearing after input.
This commit is contained in:
@@ -6,11 +6,6 @@ import { useTransactionState } from './hooks/useTransactionState';
|
||||
import { useCategoryBudgetState } from './hooks/useCategoryBudgetState';
|
||||
import { useBudgetDataState } from './hooks/useBudgetDataState';
|
||||
import { useCategorySpending } from './hooks/useCategorySpending';
|
||||
import {
|
||||
clearAllTransactions,
|
||||
clearAllCategoryBudgets,
|
||||
clearAllBudgetData
|
||||
} from './storage';
|
||||
|
||||
export const useBudgetState = () => {
|
||||
// 각 상태 관리 훅 사용
|
||||
@@ -39,33 +34,45 @@ export const useBudgetState = () => {
|
||||
|
||||
const { getCategorySpending } = useCategorySpending(transactions, categoryBudgets);
|
||||
|
||||
// 디버깅을 위한 로그
|
||||
useEffect(() => {
|
||||
console.log('현재 예산 데이터:', budgetData);
|
||||
console.log('현재 카테고리 예산:', categoryBudgets);
|
||||
console.log('현재 거래 수:', transactions.length);
|
||||
}, [budgetData, categoryBudgets, transactions]);
|
||||
|
||||
// 카테고리별 예산 및 지출 계산
|
||||
useEffect(() => {
|
||||
const totalMonthlyBudget = Object.values(categoryBudgets).reduce((sum, value) => sum + value, 0);
|
||||
const totalDailyBudget = Math.round(totalMonthlyBudget / 30);
|
||||
const totalWeeklyBudget = Math.round(totalMonthlyBudget / 4.3);
|
||||
|
||||
const updatedBudgetData = {
|
||||
daily: {
|
||||
targetAmount: totalDailyBudget,
|
||||
spentAmount: budgetData.daily.spentAmount,
|
||||
remainingAmount: totalDailyBudget - budgetData.daily.spentAmount
|
||||
},
|
||||
weekly: {
|
||||
targetAmount: totalWeeklyBudget,
|
||||
spentAmount: budgetData.weekly.spentAmount,
|
||||
remainingAmount: totalWeeklyBudget - budgetData.weekly.spentAmount
|
||||
},
|
||||
monthly: {
|
||||
targetAmount: totalMonthlyBudget,
|
||||
spentAmount: budgetData.monthly.spentAmount,
|
||||
remainingAmount: totalMonthlyBudget - budgetData.monthly.spentAmount
|
||||
}
|
||||
};
|
||||
console.log('카테고리 예산 합계:', totalMonthlyBudget);
|
||||
|
||||
// 로컬 이벤트 발생 (다른 컴포넌트에서 변경 감지하도록)
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
}, [categoryBudgets, budgetData]);
|
||||
if (totalMonthlyBudget > 0) {
|
||||
const totalDailyBudget = Math.round(totalMonthlyBudget / 30);
|
||||
const totalWeeklyBudget = Math.round(totalMonthlyBudget / 4.3);
|
||||
|
||||
const updatedBudgetData = {
|
||||
daily: {
|
||||
targetAmount: totalDailyBudget,
|
||||
spentAmount: budgetData.daily.spentAmount,
|
||||
remainingAmount: totalDailyBudget - budgetData.daily.spentAmount
|
||||
},
|
||||
weekly: {
|
||||
targetAmount: totalWeeklyBudget,
|
||||
spentAmount: budgetData.weekly.spentAmount,
|
||||
remainingAmount: totalWeeklyBudget - budgetData.weekly.spentAmount
|
||||
},
|
||||
monthly: {
|
||||
targetAmount: totalMonthlyBudget,
|
||||
spentAmount: budgetData.monthly.spentAmount,
|
||||
remainingAmount: totalMonthlyBudget - budgetData.monthly.spentAmount
|
||||
}
|
||||
};
|
||||
|
||||
// 로컬 상태 업데이트
|
||||
handleBudgetGoalUpdate('monthly', totalMonthlyBudget);
|
||||
console.log('예산 데이터 자동 업데이트:', updatedBudgetData);
|
||||
}
|
||||
}, [categoryBudgets, handleBudgetGoalUpdate]);
|
||||
|
||||
// 모든 데이터 리셋 함수
|
||||
const resetBudgetData = useCallback(() => {
|
||||
@@ -80,11 +87,13 @@ export const useBudgetState = () => {
|
||||
}, [resetTransactions, resetCategoryBudgets, resetBudgetDataInternal]);
|
||||
|
||||
// 확장된 예산 목표 업데이트 함수
|
||||
const extendedBudgetGoalUpdate = (
|
||||
const extendedBudgetGoalUpdate = useCallback((
|
||||
type: BudgetPeriod,
|
||||
amount: number,
|
||||
newCategoryBudgets?: Record<string, number>
|
||||
) => {
|
||||
console.log(`확장된 예산 목표 업데이트: ${type}, 금액: ${amount}, 카테고리 예산:`, newCategoryBudgets);
|
||||
|
||||
// 카테고리 예산이 직접 업데이트된 경우
|
||||
if (newCategoryBudgets) {
|
||||
updateCategoryBudgets(newCategoryBudgets);
|
||||
@@ -111,7 +120,7 @@ export const useBudgetState = () => {
|
||||
// 일일이나 주간 예산이 직접 업데이트되는 경우
|
||||
handleBudgetGoalUpdate(type, amount);
|
||||
}
|
||||
};
|
||||
}, [budgetData, categoryBudgets, handleBudgetGoalUpdate, updateCategoryBudgets]);
|
||||
|
||||
return {
|
||||
transactions,
|
||||
|
||||
Reference in New Issue
Block a user