Fix data persistence issue
Addresses a problem where budget and expense data was not being saved correctly.
This commit is contained in:
@@ -4,74 +4,95 @@ import { DEFAULT_CATEGORY_BUDGETS, getInitialBudgetData } from './budgetUtils';
|
||||
|
||||
// 로컬 스토리지에서 트랜잭션 불러오기
|
||||
export const loadTransactionsFromStorage = (): Transaction[] => {
|
||||
const storedTransactions = localStorage.getItem('transactions');
|
||||
if (storedTransactions) {
|
||||
try {
|
||||
try {
|
||||
const storedTransactions = localStorage.getItem('transactions');
|
||||
if (storedTransactions) {
|
||||
return JSON.parse(storedTransactions);
|
||||
} catch (error) {
|
||||
console.error('트랜잭션 데이터 파싱 오류:', error);
|
||||
return [];
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('트랜잭션 데이터 파싱 오류:', error);
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
||||
// 트랜잭션 저장
|
||||
export const saveTransactionsToStorage = (transactions: Transaction[]): void => {
|
||||
localStorage.setItem('transactions', JSON.stringify(transactions));
|
||||
try {
|
||||
localStorage.setItem('transactions', JSON.stringify(transactions));
|
||||
console.log('트랜잭션 저장 완료, 항목 수:', transactions.length);
|
||||
} catch (error) {
|
||||
console.error('트랜잭션 저장 오류:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// 모든 트랜잭션 삭제
|
||||
export const clearAllTransactions = (): void => {
|
||||
localStorage.removeItem('transactions');
|
||||
// 빈 배열을 저장하여 확실히 초기화
|
||||
localStorage.setItem('transactions', JSON.stringify([]));
|
||||
try {
|
||||
localStorage.removeItem('transactions');
|
||||
// 빈 배열을 저장하여 확실히 초기화
|
||||
localStorage.setItem('transactions', JSON.stringify([]));
|
||||
console.log('모든 트랜잭션이 삭제되었습니다.');
|
||||
} catch (error) {
|
||||
console.error('트랜잭션 삭제 오류:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// 카테고리 예산 불러오기
|
||||
export const loadCategoryBudgetsFromStorage = (): Record<string, number> => {
|
||||
const storedCategoryBudgets = localStorage.getItem('categoryBudgets');
|
||||
if (storedCategoryBudgets) {
|
||||
try {
|
||||
return JSON.parse(storedCategoryBudgets);
|
||||
} catch (error) {
|
||||
console.error('카테고리 예산 데이터 파싱 오류:', error);
|
||||
return DEFAULT_CATEGORY_BUDGETS;
|
||||
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 => {
|
||||
localStorage.setItem('categoryBudgets', JSON.stringify(categoryBudgets));
|
||||
try {
|
||||
localStorage.setItem('categoryBudgets', JSON.stringify(categoryBudgets));
|
||||
console.log('카테고리 예산 저장 완료:', categoryBudgets);
|
||||
} catch (error) {
|
||||
console.error('카테고리 예산 저장 오류:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// 모든 카테고리 예산 삭제
|
||||
export const clearAllCategoryBudgets = (): void => {
|
||||
localStorage.removeItem('categoryBudgets');
|
||||
// 기본값으로 재설정
|
||||
saveCategoryBudgetsToStorage(DEFAULT_CATEGORY_BUDGETS);
|
||||
try {
|
||||
localStorage.removeItem('categoryBudgets');
|
||||
// 기본값으로 재설정
|
||||
saveCategoryBudgetsToStorage(DEFAULT_CATEGORY_BUDGETS);
|
||||
console.log('카테고리 예산이 초기화되었습니다.');
|
||||
} catch (error) {
|
||||
console.error('카테고리 예산 삭제 오류:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// 예산 데이터 불러오기
|
||||
export const loadBudgetDataFromStorage = (): BudgetData => {
|
||||
const storedBudgetData = localStorage.getItem('budgetData');
|
||||
if (storedBudgetData) {
|
||||
try {
|
||||
return JSON.parse(storedBudgetData);
|
||||
} catch (error) {
|
||||
console.error('예산 데이터 파싱 오류:', error);
|
||||
const initialData = getInitialBudgetData();
|
||||
saveBudgetDataToStorage(initialData);
|
||||
return initialData;
|
||||
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;
|
||||
@@ -79,15 +100,25 @@ export const loadBudgetDataFromStorage = (): BudgetData => {
|
||||
|
||||
// 예산 데이터 저장
|
||||
export const saveBudgetDataToStorage = (budgetData: BudgetData): void => {
|
||||
localStorage.setItem('budgetData', JSON.stringify(budgetData));
|
||||
try {
|
||||
localStorage.setItem('budgetData', JSON.stringify(budgetData));
|
||||
console.log('예산 데이터 저장 완료');
|
||||
} catch (error) {
|
||||
console.error('예산 데이터 저장 오류:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// 모든 예산 데이터 삭제
|
||||
export const clearAllBudgetData = (): void => {
|
||||
localStorage.removeItem('budgetData');
|
||||
// 기본값으로 재설정
|
||||
const initialData = getInitialBudgetData();
|
||||
saveBudgetDataToStorage(initialData);
|
||||
try {
|
||||
localStorage.removeItem('budgetData');
|
||||
// 기본값으로 재설정
|
||||
const initialData = getInitialBudgetData();
|
||||
saveBudgetDataToStorage(initialData);
|
||||
console.log('예산 데이터가 초기화되었습니다.');
|
||||
} catch (error) {
|
||||
console.error('예산 데이터 삭제 오류:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// 모든 데이터 초기화 (첫 로그인 상태)
|
||||
|
||||
Reference in New Issue
Block a user