Refactor: Split useTransactions hook
Splits the `useTransactions` hook into smaller, more manageable files for improved code organization and maintainability. No functional changes are included.
This commit is contained in:
32
src/utils/dateUtils.ts
Normal file
32
src/utils/dateUtils.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
// 월 이름 정의
|
||||
export const MONTHS_KR = [
|
||||
'1월', '2월', '3월', '4월', '5월', '6월',
|
||||
'7월', '8월', '9월', '10월', '11월', '12월'
|
||||
];
|
||||
|
||||
// 현재 월 가져오기
|
||||
export const getCurrentMonth = () => {
|
||||
const now = new Date();
|
||||
return MONTHS_KR[now.getMonth()];
|
||||
};
|
||||
|
||||
// 이전 달 가져오기
|
||||
export const getPrevMonth = (currentMonth: string) => {
|
||||
const currentIndex = MONTHS_KR.indexOf(currentMonth);
|
||||
if (currentIndex > 0) {
|
||||
return MONTHS_KR[currentIndex - 1];
|
||||
} else {
|
||||
return MONTHS_KR[MONTHS_KR.length - 1];
|
||||
}
|
||||
};
|
||||
|
||||
// 다음 달 가져오기
|
||||
export const getNextMonth = (currentMonth: string) => {
|
||||
const currentIndex = MONTHS_KR.indexOf(currentMonth);
|
||||
if (currentIndex < MONTHS_KR.length - 1) {
|
||||
return MONTHS_KR[currentIndex + 1];
|
||||
} else {
|
||||
return MONTHS_KR[0];
|
||||
}
|
||||
};
|
||||
66
src/utils/storageUtils.ts
Normal file
66
src/utils/storageUtils.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
import { Transaction } from '@/components/TransactionCard';
|
||||
|
||||
// 트랜잭션 데이터 불러오기
|
||||
export const loadTransactionsFromStorage = (): Transaction[] => {
|
||||
const localData = localStorage.getItem('transactions');
|
||||
if (localData) {
|
||||
return JSON.parse(localData) as Transaction[];
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
||||
// 기본 샘플 데이터 생성
|
||||
export const createSampleTransactions = (selectedMonth: string): Transaction[] => {
|
||||
return [{
|
||||
id: '1',
|
||||
title: '식료품 구매',
|
||||
amount: 25000,
|
||||
date: `${selectedMonth} 25일, 12:30 PM`,
|
||||
category: '식비',
|
||||
type: 'expense'
|
||||
}, {
|
||||
id: '2',
|
||||
title: '주유소',
|
||||
amount: 50000,
|
||||
date: `${selectedMonth} 24일, 3:45 PM`,
|
||||
category: '교통비',
|
||||
type: 'expense'
|
||||
}, {
|
||||
id: '4',
|
||||
title: '생필품 구매',
|
||||
amount: 35000,
|
||||
date: `${selectedMonth} 18일, 6:00 AM`,
|
||||
category: '생활비',
|
||||
type: 'expense'
|
||||
}, {
|
||||
id: '5',
|
||||
title: '월세',
|
||||
amount: 650000,
|
||||
date: `${selectedMonth} 15일, 10:00 AM`,
|
||||
category: '생활비',
|
||||
type: 'expense'
|
||||
}, {
|
||||
id: '6',
|
||||
title: '식당',
|
||||
amount: 15500,
|
||||
date: `${selectedMonth} 12일, 2:15 PM`,
|
||||
category: '식비',
|
||||
type: 'expense'
|
||||
}];
|
||||
};
|
||||
|
||||
// 트랜잭션 데이터 저장하기
|
||||
export const saveTransactionsToStorage = (transactions: Transaction[]): void => {
|
||||
localStorage.setItem('transactions', JSON.stringify(transactions));
|
||||
};
|
||||
|
||||
// 예산 불러오기
|
||||
export const loadBudgetFromStorage = (): number => {
|
||||
const budgetData = localStorage.getItem('budget');
|
||||
if (budgetData) {
|
||||
const parsedBudget = JSON.parse(budgetData);
|
||||
return parsedBudget.total || 1000000;
|
||||
}
|
||||
return 1000000; // 기본 예산
|
||||
};
|
||||
91
src/utils/supabaseTransactionUtils.ts
Normal file
91
src/utils/supabaseTransactionUtils.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
|
||||
import { supabase } from '@/lib/supabase';
|
||||
import { Transaction } from '@/components/TransactionCard';
|
||||
import { isSyncEnabled } from '@/utils/syncUtils';
|
||||
import { toast } from '@/hooks/useToast.wrapper';
|
||||
|
||||
// Supabase와 거래 데이터 동기화
|
||||
export const syncTransactionsWithSupabase = async (
|
||||
user: any,
|
||||
transactions: Transaction[]
|
||||
): Promise<Transaction[]> => {
|
||||
if (!user || !isSyncEnabled()) return transactions;
|
||||
|
||||
try {
|
||||
const { data, error } = await supabase
|
||||
.from('transactions')
|
||||
.select('*')
|
||||
.eq('user_id', user.id);
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
if (data && data.length > 0) {
|
||||
// Supabase 데이터 로컬 형식으로 변환
|
||||
const supabaseTransactions = data.map(t => ({
|
||||
id: t.transaction_id || t.id,
|
||||
title: t.title,
|
||||
amount: t.amount,
|
||||
date: t.date,
|
||||
category: t.category,
|
||||
type: t.type
|
||||
}));
|
||||
|
||||
// 로컬 데이터와 병합 (중복 ID 제거)
|
||||
const mergedTransactions = [...transactions];
|
||||
|
||||
supabaseTransactions.forEach(newTx => {
|
||||
const existingIndex = mergedTransactions.findIndex(t => t.id === newTx.id);
|
||||
if (existingIndex >= 0) {
|
||||
mergedTransactions[existingIndex] = newTx;
|
||||
} else {
|
||||
mergedTransactions.push(newTx);
|
||||
}
|
||||
});
|
||||
|
||||
return mergedTransactions;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Supabase 동기화 오류:', err);
|
||||
}
|
||||
|
||||
return transactions;
|
||||
};
|
||||
|
||||
// Supabase에 트랜잭션 업데이트
|
||||
export const updateTransactionInSupabase = async (
|
||||
user: any,
|
||||
transaction: Transaction
|
||||
): Promise<void> => {
|
||||
if (!user || !isSyncEnabled()) return;
|
||||
|
||||
try {
|
||||
await supabase.from('transactions')
|
||||
.upsert({
|
||||
user_id: user.id,
|
||||
title: transaction.title,
|
||||
amount: transaction.amount,
|
||||
date: transaction.date,
|
||||
category: transaction.category,
|
||||
type: transaction.type,
|
||||
transaction_id: transaction.id
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Supabase 업데이트 오류:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// Supabase에서 트랜잭션 삭제
|
||||
export const deleteTransactionFromSupabase = async (
|
||||
user: any,
|
||||
transactionId: string
|
||||
): Promise<void> => {
|
||||
if (!user || !isSyncEnabled()) return;
|
||||
|
||||
try {
|
||||
await supabase.from('transactions')
|
||||
.delete()
|
||||
.eq('transaction_id', transactionId);
|
||||
} catch (error) {
|
||||
console.error('Supabase 삭제 오류:', error);
|
||||
}
|
||||
};
|
||||
31
src/utils/transactionUtils.ts
Normal file
31
src/utils/transactionUtils.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
import { Transaction } from '@/components/TransactionCard';
|
||||
|
||||
// 월별로 트랜잭션 필터링
|
||||
export const filterTransactionsByMonth = (
|
||||
transactions: Transaction[],
|
||||
selectedMonth: string
|
||||
): Transaction[] => {
|
||||
return transactions.filter(transaction =>
|
||||
transaction.date.includes(selectedMonth) && transaction.type === 'expense'
|
||||
);
|
||||
};
|
||||
|
||||
// 검색어로 트랜잭션 필터링
|
||||
export const filterTransactionsByQuery = (
|
||||
transactions: Transaction[],
|
||||
searchQuery: string
|
||||
): Transaction[] => {
|
||||
if (!searchQuery.trim()) return transactions;
|
||||
|
||||
const query = searchQuery.toLowerCase().trim();
|
||||
return transactions.filter(transaction =>
|
||||
transaction.title.toLowerCase().includes(query) ||
|
||||
transaction.category.toLowerCase().includes(query)
|
||||
);
|
||||
};
|
||||
|
||||
// 총 지출 금액 계산
|
||||
export const calculateTotalExpenses = (transactions: Transaction[]): number => {
|
||||
return transactions.reduce((sum, t) => sum + t.amount, 0);
|
||||
};
|
||||
Reference in New Issue
Block a user