Fix budget display and category issues
- Fix budget amount not displaying outside the homepage. - Remove unexpected categories and revert to the original 3 categories.
This commit is contained in:
@@ -21,7 +21,10 @@ export const calculateCategorySpending = (
|
||||
|
||||
// 모든 카테고리에 대해 초기값 0 설정
|
||||
Object.keys(categoryBudgets).forEach(category => {
|
||||
categorySpending[category] = 0;
|
||||
// 3개 카테고리만 유지
|
||||
if (EXPENSE_CATEGORIES.includes(category)) {
|
||||
categorySpending[category] = 0;
|
||||
}
|
||||
});
|
||||
|
||||
expenseTransactions.forEach(t => {
|
||||
@@ -30,10 +33,10 @@ export const calculateCategorySpending = (
|
||||
}
|
||||
});
|
||||
|
||||
return Object.keys(categoryBudgets).map(category => ({
|
||||
return EXPENSE_CATEGORIES.map(category => ({
|
||||
title: category,
|
||||
current: categorySpending[category] || 0,
|
||||
total: categoryBudgets[category]
|
||||
total: categoryBudgets[category] || 0
|
||||
}));
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
import { DEFAULT_CATEGORY_BUDGETS } from '../budgetUtils';
|
||||
import { EXPENSE_CATEGORIES } from '@/constants/categoryIcons';
|
||||
import { toast } from '@/components/ui/use-toast';
|
||||
|
||||
/**
|
||||
@@ -12,7 +12,14 @@ export const loadCategoryBudgetsFromStorage = (): Record<string, number> => {
|
||||
if (storedCategoryBudgets) {
|
||||
const parsed = JSON.parse(storedCategoryBudgets);
|
||||
console.log('카테고리 예산 로드 완료:', parsed);
|
||||
return parsed;
|
||||
|
||||
// 3개 카테고리만 유지
|
||||
const filteredBudgets: Record<string, number> = {};
|
||||
EXPENSE_CATEGORIES.forEach(category => {
|
||||
filteredBudgets[category] = parsed[category] || 0;
|
||||
});
|
||||
|
||||
return filteredBudgets;
|
||||
}
|
||||
|
||||
// 백업에서 시도
|
||||
@@ -20,9 +27,16 @@ export const loadCategoryBudgetsFromStorage = (): Record<string, number> => {
|
||||
if (backupCategoryBudgets) {
|
||||
const parsedBackup = JSON.parse(backupCategoryBudgets);
|
||||
console.log('백업에서 카테고리 예산 복구:', parsedBackup);
|
||||
|
||||
// 3개 카테고리만 유지
|
||||
const filteredBudgets: Record<string, number> = {};
|
||||
EXPENSE_CATEGORIES.forEach(category => {
|
||||
filteredBudgets[category] = parsedBackup[category] || 0;
|
||||
});
|
||||
|
||||
// 메인 스토리지도 복구
|
||||
localStorage.setItem('categoryBudgets', backupCategoryBudgets);
|
||||
return parsedBackup;
|
||||
localStorage.setItem('categoryBudgets', JSON.stringify(filteredBudgets));
|
||||
return filteredBudgets;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('카테고리 예산 데이터 파싱 오류:', error);
|
||||
@@ -39,15 +53,21 @@ export const loadCategoryBudgetsFromStorage = (): Record<string, number> => {
|
||||
*/
|
||||
export const saveCategoryBudgetsToStorage = (categoryBudgets: Record<string, number>): void => {
|
||||
try {
|
||||
// 3개 카테고리만 유지하고 나머지는 제거
|
||||
const filteredBudgets: Record<string, number> = {};
|
||||
EXPENSE_CATEGORIES.forEach(category => {
|
||||
filteredBudgets[category] = categoryBudgets[category] || 0;
|
||||
});
|
||||
|
||||
// 데이터 문자열로 변환
|
||||
const dataString = JSON.stringify(categoryBudgets);
|
||||
const dataString = JSON.stringify(filteredBudgets);
|
||||
|
||||
// 로컬 스토리지에 저장
|
||||
localStorage.setItem('categoryBudgets', dataString);
|
||||
// 백업 저장
|
||||
localStorage.setItem('categoryBudgets_backup', dataString);
|
||||
|
||||
console.log('카테고리 예산 저장 완료:', categoryBudgets);
|
||||
console.log('카테고리 예산 저장 완료:', filteredBudgets);
|
||||
|
||||
// 스토리지 이벤트 수동 트리거 (동일 창에서도 감지하기 위함)
|
||||
try {
|
||||
@@ -64,7 +84,7 @@ export const saveCategoryBudgetsToStorage = (categoryBudgets: Record<string, num
|
||||
localStorage.setItem('lastCategoryBudgetSaveTime', new Date().toISOString());
|
||||
|
||||
// 토스트 알림
|
||||
const totalBudget = Object.values(categoryBudgets).reduce((sum, val) => sum + val, 0);
|
||||
const totalBudget = Object.values(filteredBudgets).reduce((sum, val) => sum + val, 0);
|
||||
if (totalBudget > 0) {
|
||||
toast({
|
||||
title: "카테고리 예산 저장 완료",
|
||||
@@ -91,7 +111,7 @@ export const clearAllCategoryBudgets = (): void => {
|
||||
localStorage.removeItem('categoryBudgets');
|
||||
localStorage.removeItem('categoryBudgets_backup');
|
||||
|
||||
// 기본값으로 재설정
|
||||
// 기본값으로 재설정 (3개 카테고리만)
|
||||
const dataString = JSON.stringify(DEFAULT_CATEGORY_BUDGETS);
|
||||
localStorage.setItem('categoryBudgets', dataString);
|
||||
localStorage.setItem('categoryBudgets_backup', dataString);
|
||||
|
||||
Reference in New Issue
Block a user