Files
zellyy-finance/src/contexts/budget/hooks/useExtendedBudgetUpdate.ts
gpt-engineer-app[bot] 4f552632b7 Enforce monthly budget updates
The budget update logic was modified to ensure that all budget updates are applied to the monthly budget, regardless of the selected tab. This resolves inconsistencies in budget calculations and data storage.
2025-03-22 11:27:35 +00:00

49 lines
1.7 KiB
TypeScript

import { BudgetPeriod } from '../types';
/**
* 예산 목표 업데이트 확장 함수를 제공하는 훅
* 예산 목표 업데이트와 카테고리 예산 업데이트를 통합합니다.
*/
export const useExtendedBudgetUpdate = (
handleBudgetUpdate: (type: BudgetPeriod, amount: number) => void,
setCategoryBudgets: (budgets: Record<string, number>) => void
) => {
// 예산 목표 업데이트 확장 함수
const extendedBudgetUpdate = (
type: BudgetPeriod,
amount: number,
newCategoryBudgets?: Record<string, number>
) => {
console.log('확장 예산 업데이트 시작:', type, amount, newCategoryBudgets);
// 주간 예산인 경우 월간 예산으로 변환 (주간 값이 그대로 월간 값으로 설정되도록)
if (type === 'weekly') {
console.log(`주간 예산(${amount})을 월간 예산으로 직접 변환`);
type = 'monthly';
}
// 예산 업데이트
console.log(`예산 업데이트: 타입=${type}, 금액=${amount}`);
handleBudgetUpdate(type, amount);
// 카테고리 예산 업데이트 (제공된 경우)
if (newCategoryBudgets) {
console.log('카테고리 예산 업데이트:', newCategoryBudgets);
setCategoryBudgets(newCategoryBudgets);
// 카테고리 예산 업데이트 이벤트 발생
setTimeout(() => {
window.dispatchEvent(new Event('categoryBudgetsUpdated'));
}, 100);
}
// 모든 업데이트가 완료된 후 전역 예산 데이터 업데이트 이벤트 발생
setTimeout(() => {
window.dispatchEvent(new Event('budgetDataUpdated'));
}, 200);
};
return extendedBudgetUpdate;
};