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:
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user