Fix data persistence issue
Addresses a problem where budget and expense data was not being saved correctly.
This commit is contained in:
@@ -61,9 +61,21 @@ export const useBudgetState = () => {
|
||||
|
||||
// 지출 계산 및 업데이트
|
||||
useEffect(() => {
|
||||
const updatedBudgetData = calculateSpentAmounts(transactions, budgetData);
|
||||
// 스토리지에서 데이터 로드
|
||||
const loadedBudgetData = loadBudgetDataFromStorage();
|
||||
|
||||
// 지출 금액 업데이트
|
||||
const updatedBudgetData = calculateSpentAmounts(transactions, loadedBudgetData);
|
||||
|
||||
// 상태 및 스토리지 모두 업데이트
|
||||
setBudgetData(updatedBudgetData);
|
||||
saveBudgetDataToStorage(updatedBudgetData);
|
||||
|
||||
// 트랜잭션 변경 내용도 저장
|
||||
saveTransactionsToStorage(transactions);
|
||||
|
||||
// 로컬 이벤트 발생 (다른 컴포넌트에서 변경 감지하도록)
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
}, [transactions]);
|
||||
|
||||
// 카테고리별 예산 및 지출 계산
|
||||
@@ -91,11 +103,27 @@ export const useBudgetState = () => {
|
||||
}
|
||||
};
|
||||
|
||||
saveBudgetDataToStorage(updatedBudgetData);
|
||||
// 저장 과정 강화 - 예산 데이터 저장
|
||||
try {
|
||||
saveBudgetDataToStorage(updatedBudgetData);
|
||||
console.log('예산 데이터가 저장되었습니다:', updatedBudgetData);
|
||||
} catch (error) {
|
||||
console.error('예산 데이터 저장 중 오류:', error);
|
||||
}
|
||||
|
||||
return updatedBudgetData;
|
||||
});
|
||||
|
||||
saveCategoryBudgetsToStorage(categoryBudgets);
|
||||
// 저장 과정 강화 - 카테고리 예산 저장
|
||||
try {
|
||||
saveCategoryBudgetsToStorage(categoryBudgets);
|
||||
console.log('카테고리 예산이 저장되었습니다:', categoryBudgets);
|
||||
} catch (error) {
|
||||
console.error('카테고리 예산 저장 중 오류:', error);
|
||||
}
|
||||
|
||||
// 로컬 이벤트 발생 (다른 컴포넌트에서 변경 감지하도록)
|
||||
window.dispatchEvent(new Event('categoryBudgetsUpdated'));
|
||||
}, [categoryBudgets]);
|
||||
|
||||
// 카테고리별 지출 계산
|
||||
@@ -108,6 +136,15 @@ export const useBudgetState = () => {
|
||||
// 카테고리 예산이 직접 업데이트된 경우
|
||||
if (newCategoryBudgets) {
|
||||
setCategoryBudgets(newCategoryBudgets);
|
||||
|
||||
// 저장 과정 추가
|
||||
saveCategoryBudgetsToStorage(newCategoryBudgets);
|
||||
|
||||
toast({
|
||||
title: "카테고리 예산 업데이트 완료",
|
||||
description: "카테고리별 예산이 저장되었습니다."
|
||||
});
|
||||
|
||||
return; // 카테고리 예산이 변경되면 useEffect에서 자동으로 budgetData가 업데이트됩니다
|
||||
}
|
||||
|
||||
@@ -121,6 +158,7 @@ export const useBudgetState = () => {
|
||||
});
|
||||
|
||||
setCategoryBudgets(updatedCategoryBudgets);
|
||||
saveCategoryBudgetsToStorage(updatedCategoryBudgets);
|
||||
} else {
|
||||
// 일일이나 주간 예산이 직접 업데이트되는 경우
|
||||
const updatedBudgetData = calculateUpdatedBudgetData(budgetData, type, amount);
|
||||
@@ -134,6 +172,15 @@ export const useBudgetState = () => {
|
||||
});
|
||||
};
|
||||
|
||||
// 트랜잭션 추가 함수 추가
|
||||
const addTransaction = (newTransaction: Transaction) => {
|
||||
setTransactions(prev => {
|
||||
const updated = [newTransaction, ...prev];
|
||||
saveTransactionsToStorage(updated);
|
||||
return updated;
|
||||
});
|
||||
};
|
||||
|
||||
// 트랜잭션 업데이트 처리
|
||||
const updateTransaction = (updatedTransaction: Transaction) => {
|
||||
setTransactions(prev => {
|
||||
@@ -145,15 +192,26 @@ export const useBudgetState = () => {
|
||||
});
|
||||
};
|
||||
|
||||
// 트랜잭션 삭제 함수 추가
|
||||
const deleteTransaction = (transactionId: string) => {
|
||||
setTransactions(prev => {
|
||||
const updated = prev.filter(transaction => transaction.id !== transactionId);
|
||||
saveTransactionsToStorage(updated);
|
||||
return updated;
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
transactions,
|
||||
categoryBudgets,
|
||||
budgetData,
|
||||
selectedTab,
|
||||
setSelectedTab,
|
||||
addTransaction,
|
||||
updateTransaction,
|
||||
deleteTransaction,
|
||||
handleBudgetGoalUpdate,
|
||||
getCategorySpending,
|
||||
resetBudgetData // 새로 추가된 리셋 함수
|
||||
resetBudgetData
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user