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:
gpt-engineer-app[bot]
2025-03-16 06:41:57 +00:00
parent c23f5cddef
commit 16c17f0257
9 changed files with 135 additions and 37 deletions

View File

@@ -5,7 +5,7 @@ import {
loadBudgetDataFromStorage,
saveBudgetDataToStorage,
clearAllBudgetData
} from '../storageUtils';
} from '../storage';
import { toast } from '@/components/ui/use-toast';
import {
calculateUpdatedBudgetData,
@@ -17,21 +17,56 @@ export const useBudgetDataState = (transactions: any[]) => {
const [budgetData, setBudgetData] = useState<BudgetData>(loadBudgetDataFromStorage());
const [selectedTab, setSelectedTab] = useState<BudgetPeriod>("daily");
// 지출 금액 업데이트
// 지출 금액 업데이트 - 트랜잭션이 변경될 때마다 실행
useEffect(() => {
// 스토리지에서 데이터 로드
const loadedBudgetData = loadBudgetDataFromStorage();
// 지출 금액 업데이트
const updatedBudgetData = calculateSpentAmounts(transactions, loadedBudgetData);
const updatedBudgetData = calculateSpentAmounts(transactions, budgetData);
// 상태 및 스토리지 모두 업데이트
setBudgetData(updatedBudgetData);
saveBudgetDataToStorage(updatedBudgetData);
}, [transactions, budgetData]);
// 이벤트 리스너 설정
useEffect(() => {
const handleBudgetUpdate = () => {
const loadedBudgetData = loadBudgetDataFromStorage();
setBudgetData(prevData => {
// 예산 목표는 로드된 데이터에서 가져오고, 지출 금액은 유지
const updatedData = {
daily: {
...loadedBudgetData.daily,
spentAmount: prevData.daily.spentAmount,
},
weekly: {
...loadedBudgetData.weekly,
spentAmount: prevData.weekly.spentAmount,
},
monthly: {
...loadedBudgetData.monthly,
spentAmount: prevData.monthly.spentAmount,
}
};
// 남은 금액 재계산
updatedData.daily.remainingAmount = updatedData.daily.targetAmount - updatedData.daily.spentAmount;
updatedData.weekly.remainingAmount = updatedData.weekly.targetAmount - updatedData.weekly.spentAmount;
updatedData.monthly.remainingAmount = updatedData.monthly.targetAmount - updatedData.monthly.spentAmount;
return updatedData;
});
};
// 로컬 이벤트 발생 (다른 컴포넌트에서 변경 감지하도록)
window.dispatchEvent(new Event('budgetDataUpdated'));
}, [transactions]);
window.addEventListener('budgetDataUpdated', handleBudgetUpdate);
window.addEventListener('categoryBudgetsUpdated', handleBudgetUpdate);
window.addEventListener('storage', handleBudgetUpdate);
return () => {
window.removeEventListener('budgetDataUpdated', handleBudgetUpdate);
window.removeEventListener('categoryBudgetsUpdated', handleBudgetUpdate);
window.removeEventListener('storage', handleBudgetUpdate);
};
}, []);
// 예산 목표 업데이트 함수
const handleBudgetGoalUpdate = useCallback((
@@ -45,6 +80,9 @@ export const useBudgetDataState = (transactions: any[]) => {
setBudgetData(updatedBudgetData);
saveBudgetDataToStorage(updatedBudgetData);
// 이벤트 발생시키기
window.dispatchEvent(new Event('budgetDataUpdated'));
toast({
title: "목표 업데이트 완료",
description: `${type === 'daily' ? '일일' : type === 'weekly' ? '주간' : '월간'} 목표가 ${amount.toLocaleString()}원으로 설정되었습니다.`