Investigate budget data issues
Investigate why budget and expense cards are not displaying correctly and why budget is showing as 0 on other pages.
This commit is contained in:
@@ -58,6 +58,7 @@ const BudgetTabContent: React.FC<BudgetTabContentProps> = ({
|
|||||||
// 카테고리 예산 저장
|
// 카테고리 예산 저장
|
||||||
const handleSaveCategoryBudgets = () => {
|
const handleSaveCategoryBudgets = () => {
|
||||||
const totalBudget = calculateTotalBudget();
|
const totalBudget = calculateTotalBudget();
|
||||||
|
console.log('카테고리 예산 저장 및 총 예산 설정:', totalBudget, categoryBudgets);
|
||||||
onSaveBudget(totalBudget, categoryBudgets);
|
onSaveBudget(totalBudget, categoryBudgets);
|
||||||
setShowBudgetInput(false);
|
setShowBudgetInput(false);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -119,8 +119,7 @@ export const useBudgetDataState = (transactions: any[]) => {
|
|||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
console.log(`예산 목표 업데이트: ${type}, 금액: ${amount}`);
|
console.log(`예산 목표 업데이트: ${type}, 금액: ${amount}`);
|
||||||
// 월간 예산 직접 업데이트 (카테고리 예산이 없는 경우)
|
// 예산 업데이트 (카테고리 예산이 있든 없든 무조건 실행)
|
||||||
if (!newCategoryBudgets) {
|
|
||||||
const updatedBudgetData = calculateUpdatedBudgetData(budgetData, type, amount);
|
const updatedBudgetData = calculateUpdatedBudgetData(budgetData, type, amount);
|
||||||
console.log('새 예산 데이터:', updatedBudgetData);
|
console.log('새 예산 데이터:', updatedBudgetData);
|
||||||
|
|
||||||
@@ -130,7 +129,6 @@ export const useBudgetDataState = (transactions: any[]) => {
|
|||||||
|
|
||||||
// 저장 시간 업데이트
|
// 저장 시간 업데이트
|
||||||
localStorage.setItem('lastBudgetSaveTime', new Date().toISOString());
|
localStorage.setItem('lastBudgetSaveTime', new Date().toISOString());
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('예산 목표 업데이트 중 오류:', error);
|
console.error('예산 목표 업데이트 중 오류:', error);
|
||||||
toast({
|
toast({
|
||||||
|
|||||||
@@ -1,70 +1,116 @@
|
|||||||
|
|
||||||
import { useCallback } from 'react';
|
import { useState, useEffect, useCallback } from 'react';
|
||||||
import { BudgetPeriod } from './types';
|
import { BudgetData, BudgetPeriod, Transaction } from './types';
|
||||||
import { useTransactionState } from './hooks/useTransactionState';
|
|
||||||
import { useCategoryBudgetState } from './hooks/useCategoryBudgetState';
|
|
||||||
import { useBudgetDataState } from './hooks/useBudgetDataState';
|
import { useBudgetDataState } from './hooks/useBudgetDataState';
|
||||||
import { useCategorySpending } from './hooks/useCategorySpending';
|
import { useCategoryBudgetState } from './hooks/useCategoryBudgetState';
|
||||||
import { useBudgetBackup } from './hooks/useBudgetBackup';
|
import { useTransactionState } from './hooks/useTransactionState';
|
||||||
import { useBudgetReset } from './hooks/useBudgetReset';
|
import { calculateCategorySpending } from './budgetUtils';
|
||||||
import { useExtendedBudgetUpdate } from './hooks/useExtendedBudgetUpdate';
|
import { toast } from '@/hooks/useToast.wrapper';
|
||||||
|
import { loadCategoryBudgetsFromStorage, saveCategoryBudgetsToStorage } from './storage';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 예산 상태 관리를 위한 메인 훅
|
||||||
|
*/
|
||||||
export const useBudgetState = () => {
|
export const useBudgetState = () => {
|
||||||
// 각 상태 관리 훅 사용
|
// 트랜잭션 상태 관리
|
||||||
const {
|
const {
|
||||||
transactions,
|
transactions,
|
||||||
addTransaction,
|
addTransaction,
|
||||||
updateTransaction,
|
updateTransaction,
|
||||||
deleteTransaction,
|
deleteTransaction
|
||||||
resetTransactions
|
|
||||||
} = useTransactionState();
|
} = useTransactionState();
|
||||||
|
|
||||||
|
// 예산 데이터 상태 관리
|
||||||
|
const {
|
||||||
|
budgetData,
|
||||||
|
selectedTab,
|
||||||
|
setSelectedTab,
|
||||||
|
handleBudgetGoalUpdate,
|
||||||
|
resetBudgetData
|
||||||
|
} = useBudgetDataState(transactions);
|
||||||
|
|
||||||
|
// 카테고리 예산 상태 관리
|
||||||
const {
|
const {
|
||||||
categoryBudgets,
|
categoryBudgets,
|
||||||
setCategoryBudgets,
|
|
||||||
updateCategoryBudgets,
|
updateCategoryBudgets,
|
||||||
resetCategoryBudgets
|
resetCategoryBudgets
|
||||||
} = useCategoryBudgetState();
|
} = useCategoryBudgetState();
|
||||||
|
|
||||||
const {
|
// 카테고리별 지출 계산
|
||||||
budgetData,
|
const getCategorySpending = useCallback(() => {
|
||||||
selectedTab,
|
return calculateCategorySpending(transactions, categoryBudgets);
|
||||||
setSelectedTab,
|
}, [transactions, categoryBudgets]);
|
||||||
handleBudgetGoalUpdate,
|
|
||||||
resetBudgetData: resetBudgetDataInternal
|
|
||||||
} = useBudgetDataState(transactions);
|
|
||||||
|
|
||||||
const { getCategorySpending } = useCategorySpending(transactions, categoryBudgets);
|
// 예산 목표 업데이트 함수 (기존 함수 래핑)
|
||||||
|
const handleBudgetUpdate = useCallback((
|
||||||
|
type: BudgetPeriod,
|
||||||
|
amount: number,
|
||||||
|
newCategoryBudgets?: Record<string, number>
|
||||||
|
) => {
|
||||||
|
console.log(`예산 업데이트 시작: ${type}, 금액: ${amount}, 카테고리 예산:`, newCategoryBudgets);
|
||||||
|
|
||||||
// 자동 백업 사용
|
try {
|
||||||
useBudgetBackup(budgetData, categoryBudgets, transactions);
|
// 카테고리 예산이 제공된 경우
|
||||||
|
if (newCategoryBudgets) {
|
||||||
|
console.log('카테고리 예산도 함께 업데이트:', newCategoryBudgets);
|
||||||
|
// 카테고리 예산 상태 업데이트
|
||||||
|
updateCategoryBudgets(newCategoryBudgets);
|
||||||
|
|
||||||
// 확장된 예산 업데이트 로직 사용
|
// 전체 예산 값도 함께 업데이트 (카테고리 합계와 일치하도록)
|
||||||
const { extendedBudgetGoalUpdate } = useExtendedBudgetUpdate(
|
console.log('전체 예산도 업데이트:', amount);
|
||||||
budgetData,
|
}
|
||||||
categoryBudgets,
|
|
||||||
handleBudgetGoalUpdate,
|
|
||||||
updateCategoryBudgets
|
|
||||||
);
|
|
||||||
|
|
||||||
// 리셋 로직 사용
|
// 예산 목표 업데이트 (카테고리 예산이 없는 경우에도 실행)
|
||||||
const { resetBudgetData } = useBudgetReset(
|
handleBudgetGoalUpdate(type, amount, newCategoryBudgets);
|
||||||
resetTransactions,
|
|
||||||
resetCategoryBudgets,
|
// 로컬 스토리지에 직접 저장 - 중복 저장이지만 안전을 위해 추가
|
||||||
resetBudgetDataInternal
|
if (newCategoryBudgets) {
|
||||||
);
|
saveCategoryBudgetsToStorage(newCategoryBudgets);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('예산 업데이트 완료');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('예산 업데이트 오류:', error);
|
||||||
|
toast({
|
||||||
|
title: "예산 업데이트 실패",
|
||||||
|
description: "예산 설정 중 오류가 발생했습니다.",
|
||||||
|
variant: "destructive"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [handleBudgetGoalUpdate, updateCategoryBudgets]);
|
||||||
|
|
||||||
|
// 모든 데이터 초기화
|
||||||
|
const resetAllData = useCallback(() => {
|
||||||
|
resetBudgetData?.();
|
||||||
|
resetCategoryBudgets();
|
||||||
|
}, [resetBudgetData, resetCategoryBudgets]);
|
||||||
|
|
||||||
|
// 상태 디버깅 (개발 시 유용)
|
||||||
|
useEffect(() => {
|
||||||
|
console.log('BudgetState 훅 - 현재 상태:');
|
||||||
|
console.log('- 예산 데이터:', budgetData);
|
||||||
|
console.log('- 카테고리 예산:', categoryBudgets);
|
||||||
|
console.log('- 트랜잭션 수:', transactions.length);
|
||||||
|
}, [budgetData, categoryBudgets, transactions.length]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
// 데이터
|
||||||
transactions,
|
transactions,
|
||||||
categoryBudgets,
|
|
||||||
budgetData,
|
budgetData,
|
||||||
|
categoryBudgets,
|
||||||
selectedTab,
|
selectedTab,
|
||||||
|
|
||||||
|
// 상태 변경 함수
|
||||||
setSelectedTab,
|
setSelectedTab,
|
||||||
addTransaction,
|
addTransaction,
|
||||||
updateTransaction,
|
updateTransaction,
|
||||||
deleteTransaction,
|
deleteTransaction,
|
||||||
handleBudgetGoalUpdate: extendedBudgetGoalUpdate,
|
handleBudgetGoalUpdate: handleBudgetUpdate, // 래핑된 함수 사용
|
||||||
|
|
||||||
|
// 도우미 함수
|
||||||
getCategorySpending,
|
getCategorySpending,
|
||||||
resetBudgetData
|
|
||||||
|
// 데이터 초기화
|
||||||
|
resetBudgetData: resetAllData
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user