Fix persistent sync failure alerts
Addresses an issue where sync failures were repeatedly triggering notifications, even after the initial failure was acknowledged.
This commit is contained in:
@@ -10,6 +10,7 @@ import ExpenseForm, { ExpenseFormValues } from './expenses/ExpenseForm';
|
||||
import { Transaction } from '@/contexts/budget/types';
|
||||
import { normalizeDate } from '@/utils/sync/transaction/dateUtils';
|
||||
import useNotifications from '@/hooks/useNotifications';
|
||||
import { checkNetworkStatus } from '@/utils/network/checker';
|
||||
|
||||
const AddTransactionButton = () => {
|
||||
const [showExpenseDialog, setShowExpenseDialog] = useState(false);
|
||||
@@ -54,66 +55,75 @@ const AddTransactionButton = () => {
|
||||
// BudgetContext를 통해 지출 추가
|
||||
addTransaction(newExpense);
|
||||
|
||||
try {
|
||||
const { data: { user } } = await supabase.auth.getUser();
|
||||
|
||||
if (isSyncEnabled() && user) {
|
||||
// ISO 형식으로 날짜 변환
|
||||
const isoDate = normalizeDate(formattedDate);
|
||||
|
||||
const { error } = await supabase.from('transactions').insert({
|
||||
user_id: user.id,
|
||||
title: data.title,
|
||||
amount: parseInt(numericAmount),
|
||||
date: isoDate, // ISO 형식 사용
|
||||
category: data.category,
|
||||
type: 'expense',
|
||||
transaction_id: newExpense.id,
|
||||
payment_method: data.paymentMethod // Supabase에 필드 추가
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
// 지출 추가 후 자동 동기화 실행
|
||||
console.log('지출 추가 후 자동 동기화 시작');
|
||||
const syncResult = await trySyncAllData(user.id);
|
||||
|
||||
if (syncResult.success) {
|
||||
// 동기화 성공 시 마지막 동기화 시간 업데이트
|
||||
const currentTime = new Date().toISOString();
|
||||
console.log('자동 동기화 성공, 시간 업데이트:', currentTime);
|
||||
setLastSyncTime(currentTime);
|
||||
|
||||
// 동기화 성공 알림 추가
|
||||
addNotification(
|
||||
'동기화 완료',
|
||||
'방금 추가하신 지출 데이터가 클라우드에 동기화되었습니다.'
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Supabase에 지출 추가 실패:', error);
|
||||
// 실패 시 알림 추가
|
||||
addNotification(
|
||||
'동기화 실패',
|
||||
'지출 데이터 동기화 중 문제가 발생했습니다. 나중에 다시 시도됩니다.'
|
||||
);
|
||||
}
|
||||
|
||||
// 다이얼로그를 닫습니다
|
||||
setShowExpenseDialog(false);
|
||||
|
||||
// 이벤트 발생 처리 - 단일 이벤트로 통합
|
||||
window.dispatchEvent(new CustomEvent('transactionChanged', {
|
||||
detail: { type: 'add', transaction: newExpense }
|
||||
}));
|
||||
|
||||
// 토스트는 한 번만 표시 (지연 제거하여 래퍼에서 처리되도록)
|
||||
toast({
|
||||
title: "지출이 추가되었습니다",
|
||||
description: `${data.title} 항목이 ${formatWithCommas(numericAmount)}원으로 등록되었습니다.`,
|
||||
duration: 3000
|
||||
});
|
||||
|
||||
// 네트워크 상태 확인 후 Supabase 동기화 시도
|
||||
const isOnline = await checkNetworkStatus();
|
||||
|
||||
if (isSyncEnabled() && isOnline) {
|
||||
try {
|
||||
const { data: { user } } = await supabase.auth.getUser();
|
||||
|
||||
if (user) {
|
||||
// ISO 형식으로 날짜 변환
|
||||
const isoDate = normalizeDate(formattedDate);
|
||||
|
||||
console.log('Supabase에 지출 추가 시도 중...');
|
||||
const { error } = await supabase.from('transactions').insert({
|
||||
user_id: user.id,
|
||||
title: data.title,
|
||||
amount: parseInt(numericAmount),
|
||||
date: isoDate, // ISO 형식 사용
|
||||
category: data.category,
|
||||
type: 'expense',
|
||||
transaction_id: newExpense.id,
|
||||
payment_method: data.paymentMethod // Supabase에 필드 추가
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.error('Supabase 데이터 저장 오류:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
// 지출 추가 후 자동 동기화 실행
|
||||
console.log('지출 추가 후 자동 동기화 시작');
|
||||
const syncResult = await trySyncAllData(user.id);
|
||||
|
||||
if (syncResult.success) {
|
||||
// 동기화 성공 시 마지막 동기화 시간 업데이트
|
||||
const currentTime = new Date().toISOString();
|
||||
console.log('자동 동기화 성공, 시간 업데이트:', currentTime);
|
||||
setLastSyncTime(currentTime);
|
||||
|
||||
// 동기화 성공 알림 추가
|
||||
addNotification(
|
||||
'동기화 완료',
|
||||
'방금 추가하신 지출 데이터가 클라우드에 동기화되었습니다.'
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Supabase에 지출 추가 실패:', error);
|
||||
// 실패해도 조용히 처리 (나중에 자동으로 재시도될 것임)
|
||||
console.log('로컬 데이터는 저장됨, 다음 동기화에서 재시도할 예정');
|
||||
}
|
||||
} else if (isSyncEnabled() && !isOnline) {
|
||||
console.log('네트워크 연결이 없어 로컬에만 저장되었습니다. 다음 동기화에서 업로드될 예정입니다.');
|
||||
}
|
||||
|
||||
// 이벤트 발생 처리 - 단일 이벤트로 통합
|
||||
window.dispatchEvent(new CustomEvent('transactionChanged', {
|
||||
detail: { type: 'add', transaction: newExpense }
|
||||
}));
|
||||
|
||||
} catch (error) {
|
||||
console.error('지출 추가 중 오류 발생:', error);
|
||||
toast({
|
||||
|
||||
Reference in New Issue
Block a user