diff --git a/src/components/BudgetTabContent.tsx b/src/components/BudgetTabContent.tsx index 7dc7ff3..0cb4642 100644 --- a/src/components/BudgetTabContent.tsx +++ b/src/components/BudgetTabContent.tsx @@ -32,10 +32,12 @@ const BudgetTabContent: React.FC = ({ // 실제 백분율 계산 (초과해도 실제 퍼센트로 표시) 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 = ({ }; // 예산 여부에 따른 텍스트 결정 - const budgetButtonText = targetAmount > 0 ? "예산 수정하기" : "예산 입력하기"; + const budgetButtonText = isBudgetSet ? "예산 수정하기" : "예산 입력하기"; return (
- {targetAmount > 0 ? ( + {isBudgetSet ? ( <>
{formatCurrency(spentAmount)}
diff --git a/src/contexts/budget/budgetUtils.ts b/src/contexts/budget/budgetUtils.ts index 52ad4e4..74d7d68 100644 --- a/src/contexts/budget/budgetUtils.ts +++ b/src/contexts/budget/budgetUtils.ts @@ -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) } diff --git a/src/contexts/budget/hooks/useExtendedBudgetUpdate.ts b/src/contexts/budget/hooks/useExtendedBudgetUpdate.ts index 352e0c0..4a24056 100644 --- a/src/contexts/budget/hooks/useExtendedBudgetUpdate.ts +++ b/src/contexts/budget/hooks/useExtendedBudgetUpdate.ts @@ -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); - - // 총액 계산 - const totalAmount = Object.values(newCategoryBudgets).reduce((sum, val) => sum + val, 0); - console.log('카테고리 총액:', totalAmount); + try { + // 카테고리 예산 저장 + updateCategoryBudgets(newCategoryBudgets); + + // 총액 계산 (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]);