Fix data initialization issue

The data initialization logic was not properly clearing existing data, leading to incorrect budget values. This commit ensures that all relevant data is cleared upon initialization.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-15 23:08:06 +00:00
parent f8abebcac6
commit d798632d05
5 changed files with 191 additions and 40 deletions

View File

@@ -1,13 +1,30 @@
import React, { createContext, useContext, ReactNode } from 'react';
import { BudgetContextType } from './types';
import React, { createContext, useContext } from 'react';
import { useBudgetState } from './useBudgetState';
import { BudgetData, BudgetPeriod, Transaction } from './types';
// Context 생성
// 컨텍스트 인터페이스 정의
interface BudgetContextType {
transactions: Transaction[];
selectedTab: BudgetPeriod;
setSelectedTab: (tab: BudgetPeriod) => void;
budgetData: BudgetData;
categoryBudgets: Record<string, number>;
getCategorySpending: () => Array<{
title: string;
current: number;
total: number;
}>;
updateTransaction: (transaction: Transaction) => void;
handleBudgetGoalUpdate: (type: BudgetPeriod, amount: number, newCategoryBudgets?: Record<string, number>) => void;
resetBudgetData?: () => void; // 선택적 필드로 추가
}
// 컨텍스트 생성
const BudgetContext = createContext<BudgetContextType | undefined>(undefined);
// Context Provider 컴포넌트
export const BudgetProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
// 컨텍스트 프로바이더 컴포넌트
export const BudgetProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const budgetState = useBudgetState();
return (
@@ -17,11 +34,13 @@ export const BudgetProvider: React.FC<{ children: ReactNode }> = ({ children })
);
};
// Context 사용Hook
export const useBudget = () => {
// 컨텍스트 접근
export const useBudget = (): BudgetContextType => {
const context = useContext(BudgetContext);
if (!context) {
if (context === undefined) {
throw new Error('useBudget must be used within a BudgetProvider');
}
return context;
};
export type { BudgetPeriod } from './types';