Fix budget display issues
The budget was not being displayed correctly in the spending and analytics screens, as well as the weekly and monthly views on the home screen. This commit addresses these issues.
This commit is contained in:
@@ -7,8 +7,7 @@ import { isSyncEnabled } from '@/utils/syncUtils';
|
||||
import { MONTHS_KR, getCurrentMonth, getPrevMonth, getNextMonth } from '@/utils/dateUtils';
|
||||
import {
|
||||
loadTransactionsFromStorage,
|
||||
saveTransactionsToStorage,
|
||||
loadBudgetFromStorage
|
||||
saveTransactionsToStorage
|
||||
} from '@/utils/storageUtils';
|
||||
import {
|
||||
filterTransactionsByMonth,
|
||||
@@ -32,7 +31,7 @@ export const useTransactions = () => {
|
||||
const [searchQuery, setSearchQuery] = useState('');
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [totalBudget, setTotalBudget] = useState(1000000); // 기본 예산
|
||||
const [totalBudget, setTotalBudget] = useState(0);
|
||||
const { user } = useAuth();
|
||||
|
||||
// 월 변경 처리
|
||||
@@ -69,8 +68,16 @@ export const useTransactions = () => {
|
||||
setTransactions(filteredData);
|
||||
|
||||
// 예산 가져오기
|
||||
const budget = loadBudgetFromStorage();
|
||||
setTotalBudget(budget);
|
||||
const budgetDataStr = localStorage.getItem('budgetData');
|
||||
if (budgetDataStr) {
|
||||
try {
|
||||
const budgetData = JSON.parse(budgetDataStr);
|
||||
setTotalBudget(budgetData.monthly.targetAmount);
|
||||
} catch (e) {
|
||||
console.error('예산 데이터 파싱 오류:', e);
|
||||
setTotalBudget(0);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('트랜잭션 로드 중 오류:', err);
|
||||
setError('데이터를 불러오는 중 문제가 발생했습니다.');
|
||||
@@ -113,6 +120,31 @@ export const useTransactions = () => {
|
||||
};
|
||||
|
||||
syncWithSupabase();
|
||||
|
||||
// 예산 데이터 변경 이벤트 리스너
|
||||
const handleBudgetUpdate = () => {
|
||||
const budgetDataStr = localStorage.getItem('budgetData');
|
||||
if (budgetDataStr) {
|
||||
try {
|
||||
const budgetData = JSON.parse(budgetDataStr);
|
||||
setTotalBudget(budgetData.monthly.targetAmount);
|
||||
} catch (e) {
|
||||
console.error('예산 데이터 파싱 오류:', e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener('budgetDataUpdated', handleBudgetUpdate);
|
||||
window.addEventListener('storage', (e) => {
|
||||
if (e.key === 'budgetData') {
|
||||
handleBudgetUpdate();
|
||||
}
|
||||
});
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('budgetDataUpdated', handleBudgetUpdate);
|
||||
window.removeEventListener('storage', () => {});
|
||||
};
|
||||
}, [user]);
|
||||
|
||||
// 트랜잭션 업데이트
|
||||
|
||||
Reference in New Issue
Block a user