Addresses problems with budget display and data reset: - Ensures budget data is correctly displayed after initialization. - Fixes issue where daily and weekly budget data were missing. - Corrects data reset to properly clear transaction data.
94 lines
3.5 KiB
TypeScript
94 lines
3.5 KiB
TypeScript
|
|
import { useCallback } from 'react';
|
|
import { BudgetData, BudgetPeriod } from '../types';
|
|
import { calculateUpdatedBudgetData } from '../budgetUtils';
|
|
import { toast } from '@/components/ui/use-toast';
|
|
|
|
// 확장된 예산 업데이트 로직을 제공하는 훅
|
|
export const useExtendedBudgetUpdate = (
|
|
budgetData: BudgetData,
|
|
categoryBudgets: Record<string, number>,
|
|
handleBudgetGoalUpdate: (type: BudgetPeriod, amount: number) => void,
|
|
updateCategoryBudgets: (budgets: Record<string, number>) => void
|
|
) => {
|
|
// 확장된 예산 업데이트 로직
|
|
const extendedBudgetGoalUpdate = useCallback((
|
|
type: BudgetPeriod,
|
|
amount: number,
|
|
newCategoryBudgets?: Record<string, number>
|
|
) => {
|
|
console.log(`확장된 예산 목표 업데이트: ${type}, 금액: ${amount}`, newCategoryBudgets);
|
|
|
|
// 카테고리 예산이 제공된 경우 업데이트
|
|
if (newCategoryBudgets) {
|
|
try {
|
|
// 교통비 값이 있으면 교통으로 통합
|
|
if (newCategoryBudgets['교통비'] && !newCategoryBudgets['교통']) {
|
|
newCategoryBudgets['교통'] = newCategoryBudgets['교통비'];
|
|
delete newCategoryBudgets['교통비'];
|
|
}
|
|
|
|
// 식비 값이 있으면 음식으로 통합
|
|
if (newCategoryBudgets['식비'] && !newCategoryBudgets['음식']) {
|
|
newCategoryBudgets['음식'] = newCategoryBudgets['식비'];
|
|
delete newCategoryBudgets['식비'];
|
|
}
|
|
|
|
// 생활비 값이 있으면 쇼핑으로 통합
|
|
if (newCategoryBudgets['생활비'] && !newCategoryBudgets['쇼핑']) {
|
|
newCategoryBudgets['쇼핑'] = newCategoryBudgets['생활비'];
|
|
delete newCategoryBudgets['생활비'];
|
|
}
|
|
|
|
// 카테고리 예산 저장
|
|
updateCategoryBudgets(newCategoryBudgets);
|
|
|
|
// 총액 계산 (0 확인)
|
|
const totalAmount = Object.values(newCategoryBudgets).reduce((sum, val) => sum + val, 0);
|
|
console.log('카테고리 총액:', totalAmount);
|
|
|
|
if (totalAmount <= 0) {
|
|
toast({
|
|
title: "예산 설정 오류",
|
|
description: "유효한 예산 금액을 입력해주세요.",
|
|
variant: "destructive"
|
|
});
|
|
return;
|
|
}
|
|
|
|
// 월간 예산 금액으로 예산 데이터 업데이트
|
|
// 월간 예산을 설정하면 자동으로 일간/주간 예산도 계산됨
|
|
handleBudgetGoalUpdate('monthly', totalAmount);
|
|
|
|
// 성공 토스트 표시
|
|
toast({
|
|
title: "카테고리 예산 설정 완료",
|
|
description: `월간 총 예산이 ${totalAmount.toLocaleString()}원으로 설정되었습니다.`
|
|
});
|
|
} catch (error) {
|
|
console.error('카테고리 예산 업데이트 오류:', error);
|
|
toast({
|
|
title: "예산 설정 오류",
|
|
description: "카테고리 예산을 업데이트하는 중 오류가 발생했습니다.",
|
|
variant: "destructive"
|
|
});
|
|
}
|
|
} else {
|
|
// 카테고리 예산이 없는 경우, 선택된 기간 유형에 맞게 예산 설정
|
|
// 이 경우에도 다른 기간의 예산이 자동으로 계산됨
|
|
if (amount <= 0) {
|
|
toast({
|
|
title: "예산 설정 오류",
|
|
description: "유효한 예산 금액을 입력해주세요.",
|
|
variant: "destructive"
|
|
});
|
|
return;
|
|
}
|
|
|
|
handleBudgetGoalUpdate(type, amount);
|
|
}
|
|
}, [categoryBudgets, handleBudgetGoalUpdate, updateCategoryBudgets]);
|
|
|
|
return { extendedBudgetGoalUpdate };
|
|
};
|