Refactor data initialization process
Review and adjust the data initialization logic to ensure proper functionality.
This commit is contained in:
@@ -12,10 +12,10 @@ import { toast } from '@/components/ui/use-toast';
|
||||
export const resetAllData = (): void => {
|
||||
console.log('완전 초기화 시작 - resetAllData');
|
||||
|
||||
// dontShowWelcome 설정 값 백업
|
||||
// 중요: 사용자 설정 관련 값 백업
|
||||
const dontShowWelcomeValue = localStorage.getItem('dontShowWelcome');
|
||||
const hasVisitedBefore = localStorage.getItem('hasVisitedBefore');
|
||||
console.log('resetAllData - dontShowWelcome 백업 값:', dontShowWelcomeValue);
|
||||
console.log('resetAllData - 사용자 설정 백업:', { dontShowWelcome: dontShowWelcomeValue, hasVisitedBefore });
|
||||
|
||||
// 모든 관련 데이터 키 목록 (분석 페이지의 데이터 포함)
|
||||
const dataKeys = [
|
||||
@@ -35,79 +35,68 @@ export const resetAllData = (): void => {
|
||||
'weeklyBudget', // 주간 예산
|
||||
'monthlyBudget', // 월간 예산
|
||||
'chartData', // 차트 데이터
|
||||
// 'hasVisitedBefore', // 방문 여부 - 제외
|
||||
// 'dontShowWelcome' 키는 삭제 목록에서 제외
|
||||
];
|
||||
|
||||
// 모든 관련 데이터 키 삭제
|
||||
dataKeys.forEach(key => {
|
||||
console.log(`삭제 중: ${key}`);
|
||||
localStorage.removeItem(key);
|
||||
});
|
||||
|
||||
// 기본 데이터로 초기화
|
||||
clearAllTransactions();
|
||||
clearAllCategoryBudgets();
|
||||
clearAllBudgetData();
|
||||
|
||||
// 추가적으로 사용자 기기에 저장된 모든 데이터 검사 (역순으로 루프)
|
||||
for (let i = localStorage.length - 1; i >= 0; i--) {
|
||||
const key = localStorage.key(i);
|
||||
if (key &&
|
||||
key !== 'dontShowWelcome' &&
|
||||
key !== 'hasVisitedBefore' && // hasVisitedBefore 키도 제외
|
||||
(key.includes('expense') ||
|
||||
key.includes('budget') ||
|
||||
key.includes('transaction') ||
|
||||
key.includes('analytics') ||
|
||||
key.includes('spending') ||
|
||||
key.includes('financial') ||
|
||||
key.includes('chart') ||
|
||||
key.includes('month') ||
|
||||
key.includes('sync') ||
|
||||
key.includes('total'))
|
||||
) {
|
||||
console.log(`추가 데이터 삭제: ${key}`);
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
}
|
||||
|
||||
// 강제로 빈 데이터로 초기화
|
||||
localStorage.setItem('transactions', JSON.stringify([]));
|
||||
localStorage.setItem('budget', JSON.stringify({total: 0}));
|
||||
const initialBudgetData = getInitialBudgetData();
|
||||
localStorage.setItem('budgetData', JSON.stringify(initialBudgetData));
|
||||
localStorage.setItem('categoryBudgets', JSON.stringify(DEFAULT_CATEGORY_BUDGETS));
|
||||
|
||||
// 이벤트 발생시켜 데이터 로드 트리거
|
||||
try {
|
||||
window.dispatchEvent(new Event('transactionUpdated'));
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
window.dispatchEvent(new Event('categoryBudgetsUpdated'));
|
||||
window.dispatchEvent(new StorageEvent('storage'));
|
||||
} catch (e) {
|
||||
console.error('이벤트 발생 오류:', e);
|
||||
}
|
||||
|
||||
// dontShowWelcome 값 복원 (백업한 값이 있는 경우)
|
||||
if (dontShowWelcomeValue) {
|
||||
console.log('resetAllData - dontShowWelcome 값 복원:', dontShowWelcomeValue);
|
||||
localStorage.setItem('dontShowWelcome', dontShowWelcomeValue);
|
||||
}
|
||||
|
||||
// hasVisitedBefore 값 복원 (백업한 값이 있는 경우)
|
||||
if (hasVisitedBefore) {
|
||||
console.log('resetAllData - hasVisitedBefore 값 복원:', hasVisitedBefore);
|
||||
localStorage.setItem('hasVisitedBefore', hasVisitedBefore);
|
||||
}
|
||||
|
||||
// 초기화 알림은 첫 방문인 경우에는 표시하지 않음
|
||||
if (hasVisitedBefore === 'true') {
|
||||
// 모든 관련 데이터 키 삭제
|
||||
dataKeys.forEach(key => {
|
||||
console.log(`삭제 중: ${key}`);
|
||||
localStorage.removeItem(key);
|
||||
});
|
||||
|
||||
// 파일별 초기화 함수 호출
|
||||
clearAllTransactions();
|
||||
clearAllCategoryBudgets();
|
||||
clearAllBudgetData();
|
||||
|
||||
// 기본 데이터로 초기화 (중복 삭제 방지를 위해 한 번만 실행)
|
||||
const initialBudgetData = getInitialBudgetData();
|
||||
localStorage.setItem('budgetData', JSON.stringify(initialBudgetData));
|
||||
localStorage.setItem('categoryBudgets', JSON.stringify(DEFAULT_CATEGORY_BUDGETS));
|
||||
localStorage.setItem('transactions', JSON.stringify([]));
|
||||
|
||||
// 중요: budgetData_backup도 설정하여 복구 가능하게 함
|
||||
localStorage.setItem('budgetData_backup', JSON.stringify(initialBudgetData));
|
||||
localStorage.setItem('categoryBudgets_backup', JSON.stringify(DEFAULT_CATEGORY_BUDGETS));
|
||||
localStorage.setItem('transactions_backup', JSON.stringify([]));
|
||||
|
||||
// 이벤트 발생시켜 데이터 로드 트리거
|
||||
try {
|
||||
window.dispatchEvent(new Event('transactionUpdated'));
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
window.dispatchEvent(new Event('categoryBudgetsUpdated'));
|
||||
window.dispatchEvent(new StorageEvent('storage'));
|
||||
} catch (e) {
|
||||
console.error('이벤트 발생 오류:', e);
|
||||
}
|
||||
|
||||
// 중요: 사용자 설정 값 복원 (백업한 값이 있는 경우)
|
||||
if (dontShowWelcomeValue) {
|
||||
console.log('resetAllData - dontShowWelcome 값 복원:', dontShowWelcomeValue);
|
||||
localStorage.setItem('dontShowWelcome', dontShowWelcomeValue);
|
||||
}
|
||||
|
||||
if (hasVisitedBefore) {
|
||||
console.log('resetAllData - hasVisitedBefore 값 복원:', hasVisitedBefore);
|
||||
localStorage.setItem('hasVisitedBefore', hasVisitedBefore);
|
||||
}
|
||||
|
||||
// 첫 방문이 아닌 경우에만 토스트 알림 표시
|
||||
if (hasVisitedBefore === 'true') {
|
||||
toast({
|
||||
title: "데이터 초기화 완료",
|
||||
description: "모든 예산 및 지출 데이터가 초기화되었습니다.",
|
||||
});
|
||||
}
|
||||
|
||||
console.log('모든 데이터가 초기화되었습니다.');
|
||||
} catch (error) {
|
||||
console.error('데이터 초기화 중 오류 발생:', error);
|
||||
// 오류 발생 시 토스트 알림
|
||||
toast({
|
||||
title: "데이터 초기화 완료",
|
||||
description: "모든 예산 및 지출 데이터가 초기화되었습니다.",
|
||||
title: "초기화 실패",
|
||||
description: "데이터를 초기화하는데 문제가 발생했습니다.",
|
||||
variant: "destructive"
|
||||
});
|
||||
}
|
||||
|
||||
console.log('모든 데이터가 초기화되었습니다.');
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user