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,5 +1,5 @@
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { BudgetData, BudgetPeriod, Transaction } from './types';
|
||||
import { toast } from '@/components/ui/use-toast';
|
||||
import {
|
||||
@@ -8,7 +8,10 @@ import {
|
||||
loadCategoryBudgetsFromStorage,
|
||||
saveCategoryBudgetsToStorage,
|
||||
loadBudgetDataFromStorage,
|
||||
saveBudgetDataToStorage
|
||||
saveBudgetDataToStorage,
|
||||
clearAllTransactions,
|
||||
clearAllCategoryBudgets,
|
||||
clearAllBudgetData
|
||||
} from './storageUtils';
|
||||
import {
|
||||
calculateCategorySpending,
|
||||
@@ -23,6 +26,23 @@ export const useBudgetState = () => {
|
||||
const [categoryBudgets, setCategoryBudgets] = useState<Record<string, number>>(loadCategoryBudgetsFromStorage());
|
||||
const [budgetData, setBudgetData] = useState<BudgetData>(loadBudgetDataFromStorage());
|
||||
|
||||
// 데이터 리셋 함수
|
||||
const resetBudgetData = useCallback(() => {
|
||||
console.log('BudgetContext에서 데이터 리셋 시작');
|
||||
|
||||
// 로컬 스토리지 초기화
|
||||
clearAllTransactions();
|
||||
clearAllCategoryBudgets();
|
||||
clearAllBudgetData();
|
||||
|
||||
// 메모리내 상태 초기화
|
||||
setTransactions([]);
|
||||
setCategoryBudgets(loadCategoryBudgetsFromStorage());
|
||||
setBudgetData(loadBudgetDataFromStorage());
|
||||
|
||||
console.log('BudgetContext에서 데이터 리셋 완료');
|
||||
}, []);
|
||||
|
||||
// 트랜잭션 로드
|
||||
useEffect(() => {
|
||||
const loadTransactions = () => {
|
||||
@@ -93,7 +113,7 @@ export const useBudgetState = () => {
|
||||
|
||||
// 월간 예산을 업데이트하고 일일, 주간도 자동 계산
|
||||
if (type === 'monthly') {
|
||||
const ratio = amount / budgetData.monthly.targetAmount;
|
||||
const ratio = amount / (budgetData.monthly.targetAmount || 1); // 0으로 나누기 방지
|
||||
const updatedCategoryBudgets: Record<string, number> = {};
|
||||
|
||||
Object.keys(categoryBudgets).forEach(category => {
|
||||
@@ -133,6 +153,7 @@ export const useBudgetState = () => {
|
||||
setSelectedTab,
|
||||
updateTransaction,
|
||||
handleBudgetGoalUpdate,
|
||||
getCategorySpending
|
||||
getCategorySpending,
|
||||
resetBudgetData // 새로 추가된 리셋 함수
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user