The category budget setting is now based on the monthly budget amount, which is then divided into daily and weekly budgets.
105 lines
3.7 KiB
TypeScript
105 lines
3.7 KiB
TypeScript
|
|
import { clearAllTransactions } from './transactionStorage';
|
|
import { clearAllCategoryBudgets } from './categoryStorage';
|
|
import { clearAllBudgetData } from './budgetStorage';
|
|
import { DEFAULT_CATEGORY_BUDGETS } from '../budgetUtils';
|
|
import { getInitialBudgetData } from '../budgetUtils';
|
|
import { toast } from '@/hooks/useToast.wrapper';
|
|
|
|
/**
|
|
* 모든 데이터 초기화 (첫 로그인 상태)
|
|
*/
|
|
export const resetAllData = (): void => {
|
|
console.log('완전 초기화 시작 - resetAllData');
|
|
|
|
// 중요: 사용자 설정 관련 값 백업
|
|
const dontShowWelcomeValue = localStorage.getItem('dontShowWelcome');
|
|
const hasVisitedBefore = localStorage.getItem('hasVisitedBefore');
|
|
console.log('resetAllData - 사용자 설정 백업:', { dontShowWelcome: dontShowWelcomeValue, hasVisitedBefore });
|
|
|
|
// 모든 관련 데이터 키 목록 (분석 페이지의 데이터 포함)
|
|
const dataKeys = [
|
|
'transactions',
|
|
'categoryBudgets',
|
|
'budgetData',
|
|
'budget',
|
|
'monthlyExpenses',
|
|
'categorySpending',
|
|
'expenseAnalytics',
|
|
'expenseHistory',
|
|
'budgetHistory',
|
|
'analyticsCache',
|
|
'monthlyTotals',
|
|
'analytics',
|
|
'dailyBudget',
|
|
'weeklyBudget',
|
|
'monthlyBudget',
|
|
'chartData',
|
|
];
|
|
|
|
try {
|
|
// 모든 관련 데이터 키 삭제
|
|
dataKeys.forEach(key => {
|
|
console.log(`삭제 중: ${key}`);
|
|
localStorage.removeItem(key);
|
|
localStorage.removeItem(`${key}_backup`); // 백업 키도 함께 삭제
|
|
});
|
|
|
|
// 파일별 초기화 함수 호출
|
|
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([]));
|
|
|
|
// 이벤트 발생시켜 데이터 로드 트리거 - 이벤트 순서 최적화
|
|
const events = [
|
|
new Event('transactionUpdated'),
|
|
new Event('budgetDataUpdated'),
|
|
new Event('categoryBudgetsUpdated'),
|
|
new StorageEvent('storage')
|
|
];
|
|
|
|
// 모든 이벤트 동시에 발생
|
|
events.forEach(event => window.dispatchEvent(event));
|
|
|
|
// 중요: 사용자 설정 값 복원 (백업한 값이 있는 경우)
|
|
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: "데이터를 초기화하는데 문제가 발생했습니다.",
|
|
variant: "destructive"
|
|
});
|
|
}
|
|
};
|