195 lines
6.5 KiB
TypeScript
195 lines
6.5 KiB
TypeScript
|
|
import { BudgetData, Transaction } from './types';
|
|
import { DEFAULT_CATEGORY_BUDGETS, getInitialBudgetData } from './budgetUtils';
|
|
|
|
// 로컬 스토리지에서 트랜잭션 불러오기
|
|
export const loadTransactionsFromStorage = (): Transaction[] => {
|
|
try {
|
|
const storedTransactions = localStorage.getItem('transactions');
|
|
if (storedTransactions) {
|
|
return JSON.parse(storedTransactions);
|
|
}
|
|
} catch (error) {
|
|
console.error('트랜잭션 데이터 파싱 오류:', error);
|
|
}
|
|
return [];
|
|
};
|
|
|
|
// 트랜잭션 저장
|
|
export const saveTransactionsToStorage = (transactions: Transaction[]): void => {
|
|
try {
|
|
localStorage.setItem('transactions', JSON.stringify(transactions));
|
|
console.log('트랜잭션 저장 완료, 항목 수:', transactions.length);
|
|
} catch (error) {
|
|
console.error('트랜잭션 저장 오류:', error);
|
|
}
|
|
};
|
|
|
|
// 모든 트랜잭션 삭제
|
|
export const clearAllTransactions = (): void => {
|
|
try {
|
|
localStorage.removeItem('transactions');
|
|
// 빈 배열을 저장하여 확실히 초기화
|
|
localStorage.setItem('transactions', JSON.stringify([]));
|
|
console.log('모든 트랜잭션이 삭제되었습니다.');
|
|
} catch (error) {
|
|
console.error('트랜잭션 삭제 오류:', error);
|
|
}
|
|
};
|
|
|
|
// 카테고리 예산 불러오기
|
|
export const loadCategoryBudgetsFromStorage = (): Record<string, number> => {
|
|
try {
|
|
const storedCategoryBudgets = localStorage.getItem('categoryBudgets');
|
|
if (storedCategoryBudgets) {
|
|
const parsed = JSON.parse(storedCategoryBudgets);
|
|
console.log('카테고리 예산 로드 완료:', parsed);
|
|
return parsed;
|
|
}
|
|
} catch (error) {
|
|
console.error('카테고리 예산 데이터 파싱 오류:', error);
|
|
}
|
|
|
|
// 새 사용자를 위한 기본 카테고리 예산 저장
|
|
console.log('기본 카테고리 예산 설정');
|
|
saveCategoryBudgetsToStorage(DEFAULT_CATEGORY_BUDGETS);
|
|
return DEFAULT_CATEGORY_BUDGETS;
|
|
};
|
|
|
|
// 카테고리 예산 저장
|
|
export const saveCategoryBudgetsToStorage = (categoryBudgets: Record<string, number>): void => {
|
|
try {
|
|
localStorage.setItem('categoryBudgets', JSON.stringify(categoryBudgets));
|
|
console.log('카테고리 예산 저장 완료:', categoryBudgets);
|
|
} catch (error) {
|
|
console.error('카테고리 예산 저장 오류:', error);
|
|
}
|
|
};
|
|
|
|
// 모든 카테고리 예산 삭제
|
|
export const clearAllCategoryBudgets = (): void => {
|
|
try {
|
|
localStorage.removeItem('categoryBudgets');
|
|
// 기본값으로 재설정
|
|
saveCategoryBudgetsToStorage(DEFAULT_CATEGORY_BUDGETS);
|
|
console.log('카테고리 예산이 초기화되었습니다.');
|
|
} catch (error) {
|
|
console.error('카테고리 예산 삭제 오류:', error);
|
|
}
|
|
};
|
|
|
|
// 예산 데이터 불러오기
|
|
export const loadBudgetDataFromStorage = (): BudgetData => {
|
|
try {
|
|
const storedBudgetData = localStorage.getItem('budgetData');
|
|
if (storedBudgetData) {
|
|
const parsed = JSON.parse(storedBudgetData);
|
|
console.log('예산 데이터 로드 완료');
|
|
return parsed;
|
|
}
|
|
} catch (error) {
|
|
console.error('예산 데이터 파싱 오류:', error);
|
|
}
|
|
|
|
// 새 사용자를 위한 기본 예산 데이터 저장
|
|
console.log('기본 예산 데이터 설정');
|
|
const initialData = getInitialBudgetData();
|
|
saveBudgetDataToStorage(initialData);
|
|
return initialData;
|
|
};
|
|
|
|
// 예산 데이터 저장
|
|
export const saveBudgetDataToStorage = (budgetData: BudgetData): void => {
|
|
try {
|
|
localStorage.setItem('budgetData', JSON.stringify(budgetData));
|
|
console.log('예산 데이터 저장 완료');
|
|
} catch (error) {
|
|
console.error('예산 데이터 저장 오류:', error);
|
|
}
|
|
};
|
|
|
|
// 모든 예산 데이터 삭제
|
|
export const clearAllBudgetData = (): void => {
|
|
try {
|
|
localStorage.removeItem('budgetData');
|
|
// 기본값으로 재설정
|
|
const initialData = getInitialBudgetData();
|
|
saveBudgetDataToStorage(initialData);
|
|
console.log('예산 데이터가 초기화되었습니다.');
|
|
} catch (error) {
|
|
console.error('예산 데이터 삭제 오류:', error);
|
|
}
|
|
};
|
|
|
|
// 모든 데이터 초기화 (첫 로그인 상태)
|
|
export const resetAllData = (): void => {
|
|
console.log('완전 초기화 시작 - resetAllData');
|
|
|
|
// 모든 관련 데이터 키 목록 (분석 페이지의 데이터 포함)
|
|
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.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('welcome') ||
|
|
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));
|
|
|
|
console.log('모든 데이터가 초기화되었습니다.');
|
|
};
|