Address unresolved issues
This commit addresses previously reported issues that remain unresolved. Further investigation is required.
This commit is contained in:
@@ -92,10 +92,10 @@ export const calculateUpdatedBudgetData = (
|
||||
|
||||
console.log(`최종 예산 계산: 월간=${monthlyAmount}원, 주간=${weeklyAmount}원, 일일=${dailyAmount}원`);
|
||||
|
||||
// 새로운 BudgetData를 생성하되, 기존의 spentAmount는 유지합니다
|
||||
// prevBudgetData 출력하여 디버깅
|
||||
console.log("이전 예산 데이터:", JSON.stringify(prevBudgetData));
|
||||
|
||||
// 새 예산 데이터 생성 (spentAmount는 이전 값 유지)
|
||||
return {
|
||||
daily: {
|
||||
targetAmount: dailyAmount,
|
||||
|
||||
@@ -65,8 +65,17 @@ export const useBudgetDataState = (transactions: Transaction[]) => {
|
||||
const budgetUpdateHandler = () => handleBudgetUpdate();
|
||||
window.addEventListener('budgetDataUpdated', budgetUpdateHandler);
|
||||
|
||||
// 스토리지 이벤트도 함께 처리 (다른 탭/창에서의 업데이트 감지)
|
||||
const storageUpdateHandler = (e: StorageEvent) => {
|
||||
if (e.key === 'budgetData' || e.key === null) {
|
||||
handleBudgetUpdate();
|
||||
}
|
||||
};
|
||||
window.addEventListener('storage', storageUpdateHandler);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('budgetDataUpdated', budgetUpdateHandler);
|
||||
window.removeEventListener('storage', storageUpdateHandler);
|
||||
};
|
||||
}, [isInitialized, budgetData]);
|
||||
|
||||
@@ -75,23 +84,29 @@ export const useBudgetDataState = (transactions: Transaction[]) => {
|
||||
if (transactions.length > 0 && isInitialized) {
|
||||
console.log('트랜잭션 변경으로 인한 예산 데이터 업데이트. 트랜잭션 수:', transactions.length);
|
||||
try {
|
||||
// 현재 예산 데이터 다시 로드 (최신 상태 확보)
|
||||
const currentBudgetData = loadBudgetDataFromStorage();
|
||||
|
||||
// 지출 금액 업데이트
|
||||
const updatedBudgetData = calculateSpentAmounts(transactions, budgetData);
|
||||
const updatedBudgetData = calculateSpentAmounts(transactions, currentBudgetData);
|
||||
|
||||
// 변경이 있을 때만 저장
|
||||
if (JSON.stringify(updatedBudgetData) !== JSON.stringify(budgetData)) {
|
||||
if (JSON.stringify(updatedBudgetData) !== JSON.stringify(currentBudgetData)) {
|
||||
// 상태 및 스토리지 모두 업데이트
|
||||
setBudgetData(updatedBudgetData);
|
||||
saveBudgetDataToStorage(updatedBudgetData);
|
||||
|
||||
// 저장 시간 업데이트
|
||||
localStorage.setItem('lastBudgetSaveTime', new Date().toISOString());
|
||||
|
||||
// 기타 컴포넌트에 이벤트 알림
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('예산 데이터 업데이트 중 오류:', error);
|
||||
}
|
||||
}
|
||||
}, [transactions, budgetData, isInitialized]);
|
||||
}, [transactions, isInitialized]);
|
||||
|
||||
// 예산 목표 업데이트 함수 - 수정됨
|
||||
const handleBudgetGoalUpdate = useCallback((
|
||||
@@ -112,8 +127,11 @@ export const useBudgetDataState = (transactions: Transaction[]) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// 현재 최신 예산 데이터 로드 (다른 곳에서 변경되었을 수 있음)
|
||||
const currentBudgetData = loadBudgetDataFromStorage();
|
||||
|
||||
// 예산 데이터 업데이트 - 일간, 주간, 월간 예산이 모두 자동으로 계산됨
|
||||
const updatedBudgetData = calculateUpdatedBudgetData(budgetData, type, amount);
|
||||
const updatedBudgetData = calculateUpdatedBudgetData(currentBudgetData, type, amount);
|
||||
console.log('새 예산 데이터 계산됨:', updatedBudgetData);
|
||||
|
||||
// 상태 및 스토리지 둘 다 업데이트
|
||||
@@ -129,6 +147,10 @@ export const useBudgetDataState = (transactions: Transaction[]) => {
|
||||
// 1초 후 데이터 갱신 이벤트 한 번 더 발생 (UI 갱신 확실히 하기 위함)
|
||||
setTimeout(() => {
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
window.dispatchEvent(new StorageEvent('storage', {
|
||||
key: 'budgetData',
|
||||
newValue: JSON.stringify(updatedBudgetData)
|
||||
}));
|
||||
}, 1000);
|
||||
|
||||
console.log('예산 목표 업데이트 완료:', updatedBudgetData);
|
||||
@@ -147,7 +169,7 @@ export const useBudgetDataState = (transactions: Transaction[]) => {
|
||||
variant: "destructive"
|
||||
});
|
||||
}
|
||||
}, [budgetData]);
|
||||
}, []);
|
||||
|
||||
// 예산 데이터 초기화 함수
|
||||
const resetBudgetData = useCallback(() => {
|
||||
|
||||
@@ -59,8 +59,7 @@ export const useExtendedBudgetUpdate = (
|
||||
return;
|
||||
}
|
||||
|
||||
// 월간 예산 금액으로 예산 데이터 업데이트
|
||||
// 월간 예산을 설정하면 자동으로 일간/주간 예산도 계산됨
|
||||
// 항상 월간 예산으로 처리하여 일/주간 자동계산 보장
|
||||
handleBudgetGoalUpdate('monthly', totalAmount);
|
||||
|
||||
// 명시적으로 BudgetData 업데이트 이벤트 발생
|
||||
@@ -72,9 +71,18 @@ export const useExtendedBudgetUpdate = (
|
||||
description: `월간 총 예산이 ${totalAmount.toLocaleString()}원으로 설정되었습니다.`
|
||||
});
|
||||
|
||||
// 1초 후 페이지 리프레시 (데이터 표시 강제 갱신)
|
||||
// 여러 번의 이벤트 발생으로 UI 업데이트 보장 (타이밍 문제 해결)
|
||||
setTimeout(() => {
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
}, 300);
|
||||
|
||||
setTimeout(() => {
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
// 스토리지 이벤트도 발생시켜 데이터 로드 보장
|
||||
window.dispatchEvent(new StorageEvent('storage', {
|
||||
key: 'budgetData',
|
||||
newValue: localStorage.getItem('budgetData')
|
||||
}));
|
||||
}, 1000);
|
||||
} catch (error) {
|
||||
console.error('카테고리 예산 업데이트 오류:', error);
|
||||
@@ -86,7 +94,6 @@ export const useExtendedBudgetUpdate = (
|
||||
}
|
||||
} else {
|
||||
// 카테고리 예산이 없는 경우, 선택된 기간 유형에 맞게 예산 설정
|
||||
// 이 경우에도 다른 기간의 예산이 자동으로 계산됨
|
||||
if (amount <= 0) {
|
||||
toast({
|
||||
title: "예산 설정 오류",
|
||||
@@ -96,7 +103,24 @@ export const useExtendedBudgetUpdate = (
|
||||
return;
|
||||
}
|
||||
|
||||
handleBudgetGoalUpdate(type, amount);
|
||||
// 어떤 타입이 들어오더라도 항상 월간으로 처리하고 일/주간은 자동계산
|
||||
if (type !== 'monthly') {
|
||||
console.log(`${type} 입력을 월간 예산으로 변환합니다.`);
|
||||
// 일간 입력인 경우 월간으로 변환 (30배)
|
||||
if (type === 'daily') {
|
||||
amount = amount * 30;
|
||||
}
|
||||
// 주간 입력인 경우 월간으로 변환 (4.3배)
|
||||
else if (type === 'weekly') {
|
||||
amount = Math.round(amount * 4.3);
|
||||
}
|
||||
|
||||
// 변환된 금액으로 월간 예산 설정
|
||||
handleBudgetGoalUpdate('monthly', amount);
|
||||
} else {
|
||||
// 원래 월간이면 그대로 설정
|
||||
handleBudgetGoalUpdate('monthly', amount);
|
||||
}
|
||||
|
||||
// 명시적으로 BudgetData 업데이트 이벤트 발생
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
@@ -108,9 +132,18 @@ export const useExtendedBudgetUpdate = (
|
||||
description: `${periodText} 예산이 ${amount.toLocaleString()}원으로 설정되었습니다.`
|
||||
});
|
||||
|
||||
// 1초 후 페이지 리프레시 (데이터 표시 강제 갱신)
|
||||
// 데이터 표시 강제 갱신 (타이밍 문제 해결을 위한 여러 이벤트 발생)
|
||||
setTimeout(() => {
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
}, 300);
|
||||
|
||||
setTimeout(() => {
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
// 스토리지 이벤트도 발생시켜 데이터 로드 보장
|
||||
window.dispatchEvent(new StorageEvent('storage', {
|
||||
key: 'budgetData',
|
||||
newValue: localStorage.getItem('budgetData')
|
||||
}));
|
||||
}, 1000);
|
||||
}
|
||||
}, [categoryBudgets, handleBudgetGoalUpdate, updateCategoryBudgets]);
|
||||
|
||||
Reference in New Issue
Block a user