Refactor deleteTransaction hook
Refactors the `deleteTransaction` hook into smaller, more manageable units to improve code maintainability.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
|
||||
import { MutableRefObject } from 'react';
|
||||
import { Transaction } from '@/components/TransactionCard';
|
||||
import { saveTransactionsToStorage } from '../../storageUtils';
|
||||
import { deleteTransactionFromSupabase } from '../../supabaseUtils';
|
||||
|
||||
/**
|
||||
* 스토리지 및 Supabase 삭제 처리
|
||||
*/
|
||||
export const handleDeleteStorage = (
|
||||
isCanceled: boolean,
|
||||
updatedTransactions: Transaction[],
|
||||
id: string,
|
||||
user: any,
|
||||
transactionToDelete: Transaction,
|
||||
pendingDeletionRef: MutableRefObject<Set<string>>
|
||||
) => {
|
||||
try {
|
||||
if (isCanceled) {
|
||||
console.log('백그라운드 작업이 취소되었습니다.');
|
||||
return;
|
||||
}
|
||||
|
||||
// 로컬 스토리지 업데이트
|
||||
saveTransactionsToStorage(updatedTransactions);
|
||||
|
||||
// Supabase 업데이트
|
||||
if (user) {
|
||||
deleteTransactionFromSupabase(user, id)
|
||||
.then(() => {
|
||||
if (!isCanceled) {
|
||||
console.log('Supabase 트랜잭션 삭제 성공');
|
||||
// 성공 로그만 추가, UI 업데이트는 이미 수행됨
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Supabase 삭제 오류:', error);
|
||||
handleDeleteError(error, isCanceled, id, transactionToDelete, pendingDeletionRef);
|
||||
})
|
||||
.finally(() => {
|
||||
if (!isCanceled) {
|
||||
// 작업 완료 후 보류 중인 삭제 목록에서 제거
|
||||
pendingDeletionRef.current?.delete(id);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// 사용자 정보 없을 경우 목록에서 제거
|
||||
pendingDeletionRef.current?.delete(id);
|
||||
}
|
||||
} catch (storageError) {
|
||||
console.error('스토리지 작업 중 오류:', storageError);
|
||||
pendingDeletionRef.current?.delete(id);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 삭제 오류 처리
|
||||
*/
|
||||
export const handleDeleteError = (
|
||||
error: any,
|
||||
isCanceled: boolean,
|
||||
id: string,
|
||||
transactionToDelete: Transaction,
|
||||
pendingDeletionRef: MutableRefObject<Set<string>>
|
||||
) => {
|
||||
if (!isCanceled) {
|
||||
console.error('삭제 작업 중 오류 발생:', error);
|
||||
// 작업 완료 표시
|
||||
pendingDeletionRef.current?.delete(id);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user