Fix data persistence issue
Addresses a bug where budget and expense data were not persisting correctly, leading to data loss when navigating between pages.
This commit is contained in:
@@ -5,7 +5,7 @@ import {
|
||||
loadTransactionsFromStorage,
|
||||
saveTransactionsToStorage,
|
||||
clearAllTransactions
|
||||
} from '../storageUtils';
|
||||
} from '../storage';
|
||||
import { toast } from '@/components/ui/use-toast';
|
||||
|
||||
// 트랜잭션 상태 관리 훅
|
||||
@@ -22,9 +22,17 @@ export const useTransactionState = () => {
|
||||
loadTransactions();
|
||||
|
||||
// 이벤트 리스너를 추가하여 다른 컴포넌트에서 변경 시 업데이트
|
||||
window.addEventListener('storage', loadTransactions);
|
||||
const handleTransactionUpdate = () => loadTransactions();
|
||||
window.addEventListener('transactionUpdated', handleTransactionUpdate);
|
||||
window.addEventListener('transactionDeleted', handleTransactionUpdate);
|
||||
window.addEventListener('transactionAdded', handleTransactionUpdate);
|
||||
window.addEventListener('storage', handleTransactionUpdate);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('storage', loadTransactions);
|
||||
window.removeEventListener('transactionUpdated', handleTransactionUpdate);
|
||||
window.removeEventListener('transactionDeleted', handleTransactionUpdate);
|
||||
window.removeEventListener('transactionAdded', handleTransactionUpdate);
|
||||
window.removeEventListener('storage', handleTransactionUpdate);
|
||||
};
|
||||
}, []);
|
||||
|
||||
@@ -33,6 +41,10 @@ export const useTransactionState = () => {
|
||||
setTransactions(prev => {
|
||||
const updated = [newTransaction, ...prev];
|
||||
saveTransactionsToStorage(updated);
|
||||
|
||||
// 이벤트 발생시키기
|
||||
window.dispatchEvent(new Event('transactionAdded'));
|
||||
|
||||
return updated;
|
||||
});
|
||||
}, []);
|
||||
@@ -44,11 +56,12 @@ export const useTransactionState = () => {
|
||||
transaction.id === updatedTransaction.id ? updatedTransaction : transaction
|
||||
);
|
||||
saveTransactionsToStorage(updated);
|
||||
|
||||
// 이벤트 발생시키기
|
||||
window.dispatchEvent(new Event('transactionUpdated'));
|
||||
|
||||
return updated;
|
||||
});
|
||||
|
||||
// 로컬 이벤트 발생 (다른 컴포넌트에서 변경 감지하도록)
|
||||
window.dispatchEvent(new Event('transactionUpdated'));
|
||||
}, []);
|
||||
|
||||
// 트랜잭션 삭제 함수
|
||||
@@ -56,16 +69,17 @@ export const useTransactionState = () => {
|
||||
setTransactions(prev => {
|
||||
const updated = prev.filter(transaction => transaction.id !== transactionId);
|
||||
saveTransactionsToStorage(updated);
|
||||
|
||||
// 이벤트 발생시키기
|
||||
window.dispatchEvent(new Event('transactionDeleted'));
|
||||
|
||||
toast({
|
||||
title: "지출이 삭제되었습니다",
|
||||
description: "지출 항목이 성공적으로 삭제되었습니다.",
|
||||
});
|
||||
|
||||
return updated;
|
||||
});
|
||||
|
||||
toast({
|
||||
title: "지출이 삭제되었습니다",
|
||||
description: "지출 항목이 성공적으로 삭제되었습니다.",
|
||||
});
|
||||
|
||||
// 로컬 이벤트 발생 (다른 컴포넌트에서 변경 감지하도록)
|
||||
window.dispatchEvent(new Event('transactionDeleted'));
|
||||
}, []);
|
||||
|
||||
// 트랜잭션 초기화 함수
|
||||
|
||||
Reference in New Issue
Block a user