Refactor BudgetContext file
Refactor BudgetContext.tsx into smaller components and hooks to improve code readability and maintainability.
This commit is contained in:
144
src/contexts/budget/budgetUtils.ts
Normal file
144
src/contexts/budget/budgetUtils.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
|
||||
import { BudgetData, BudgetPeriod, CategoryBudget, Transaction } from './types';
|
||||
import { EXPENSE_CATEGORIES } from '@/constants/categoryIcons';
|
||||
|
||||
// 기본 데이터 상수
|
||||
export const DEFAULT_CATEGORY_BUDGETS: Record<string, number> = {
|
||||
식비: 400000,
|
||||
생활비: 600000,
|
||||
교통비: 200000
|
||||
};
|
||||
|
||||
export const DEFAULT_MONTHLY_BUDGET = 1200000;
|
||||
|
||||
// 카테고리별 지출 계산
|
||||
export const calculateCategorySpending = (
|
||||
transactions: Transaction[],
|
||||
categoryBudgets: Record<string, number>
|
||||
): CategoryBudget[] => {
|
||||
const expenseTransactions = transactions.filter(t => t.type === 'expense');
|
||||
const categorySpending: Record<string, number> = {};
|
||||
|
||||
// 모든 카테고리에 대해 초기값 0 설정
|
||||
Object.keys(categoryBudgets).forEach(category => {
|
||||
categorySpending[category] = 0;
|
||||
});
|
||||
|
||||
expenseTransactions.forEach(t => {
|
||||
if (t.category in categorySpending) {
|
||||
categorySpending[t.category] += t.amount;
|
||||
}
|
||||
});
|
||||
|
||||
return Object.keys(categoryBudgets).map(category => ({
|
||||
title: category,
|
||||
current: categorySpending[category] || 0,
|
||||
total: categoryBudgets[category]
|
||||
}));
|
||||
};
|
||||
|
||||
// 예산 데이터 업데이트 계산
|
||||
export const calculateUpdatedBudgetData = (
|
||||
prevBudgetData: BudgetData,
|
||||
type: BudgetPeriod,
|
||||
amount: number
|
||||
): BudgetData => {
|
||||
if (type === 'monthly') {
|
||||
const dailyAmount = Math.round(amount / 30);
|
||||
const weeklyAmount = Math.round(amount / 4.3);
|
||||
|
||||
return {
|
||||
daily: {
|
||||
targetAmount: dailyAmount,
|
||||
spentAmount: prevBudgetData.daily.spentAmount,
|
||||
remainingAmount: Math.max(0, dailyAmount - prevBudgetData.daily.spentAmount)
|
||||
},
|
||||
weekly: {
|
||||
targetAmount: weeklyAmount,
|
||||
spentAmount: prevBudgetData.weekly.spentAmount,
|
||||
remainingAmount: Math.max(0, weeklyAmount - prevBudgetData.weekly.spentAmount)
|
||||
},
|
||||
monthly: {
|
||||
targetAmount: amount,
|
||||
spentAmount: prevBudgetData.monthly.spentAmount,
|
||||
remainingAmount: Math.max(0, amount - prevBudgetData.monthly.spentAmount)
|
||||
}
|
||||
};
|
||||
} else {
|
||||
const remainingAmount = Math.max(0, amount - prevBudgetData[type].spentAmount);
|
||||
return {
|
||||
...prevBudgetData,
|
||||
[type]: {
|
||||
...prevBudgetData[type],
|
||||
targetAmount: amount,
|
||||
remainingAmount: remainingAmount
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// 지출액 계산 (일일, 주간, 월간)
|
||||
export const calculateSpentAmounts = (
|
||||
transactions: Transaction[],
|
||||
prevBudgetData: BudgetData
|
||||
): BudgetData => {
|
||||
// 지출 거래 필터링
|
||||
const expenseTransactions = transactions.filter(t => t.type === 'expense');
|
||||
|
||||
// 오늘 지출 계산
|
||||
const todayExpenses = expenseTransactions.filter(t => {
|
||||
if (t.date.includes('오늘')) return true;
|
||||
return false;
|
||||
});
|
||||
const dailySpent = todayExpenses.reduce((sum, t) => sum + t.amount, 0);
|
||||
|
||||
// 이번 주 지출 계산 (단순화된 버전)
|
||||
const weeklyExpenses = expenseTransactions.filter(t => {
|
||||
if (t.date.includes('오늘') || t.date.includes('어제')) return true;
|
||||
return true;
|
||||
});
|
||||
const weeklySpent = weeklyExpenses.reduce((sum, t) => sum + t.amount, 0);
|
||||
|
||||
// 이번 달 총 지출 계산
|
||||
const monthlySpent = expenseTransactions.reduce((sum, t) => sum + t.amount, 0);
|
||||
|
||||
// 예산 데이터 업데이트
|
||||
return {
|
||||
daily: {
|
||||
...prevBudgetData.daily,
|
||||
spentAmount: dailySpent,
|
||||
remainingAmount: Math.max(0, prevBudgetData.daily.targetAmount - dailySpent)
|
||||
},
|
||||
weekly: {
|
||||
...prevBudgetData.weekly,
|
||||
spentAmount: weeklySpent,
|
||||
remainingAmount: Math.max(0, prevBudgetData.weekly.targetAmount - weeklySpent)
|
||||
},
|
||||
monthly: {
|
||||
...prevBudgetData.monthly,
|
||||
spentAmount: monthlySpent,
|
||||
remainingAmount: Math.max(0, prevBudgetData.monthly.targetAmount - monthlySpent)
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// 초기 예산 데이터 생성
|
||||
export const getInitialBudgetData = (): BudgetData => {
|
||||
return {
|
||||
daily: {
|
||||
targetAmount: Math.round(DEFAULT_MONTHLY_BUDGET / 30),
|
||||
spentAmount: 0,
|
||||
remainingAmount: Math.round(DEFAULT_MONTHLY_BUDGET / 30)
|
||||
},
|
||||
weekly: {
|
||||
targetAmount: Math.round(DEFAULT_MONTHLY_BUDGET / 4.3),
|
||||
spentAmount: 0,
|
||||
remainingAmount: Math.round(DEFAULT_MONTHLY_BUDGET / 4.3)
|
||||
},
|
||||
monthly: {
|
||||
targetAmount: DEFAULT_MONTHLY_BUDGET,
|
||||
spentAmount: 0,
|
||||
remainingAmount: DEFAULT_MONTHLY_BUDGET
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user