Fix budget display issue

The budget display in the budget card was not showing correctly when the budget was not set. This commit fixes the issue.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-22 07:48:57 +00:00
parent 9e5406afff
commit f39733bfcd
3 changed files with 60 additions and 19 deletions

View File

@@ -32,10 +32,12 @@ const BudgetTabContent: React.FC<BudgetTabContentProps> = ({
// 실제 백분율 계산 (초과해도 실제 퍼센트로 표시)
const actualPercentage = targetAmount > 0 ? Math.round(spentAmount / targetAmount * 100) : 0;
const percentage = actualPercentage;
const isFirstBudget = targetAmount === 0;
// 예산이 설정되었는지 여부 확인 (0이면 미설정으로 간주)
const isBudgetSet = targetAmount > 0;
// 예산 초과 여부 계산
const isOverBudget = spentAmount > targetAmount;
const isOverBudget = spentAmount > targetAmount && targetAmount > 0;
// 예산이 얼마 남지 않은 경우 (10% 미만)
const isLowBudget = targetAmount > 0 && percentage >= 90 && percentage < 100;
@@ -108,11 +110,11 @@ const BudgetTabContent: React.FC<BudgetTabContentProps> = ({
};
// 예산 여부에 따른 텍스트 결정
const budgetButtonText = targetAmount > 0 ? "예산 수정하기" : "예산 입력하기";
const budgetButtonText = isBudgetSet ? "예산 수정하기" : "예산 입력하기";
return (
<div>
{targetAmount > 0 ? (
{isBudgetSet ? (
<>
<div className="flex justify-between items-center mb-3">
<div className="text-base font-bold">{formatCurrency(spentAmount)}</div>

View File

@@ -57,7 +57,7 @@ export const calculateCategorySpending = (
}));
};
// 예산 데이터 업데이트 계산 - 수정된 함수
// 예산 데이터 업데이트 계산 - 개선된 함수
export const calculateUpdatedBudgetData = (
prevBudgetData: BudgetData,
type: BudgetPeriod,
@@ -81,10 +81,15 @@ export const calculateUpdatedBudgetData = (
} else { // 'daily'
// 일일 예산이 직접 입력된 경우
dailyAmount = amount;
weeklyAmount = dailyAmount * 7;
monthlyAmount = dailyAmount * 30;
weeklyAmount = Math.round(dailyAmount * 7);
monthlyAmount = Math.round(dailyAmount * 30);
}
// 모든 금액이 최소한 0 이상이 되도록 보장
monthlyAmount = Math.max(0, monthlyAmount);
weeklyAmount = Math.max(0, weeklyAmount);
dailyAmount = Math.max(0, dailyAmount);
console.log(`최종 예산 계산: 월간=${monthlyAmount}원, 주간=${weeklyAmount}원, 일일=${dailyAmount}`);
return {
@@ -134,17 +139,17 @@ export const calculateSpentAmounts = (
// 예산 데이터 업데이트
return {
daily: {
...prevBudgetData.daily,
targetAmount: prevBudgetData.daily.targetAmount,
spentAmount: dailySpent,
remainingAmount: Math.max(0, prevBudgetData.daily.targetAmount - dailySpent)
},
weekly: {
...prevBudgetData.weekly,
targetAmount: prevBudgetData.weekly.targetAmount,
spentAmount: weeklySpent,
remainingAmount: Math.max(0, prevBudgetData.weekly.targetAmount - weeklySpent)
},
monthly: {
...prevBudgetData.monthly,
targetAmount: prevBudgetData.monthly.targetAmount,
spentAmount: monthlySpent,
remainingAmount: Math.max(0, prevBudgetData.monthly.targetAmount - monthlySpent)
}

View File

@@ -2,6 +2,7 @@
import { useCallback } from 'react';
import { BudgetData, BudgetPeriod } from '../types';
import { calculateUpdatedBudgetData } from '../budgetUtils';
import { toast } from '@/components/ui/use-toast';
// 확장된 예산 업데이트 로직을 제공하는 훅
export const useExtendedBudgetUpdate = (
@@ -20,19 +21,52 @@ export const useExtendedBudgetUpdate = (
// 카테고리 예산이 제공된 경우 업데이트
if (newCategoryBudgets) {
// 카테고리 예산 저장
updateCategoryBudgets(newCategoryBudgets);
try {
// 카테고리 예산 저장
updateCategoryBudgets(newCategoryBudgets);
// 총액 계산
const totalAmount = Object.values(newCategoryBudgets).reduce((sum, val) => sum + val, 0);
console.log('카테고리 총액:', totalAmount);
// 총액 계산 (0 확인)
const totalAmount = Object.values(newCategoryBudgets).reduce((sum, val) => sum + val, 0);
console.log('카테고리 총액:', totalAmount);
// 월간 예산 금액으로 예산 데이터 업데이트
// 월간 예산을 설정하면 자동으로 일간/주간 예산도 계산되도록 수정
handleBudgetGoalUpdate('monthly', 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]);