Fix type errors in BudgetContext
Addresses type errors related to category budgets in BudgetContext and Index page. Specifically, ensures correct type assignment for category budget updates.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
|
||||
import React, { createContext, useContext, useState, useEffect, ReactNode } from 'react';
|
||||
import { Transaction } from '@/components/TransactionCard';
|
||||
import { DEFAULT_CATEGORY_BUDGETS, DEFAULT_MONTHLY_BUDGET } from '@/constants/categoryIcons';
|
||||
import { DEFAULT_CATEGORY_BUDGETS, DEFAULT_MONTHLY_BUDGET, EXPENSE_CATEGORIES } from '@/constants/categoryIcons';
|
||||
import { toast } from '@/components/ui/use-toast';
|
||||
|
||||
// 예산 데이터 타입 정의
|
||||
@@ -44,7 +44,7 @@ const BudgetContext = createContext<BudgetContextType | undefined>(undefined);
|
||||
export const BudgetProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
|
||||
const [selectedTab, setSelectedTab] = useState<BudgetPeriod>("daily");
|
||||
const [transactions, setTransactions] = useState<Transaction[]>([]);
|
||||
const [categoryBudgets, setCategoryBudgets] = useState(DEFAULT_CATEGORY_BUDGETS);
|
||||
const [categoryBudgets, setCategoryBudgets] = useState<Record<string, number>>(DEFAULT_CATEGORY_BUDGETS);
|
||||
const [budgetData, setBudgetData] = useState<BudgetData>({
|
||||
daily: {
|
||||
targetAmount: Math.round(DEFAULT_MONTHLY_BUDGET / 30),
|
||||
@@ -195,9 +195,12 @@ export const BudgetProvider: React.FC<{ children: ReactNode }> = ({ children })
|
||||
// 카테고리별 지출 계산
|
||||
const getCategorySpending = () => {
|
||||
const expenseTransactions = transactions.filter(t => t.type === 'expense');
|
||||
const categorySpending: Record<string, number> = { ...Object.fromEntries(
|
||||
Object.keys(categoryBudgets).map(key => [key, 0])
|
||||
)};
|
||||
const categorySpending: Record<string, number> = {};
|
||||
|
||||
// 모든 카테고리에 대해 초기값 0 설정
|
||||
EXPENSE_CATEGORIES.forEach(category => {
|
||||
categorySpending[category] = 0;
|
||||
});
|
||||
|
||||
expenseTransactions.forEach(t => {
|
||||
if (t.category in categorySpending) {
|
||||
@@ -227,11 +230,10 @@ export const BudgetProvider: React.FC<{ children: ReactNode }> = ({ children })
|
||||
|
||||
// 월간 총 예산이 변경되면 카테고리별 예산도 비례하여 조정
|
||||
const ratio = amount / budgetData.monthly.targetAmount;
|
||||
const updatedCategoryBudgets = {...categoryBudgets};
|
||||
const updatedCategoryBudgets: Record<string, number> = {};
|
||||
|
||||
Object.keys(updatedCategoryBudgets).forEach(category => {
|
||||
updatedCategoryBudgets[category as keyof typeof categoryBudgets] =
|
||||
Math.round(categoryBudgets[category as keyof typeof categoryBudgets] * ratio);
|
||||
Object.keys(categoryBudgets).forEach(category => {
|
||||
updatedCategoryBudgets[category] = Math.round(categoryBudgets[category] * ratio);
|
||||
});
|
||||
|
||||
setCategoryBudgets(updatedCategoryBudgets);
|
||||
|
||||
Reference in New Issue
Block a user