Refactor budget storage utils
Splits the budget storage utils file into smaller, more manageable files for better organization and maintainability.
This commit is contained in:
93
src/contexts/budget/storage/resetStorage.ts
Normal file
93
src/contexts/budget/storage/resetStorage.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
|
||||
import { clearAllTransactions } from './transactionStorage';
|
||||
import { clearAllCategoryBudgets } from './categoryStorage';
|
||||
import { clearAllBudgetData } from './budgetStorage';
|
||||
import { DEFAULT_CATEGORY_BUDGETS } from '../budgetUtils';
|
||||
import { getInitialBudgetData } from '../budgetUtils';
|
||||
|
||||
/**
|
||||
* 모든 데이터 초기화 (첫 로그인 상태)
|
||||
*/
|
||||
export const resetAllData = (): void => {
|
||||
console.log('완전 초기화 시작 - resetAllData');
|
||||
|
||||
// dontShowWelcome 설정 값 백업
|
||||
const dontShowWelcomeValue = localStorage.getItem('dontShowWelcome');
|
||||
console.log('resetAllData - dontShowWelcome 백업 값:', dontShowWelcomeValue);
|
||||
|
||||
// 모든 관련 데이터 키 목록 (분석 페이지의 데이터 포함)
|
||||
const dataKeys = [
|
||||
'transactions',
|
||||
'categoryBudgets',
|
||||
'budgetData',
|
||||
'budget',
|
||||
'hasVisitedBefore',
|
||||
'monthlyExpenses', // 월간 지출 데이터
|
||||
'categorySpending', // 카테고리별 지출 데이터
|
||||
'expenseAnalytics', // 지출 분석 데이터
|
||||
'expenseHistory', // 지출 이력
|
||||
'budgetHistory', // 예산 이력
|
||||
'analyticsCache', // 분석 캐시 데이터
|
||||
'monthlyTotals', // 월간 합계 데이터
|
||||
'analytics', // 분석 페이지 데이터
|
||||
'dailyBudget', // 일일 예산
|
||||
'weeklyBudget', // 주간 예산
|
||||
'monthlyBudget', // 월간 예산
|
||||
'chartData', // 차트 데이터
|
||||
// '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' && ( // dontShowWelcome 키는 제외
|
||||
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') ||
|
||||
key.includes('visited')
|
||||
)) {
|
||||
console.log(`추가 데이터 삭제: ${key}`);
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
}
|
||||
|
||||
// 강제로 빈 데이터로 초기화
|
||||
localStorage.setItem('transactions', JSON.stringify([]));
|
||||
localStorage.setItem('budget', JSON.stringify({total: 0}));
|
||||
localStorage.setItem('budgetData', JSON.stringify({
|
||||
daily: {targetAmount: 0, spentAmount: 0, remainingAmount: 0},
|
||||
weekly: {targetAmount: 0, spentAmount: 0, remainingAmount: 0},
|
||||
monthly: {targetAmount: 0, spentAmount: 0, remainingAmount: 0}
|
||||
}));
|
||||
localStorage.setItem('categoryBudgets', JSON.stringify(DEFAULT_CATEGORY_BUDGETS));
|
||||
|
||||
// dontShowWelcome 값 복원 (백업한 값이 있는 경우)
|
||||
if (dontShowWelcomeValue) {
|
||||
console.log('resetAllData - dontShowWelcome 값 복원:', dontShowWelcomeValue);
|
||||
localStorage.setItem('dontShowWelcome', dontShowWelcomeValue);
|
||||
|
||||
// 복원 확인
|
||||
const restoredValue = localStorage.getItem('dontShowWelcome');
|
||||
console.log('resetAllData - 복원 후 dontShowWelcome 값:', restoredValue);
|
||||
}
|
||||
|
||||
console.log('모든 데이터가 초기화되었습니다.');
|
||||
};
|
||||
Reference in New Issue
Block a user