144 lines
5.0 KiB
TypeScript
144 lines
5.0 KiB
TypeScript
|
|
import { useCallback, useEffect } from 'react';
|
|
import { BudgetPeriod } from './types';
|
|
import { toast } from '@/components/ui/use-toast';
|
|
import { useTransactionState } from './hooks/useTransactionState';
|
|
import { useCategoryBudgetState } from './hooks/useCategoryBudgetState';
|
|
import { useBudgetDataState } from './hooks/useBudgetDataState';
|
|
import { useCategorySpending } from './hooks/useCategorySpending';
|
|
|
|
export const useBudgetState = () => {
|
|
// 각 상태 관리 훅 사용
|
|
const {
|
|
transactions,
|
|
addTransaction,
|
|
updateTransaction,
|
|
deleteTransaction,
|
|
resetTransactions
|
|
} = useTransactionState();
|
|
|
|
const {
|
|
categoryBudgets,
|
|
setCategoryBudgets,
|
|
updateCategoryBudgets,
|
|
resetCategoryBudgets
|
|
} = useCategoryBudgetState();
|
|
|
|
const {
|
|
budgetData,
|
|
selectedTab,
|
|
setSelectedTab,
|
|
handleBudgetGoalUpdate,
|
|
resetBudgetData: resetBudgetDataInternal
|
|
} = useBudgetDataState(transactions);
|
|
|
|
const { getCategorySpending } = useCategorySpending(transactions, categoryBudgets);
|
|
|
|
// 디버깅을 위한 로그
|
|
useEffect(() => {
|
|
console.log('BudgetState 훅 - 현재 상태:');
|
|
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);
|
|
console.log('카테고리 예산 합계:', totalMonthlyBudget);
|
|
|
|
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, budgetData]);
|
|
|
|
// 모든 데이터 리셋 함수
|
|
const resetBudgetData = useCallback(() => {
|
|
console.log('BudgetContext에서 데이터 리셋 시작');
|
|
|
|
// 로컬 스토리지 초기화
|
|
resetTransactions();
|
|
resetCategoryBudgets();
|
|
resetBudgetDataInternal();
|
|
|
|
console.log('BudgetContext에서 데이터 리셋 완료');
|
|
}, [resetTransactions, resetCategoryBudgets, resetBudgetDataInternal]);
|
|
|
|
// 확장된 예산 목표 업데이트 함수
|
|
const extendedBudgetGoalUpdate = useCallback((
|
|
type: BudgetPeriod,
|
|
amount: number,
|
|
newCategoryBudgets?: Record<string, number>
|
|
) => {
|
|
console.log(`확장된 예산 목표 업데이트 호출: ${type}, 금액: ${amount}`);
|
|
|
|
// 카테고리 예산이 직접 업데이트된 경우
|
|
if (newCategoryBudgets) {
|
|
console.log('카테고리 예산 직접 업데이트:', newCategoryBudgets);
|
|
updateCategoryBudgets(newCategoryBudgets);
|
|
|
|
toast({
|
|
title: "카테고리 예산 업데이트 완료",
|
|
description: "카테고리별 예산이 저장되었습니다."
|
|
});
|
|
|
|
return; // 카테고리 예산이 변경되면 useEffect에서 자동으로 budgetData가 업데이트됩니다
|
|
}
|
|
|
|
// 월간 예산을 업데이트하고 일일, 주간도 자동 계산
|
|
if (type === 'monthly') {
|
|
console.log('월간 예산 업데이트:', amount);
|
|
const ratio = amount / (budgetData.monthly.targetAmount || 1); // 0으로 나누기 방지
|
|
const updatedCategoryBudgets: Record<string, number> = {};
|
|
|
|
Object.keys(categoryBudgets).forEach(category => {
|
|
updatedCategoryBudgets[category] = Math.round(categoryBudgets[category] * ratio);
|
|
});
|
|
|
|
console.log('업데이트된 카테고리 예산:', updatedCategoryBudgets);
|
|
updateCategoryBudgets(updatedCategoryBudgets);
|
|
} else {
|
|
// 일일이나 주간 예산이 직접 업데이트되는 경우
|
|
console.log(`${type} 예산 직접 업데이트:`, amount);
|
|
handleBudgetGoalUpdate(type, amount);
|
|
}
|
|
}, [budgetData, categoryBudgets, handleBudgetGoalUpdate, updateCategoryBudgets]);
|
|
|
|
return {
|
|
transactions,
|
|
categoryBudgets,
|
|
budgetData,
|
|
selectedTab,
|
|
setSelectedTab,
|
|
addTransaction,
|
|
updateTransaction,
|
|
deleteTransaction,
|
|
handleBudgetGoalUpdate: extendedBudgetGoalUpdate,
|
|
getCategorySpending,
|
|
resetBudgetData
|
|
};
|
|
};
|