Fix budget display issues
The budget was not being displayed correctly in the spending and analytics screens, as well as the weekly and monthly views on the home screen. This commit addresses these issues.
This commit is contained in:
@@ -1,47 +1,42 @@
|
||||
|
||||
import { useCallback } from 'react';
|
||||
import { BudgetData, BudgetPeriod } from '../types';
|
||||
import { calculateUpdatedBudgetData } from '../budgetUtils';
|
||||
|
||||
/**
|
||||
* 확장된 예산 업데이트 기능을 제공하는 훅
|
||||
*/
|
||||
// 확장된 예산 업데이트 로직을 제공하는 훅
|
||||
export const useExtendedBudgetUpdate = (
|
||||
budgetData: BudgetData,
|
||||
categoryBudgets: Record<string, number>,
|
||||
handleBudgetGoalUpdate: (type: BudgetPeriod, amount: number, newCategoryBudgets?: Record<string, number>) => void,
|
||||
updateCategoryBudgets: (newCategoryBudgets: Record<string, number>) => void
|
||||
handleBudgetGoalUpdate: (type: BudgetPeriod, amount: number) => void,
|
||||
updateCategoryBudgets: (budgets: Record<string, number>) => void
|
||||
) => {
|
||||
/**
|
||||
* 확장된 예산 업데이트 함수
|
||||
* 월간 예산 및 카테고리 예산을 함께 업데이트
|
||||
*/
|
||||
// 확장된 예산 업데이트 로직
|
||||
const extendedBudgetGoalUpdate = useCallback((
|
||||
type: BudgetPeriod,
|
||||
amount: number,
|
||||
type: BudgetPeriod,
|
||||
amount: number,
|
||||
newCategoryBudgets?: Record<string, number>
|
||||
) => {
|
||||
console.log(`확장된 예산 업데이트: 타입=${type}, 금액=${amount}, 카테고리 예산 포함=${!!newCategoryBudgets}`);
|
||||
console.log(`확장된 예산 목표 업데이트: ${type}, 금액: ${amount}`, newCategoryBudgets);
|
||||
|
||||
// 기본 예산 업데이트
|
||||
handleBudgetGoalUpdate(type, amount);
|
||||
|
||||
// 카테고리 예산도 함께 업데이트 (있는 경우)
|
||||
// 카테고리 예산이 제공된 경우 업데이트
|
||||
if (newCategoryBudgets) {
|
||||
console.log('카테고리 예산 업데이트:', newCategoryBudgets);
|
||||
updateCategoryBudgets(newCategoryBudgets);
|
||||
|
||||
// 총액 계산
|
||||
const totalAmount = Object.values(newCategoryBudgets).reduce((sum, val) => sum + val, 0);
|
||||
console.log('카테고리 총액:', totalAmount);
|
||||
|
||||
// 일/주/월 모든 예산 업데이트를 위해 monthly로 처리
|
||||
// (monthly 타입은 모든 예산을 계산해 줌)
|
||||
const updatedBudgetData = calculateUpdatedBudgetData(budgetData, 'monthly', totalAmount);
|
||||
|
||||
// 각 기간별 예산 업데이트
|
||||
handleBudgetGoalUpdate('monthly', updatedBudgetData.monthly.targetAmount);
|
||||
} else {
|
||||
// 카테고리 예산이 없는 경우, 기존 로직 사용
|
||||
handleBudgetGoalUpdate(type, amount);
|
||||
}
|
||||
|
||||
// 데이터 저장 시간 기록 - 동일 세션의 다른 컴포넌트에서 변경 감지 용도
|
||||
localStorage.setItem('lastBudgetUpdateTime', new Date().toISOString());
|
||||
|
||||
// 이벤트 발생
|
||||
try {
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
window.dispatchEvent(new Event('categoryBudgetsUpdated'));
|
||||
} catch (error) {
|
||||
console.error('이벤트 발생 오류:', error);
|
||||
}
|
||||
}, [handleBudgetGoalUpdate, updateCategoryBudgets]);
|
||||
|
||||
}, [budgetData, categoryBudgets, handleBudgetGoalUpdate, updateCategoryBudgets]);
|
||||
|
||||
return { extendedBudgetGoalUpdate };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user