Refactor useBudgetState into smaller files
Refactors the `useBudgetState.ts` file into smaller, more manageable files to improve code organization and maintainability. No functionality is changed.
This commit is contained in:
85
src/contexts/budget/hooks/useTransactionState.ts
Normal file
85
src/contexts/budget/hooks/useTransactionState.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { Transaction } from '../types';
|
||||
import {
|
||||
loadTransactionsFromStorage,
|
||||
saveTransactionsToStorage,
|
||||
clearAllTransactions
|
||||
} from '../storageUtils';
|
||||
import { toast } from '@/components/ui/use-toast';
|
||||
|
||||
// 트랜잭션 상태 관리 훅
|
||||
export const useTransactionState = () => {
|
||||
const [transactions, setTransactions] = useState<Transaction[]>([]);
|
||||
|
||||
// 초기 트랜잭션 로드
|
||||
useEffect(() => {
|
||||
const loadTransactions = () => {
|
||||
const storedTransactions = loadTransactionsFromStorage();
|
||||
setTransactions(storedTransactions);
|
||||
};
|
||||
|
||||
loadTransactions();
|
||||
|
||||
// 이벤트 리스너를 추가하여 다른 컴포넌트에서 변경 시 업데이트
|
||||
window.addEventListener('storage', loadTransactions);
|
||||
return () => {
|
||||
window.removeEventListener('storage', loadTransactions);
|
||||
};
|
||||
}, []);
|
||||
|
||||
// 트랜잭션 추가 함수
|
||||
const addTransaction = useCallback((newTransaction: Transaction) => {
|
||||
setTransactions(prev => {
|
||||
const updated = [newTransaction, ...prev];
|
||||
saveTransactionsToStorage(updated);
|
||||
return updated;
|
||||
});
|
||||
}, []);
|
||||
|
||||
// 트랜잭션 업데이트 함수
|
||||
const updateTransaction = useCallback((updatedTransaction: Transaction) => {
|
||||
setTransactions(prev => {
|
||||
const updated = prev.map(transaction =>
|
||||
transaction.id === updatedTransaction.id ? updatedTransaction : transaction
|
||||
);
|
||||
saveTransactionsToStorage(updated);
|
||||
return updated;
|
||||
});
|
||||
|
||||
// 로컬 이벤트 발생 (다른 컴포넌트에서 변경 감지하도록)
|
||||
window.dispatchEvent(new Event('transactionUpdated'));
|
||||
}, []);
|
||||
|
||||
// 트랜잭션 삭제 함수
|
||||
const deleteTransaction = useCallback((transactionId: string) => {
|
||||
setTransactions(prev => {
|
||||
const updated = prev.filter(transaction => transaction.id !== transactionId);
|
||||
saveTransactionsToStorage(updated);
|
||||
return updated;
|
||||
});
|
||||
|
||||
toast({
|
||||
title: "지출이 삭제되었습니다",
|
||||
description: "지출 항목이 성공적으로 삭제되었습니다.",
|
||||
});
|
||||
|
||||
// 로컬 이벤트 발생 (다른 컴포넌트에서 변경 감지하도록)
|
||||
window.dispatchEvent(new Event('transactionDeleted'));
|
||||
}, []);
|
||||
|
||||
// 트랜잭션 초기화 함수
|
||||
const resetTransactions = useCallback(() => {
|
||||
clearAllTransactions();
|
||||
setTransactions([]);
|
||||
console.log('모든 트랜잭션이 초기화되었습니다.');
|
||||
}, []);
|
||||
|
||||
return {
|
||||
transactions,
|
||||
addTransaction,
|
||||
updateTransaction,
|
||||
deleteTransaction,
|
||||
resetTransactions
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user