Fix budget update issues
Addresses delayed notifications and data loss after budget updates and page transitions.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
|
||||
import { BudgetData } from '../types';
|
||||
import { getInitialBudgetData } from '../budgetUtils';
|
||||
import { toast } from '@/components/ui/use-toast';
|
||||
|
||||
/**
|
||||
* 예산 데이터 불러오기
|
||||
@@ -36,14 +37,36 @@ export const saveBudgetDataToStorage = (budgetData: BudgetData): void => {
|
||||
localStorage.setItem('budgetData', dataString);
|
||||
console.log('예산 데이터 저장 완료', budgetData);
|
||||
|
||||
// 중요: 즉시 자동 백업 (데이터 손실 방지)
|
||||
localStorage.setItem('budgetData_backup', dataString);
|
||||
|
||||
// 스토리지 이벤트 수동 트리거 (동일 창에서도 감지하기 위함)
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
window.dispatchEvent(new StorageEvent('storage', {
|
||||
key: 'budgetData',
|
||||
newValue: dataString
|
||||
}));
|
||||
try {
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
window.dispatchEvent(new StorageEvent('storage', {
|
||||
key: 'budgetData',
|
||||
newValue: dataString
|
||||
}));
|
||||
} catch (e) {
|
||||
console.error('이벤트 발생 오류:', e);
|
||||
}
|
||||
|
||||
// toast 알림은 즉시 표시
|
||||
if (budgetData.monthly.targetAmount > 0) {
|
||||
toast({
|
||||
title: "예산 저장 완료",
|
||||
description: `월 예산이 ${budgetData.monthly.targetAmount.toLocaleString()}원으로 설정되었습니다.`,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('예산 데이터 저장 오류:', error);
|
||||
|
||||
// 오류 발생 시 토스트 알림
|
||||
toast({
|
||||
title: "예산 저장 실패",
|
||||
description: "예산 데이터를 저장하는데 문제가 발생했습니다.",
|
||||
variant: "destructive"
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -53,10 +76,14 @@ export const saveBudgetDataToStorage = (budgetData: BudgetData): void => {
|
||||
export const clearAllBudgetData = (): void => {
|
||||
try {
|
||||
localStorage.removeItem('budgetData');
|
||||
localStorage.removeItem('budgetData_backup');
|
||||
|
||||
// 기본값으로 재설정
|
||||
const initialData = getInitialBudgetData();
|
||||
const dataString = JSON.stringify(initialData);
|
||||
localStorage.setItem('budgetData', dataString);
|
||||
localStorage.setItem('budgetData_backup', dataString);
|
||||
|
||||
console.log('예산 데이터가 초기화되었습니다.');
|
||||
|
||||
// 스토리지 이벤트 수동 트리거
|
||||
@@ -65,7 +92,18 @@ export const clearAllBudgetData = (): void => {
|
||||
key: 'budgetData',
|
||||
newValue: dataString
|
||||
}));
|
||||
|
||||
// 토스트 알림
|
||||
toast({
|
||||
title: "예산 초기화",
|
||||
description: "모든, 예산 데이터가 초기화되었습니다.",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('예산 데이터 삭제 오류:', error);
|
||||
toast({
|
||||
title: "초기화 실패",
|
||||
description: "예산 데이터를 초기화하는데 문제가 발생했습니다.",
|
||||
variant: "destructive"
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,17 +1,29 @@
|
||||
|
||||
import { DEFAULT_CATEGORY_BUDGETS } from '../budgetUtils';
|
||||
import { toast } from '@/components/ui/use-toast';
|
||||
|
||||
/**
|
||||
* 카테고리 예산 불러오기
|
||||
*/
|
||||
export const loadCategoryBudgetsFromStorage = (): Record<string, number> => {
|
||||
try {
|
||||
// 메인 스토리지에서 시도
|
||||
const storedCategoryBudgets = localStorage.getItem('categoryBudgets');
|
||||
if (storedCategoryBudgets) {
|
||||
const parsed = JSON.parse(storedCategoryBudgets);
|
||||
console.log('카테고리 예산 로드 완료:', parsed);
|
||||
return parsed;
|
||||
}
|
||||
|
||||
// 백업에서 시도
|
||||
const backupCategoryBudgets = localStorage.getItem('categoryBudgets_backup');
|
||||
if (backupCategoryBudgets) {
|
||||
const parsedBackup = JSON.parse(backupCategoryBudgets);
|
||||
console.log('백업에서 카테고리 예산 복구:', parsedBackup);
|
||||
// 메인 스토리지도 복구
|
||||
localStorage.setItem('categoryBudgets', backupCategoryBudgets);
|
||||
return parsedBackup;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('카테고리 예산 데이터 파싱 오류:', error);
|
||||
}
|
||||
@@ -32,16 +44,42 @@ export const saveCategoryBudgetsToStorage = (categoryBudgets: Record<string, num
|
||||
|
||||
// 로컬 스토리지에 저장
|
||||
localStorage.setItem('categoryBudgets', dataString);
|
||||
// 백업 저장
|
||||
localStorage.setItem('categoryBudgets_backup', dataString);
|
||||
|
||||
console.log('카테고리 예산 저장 완료:', categoryBudgets);
|
||||
|
||||
// 스토리지 이벤트 수동 트리거 (동일 창에서도 감지하기 위함)
|
||||
window.dispatchEvent(new Event('categoryBudgetsUpdated'));
|
||||
window.dispatchEvent(new StorageEvent('storage', {
|
||||
key: 'categoryBudgets',
|
||||
newValue: dataString
|
||||
}));
|
||||
try {
|
||||
window.dispatchEvent(new Event('categoryBudgetsUpdated'));
|
||||
window.dispatchEvent(new StorageEvent('storage', {
|
||||
key: 'categoryBudgets',
|
||||
newValue: dataString
|
||||
}));
|
||||
} catch (e) {
|
||||
console.error('이벤트 발생 오류:', e);
|
||||
}
|
||||
|
||||
// 마지막 저장 시간 기록 (데이터 검증용)
|
||||
localStorage.setItem('lastCategoryBudgetSaveTime', new Date().toISOString());
|
||||
|
||||
// 토스트 알림
|
||||
const totalBudget = Object.values(categoryBudgets).reduce((sum, val) => sum + val, 0);
|
||||
if (totalBudget > 0) {
|
||||
toast({
|
||||
title: "카테고리 예산 저장 완료",
|
||||
description: `카테고리별 예산 총 ${totalBudget.toLocaleString()}원이 설정되었습니다.`,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('카테고리 예산 저장 오류:', error);
|
||||
|
||||
// 오류 발생 시 토스트 알림
|
||||
toast({
|
||||
title: "카테고리 예산 저장 실패",
|
||||
description: "카테고리 예산을 저장하는데 문제가 발생했습니다.",
|
||||
variant: "destructive"
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -51,9 +89,13 @@ export const saveCategoryBudgetsToStorage = (categoryBudgets: Record<string, num
|
||||
export const clearAllCategoryBudgets = (): void => {
|
||||
try {
|
||||
localStorage.removeItem('categoryBudgets');
|
||||
localStorage.removeItem('categoryBudgets_backup');
|
||||
|
||||
// 기본값으로 재설정
|
||||
const dataString = JSON.stringify(DEFAULT_CATEGORY_BUDGETS);
|
||||
localStorage.setItem('categoryBudgets', dataString);
|
||||
localStorage.setItem('categoryBudgets_backup', dataString);
|
||||
|
||||
console.log('카테고리 예산이 초기화되었습니다.');
|
||||
|
||||
// 이벤트 발생
|
||||
@@ -62,7 +104,20 @@ export const clearAllCategoryBudgets = (): void => {
|
||||
key: 'categoryBudgets',
|
||||
newValue: dataString
|
||||
}));
|
||||
|
||||
// 토스트 알림
|
||||
toast({
|
||||
title: "카테고리 예산 초기화",
|
||||
description: "모든 카테고리 예산이 기본값으로 초기화되었습니다.",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('카테고리 예산 삭제 오류:', error);
|
||||
|
||||
// 오류 발생 시 토스트 알림
|
||||
toast({
|
||||
title: "초기화 실패",
|
||||
description: "카테고리 예산을 초기화하는데 문제가 발생했습니다.",
|
||||
variant: "destructive"
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,17 +1,29 @@
|
||||
|
||||
import { Transaction } from '../types';
|
||||
import { toast } from '@/components/ui/use-toast';
|
||||
|
||||
/**
|
||||
* 로컬 스토리지에서 트랜잭션 불러오기
|
||||
*/
|
||||
export const loadTransactionsFromStorage = (): Transaction[] => {
|
||||
try {
|
||||
// 메인 스토리지에서 먼저 시도
|
||||
const storedTransactions = localStorage.getItem('transactions');
|
||||
if (storedTransactions) {
|
||||
const parsedData = JSON.parse(storedTransactions);
|
||||
console.log('트랜잭션 로드 완료, 항목 수:', parsedData.length);
|
||||
return parsedData;
|
||||
}
|
||||
|
||||
// 백업에서 시도
|
||||
const backupTransactions = localStorage.getItem('transactions_backup');
|
||||
if (backupTransactions) {
|
||||
const parsedBackup = JSON.parse(backupTransactions);
|
||||
console.log('백업에서 트랜잭션 복구, 항목 수:', parsedBackup.length);
|
||||
// 메인 스토리지도 복구
|
||||
localStorage.setItem('transactions', backupTransactions);
|
||||
return parsedBackup;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('트랜잭션 데이터 파싱 오류:', error);
|
||||
}
|
||||
@@ -28,16 +40,33 @@ export const saveTransactionsToStorage = (transactions: Transaction[]): void =>
|
||||
|
||||
// 로컬 스토리지에 저장
|
||||
localStorage.setItem('transactions', dataString);
|
||||
// 백업 저장
|
||||
localStorage.setItem('transactions_backup', dataString);
|
||||
|
||||
console.log('트랜잭션 저장 완료, 항목 수:', transactions.length);
|
||||
|
||||
// 스토리지 이벤트 수동 트리거 (동일 창에서도 감지하기 위함)
|
||||
window.dispatchEvent(new Event('transactionUpdated'));
|
||||
window.dispatchEvent(new StorageEvent('storage', {
|
||||
key: 'transactions',
|
||||
newValue: dataString
|
||||
}));
|
||||
try {
|
||||
window.dispatchEvent(new Event('transactionUpdated'));
|
||||
window.dispatchEvent(new StorageEvent('storage', {
|
||||
key: 'transactions',
|
||||
newValue: dataString
|
||||
}));
|
||||
} catch (e) {
|
||||
console.error('이벤트 발생 오류:', e);
|
||||
}
|
||||
|
||||
// 마지막 저장 시간 기록 (데이터 검증용)
|
||||
localStorage.setItem('lastTransactionSaveTime', new Date().toISOString());
|
||||
} catch (error) {
|
||||
console.error('트랜잭션 저장 오류:', error);
|
||||
|
||||
// 오류 발생 시 토스트 알림
|
||||
toast({
|
||||
title: "지출 저장 실패",
|
||||
description: "지출 데이터를 저장하는데 문제가 발생했습니다.",
|
||||
variant: "destructive"
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -47,9 +76,13 @@ export const saveTransactionsToStorage = (transactions: Transaction[]): void =>
|
||||
export const clearAllTransactions = (): void => {
|
||||
try {
|
||||
localStorage.removeItem('transactions');
|
||||
localStorage.removeItem('transactions_backup');
|
||||
|
||||
// 빈 배열을 저장하여 확실히 초기화
|
||||
const emptyData = JSON.stringify([]);
|
||||
localStorage.setItem('transactions', emptyData);
|
||||
localStorage.setItem('transactions_backup', emptyData);
|
||||
|
||||
console.log('모든 트랜잭션이 삭제되었습니다.');
|
||||
|
||||
// 스토리지 이벤트 수동 트리거
|
||||
@@ -58,7 +91,20 @@ export const clearAllTransactions = (): void => {
|
||||
key: 'transactions',
|
||||
newValue: emptyData
|
||||
}));
|
||||
|
||||
// 토스트 알림
|
||||
toast({
|
||||
title: "지출 내역 초기화",
|
||||
description: "모든 지출 내역이 삭제되었습니다.",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('트랜잭션 삭제 오류:', error);
|
||||
|
||||
// 오류 발생 시 토스트 알림
|
||||
toast({
|
||||
title: "초기화 실패",
|
||||
description: "지출 내역을 초기화하는데 문제가 발생했습니다.",
|
||||
variant: "destructive"
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user