import React, { useState, useEffect } from 'react'; import axios from 'axios'; const BASE_URL = "http://localhost:8000"; const WithdrawalLogsTable = () => { const [logs, setLogs] = useState([]); const [users, setUsers] = useState({}); const [withdrawals, setWithdrawals] = useState({}); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [searchTerm, setSearchTerm] = useState(''); const fetchUserDetails = async (userId) => { const token = localStorage.getItem('token'); try { const response = await axios.get(`${BASE_URL}/users/${userId}`, { headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json' } }); return response.data; } catch (error) { console.error(`Error fetching user ${userId}:`, error); return null; } }; const fetchWithdrawalDetails = async (withdrawalId) => { const token = localStorage.getItem('token'); try { const response = await axios.get(`${BASE_URL}/withdrawal/${withdrawalId}`, { headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json' } }); return response.data; } catch (error) { console.error(`Error fetching withdrawal ${withdrawalId}:`, error); return null; } }; const fetchLogs = async () => { const token = localStorage.getItem('token'); try { setLoading(true); const response = await axios.get(`${BASE_URL}/withdrawal/logs`, { headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json' } }); const sortedLogs = response.data.data.sort((a, b) => new Date(b.created_at) - new Date(a.created_at) ); // Fetch user details for each unique userId const uniqueUserIds = [...new Set(sortedLogs.map(log => log.userId))]; const userDetails = {}; await Promise.all( uniqueUserIds.map(async (userId) => { const user = await fetchUserDetails(userId); if (user) { userDetails[userId] = user; } }) ); // Fetch withdrawal details for each unique withdrawalId const uniqueWithdrawalIds = [...new Set(sortedLogs.map(log => log.withdrawalId))]; const withdrawalDetails = {}; await Promise.all( uniqueWithdrawalIds.map(async (withdrawalId) => { const withdrawal = await fetchWithdrawalDetails(withdrawalId); if (withdrawal) { withdrawalDetails[withdrawalId] = withdrawal; } }) ); setUsers(userDetails); setWithdrawals(withdrawalDetails); setLogs(sortedLogs); setError(null); } catch (error) { console.error("Error fetching logs:", error); setError("Failed to load withdrawal logs. Please try again later."); } finally { setLoading(false); } }; useEffect(() => { fetchLogs(); }, []); const filteredLogs = logs.filter(log => { const userName = users[log.userId]?.username || ''; const withdrawalInfo = withdrawals[log.withdrawalId]; const searchTermLower = searchTerm.toLowerCase(); return log.note.toLowerCase().includes(searchTermLower) || userName.toLowerCase().includes(searchTermLower) || withdrawalInfo?.accountName?.toLowerCase().includes(searchTermLower) || withdrawalInfo?.accountNumber?.toLowerCase().includes(searchTermLower) || log.transactionResult.toLowerCase().includes(searchTermLower); }); const getStatusTypeClass = (status) => { switch (status) { case 'Onaylandı': return 'bg-green-100 text-green-800'; case 'İptal': return 'bg-red-100 text-red-800'; default: return 'bg-yellow-100 text-yellow-800'; } }; const getTransactionResultClass = (result) => { switch (result) { case 'Tamamlandı': return 'bg-green-100 text-green-800'; case 'Başarısız': return 'bg-red-100 text-red-800'; case 'İptal Edildi': return 'bg-yellow-100 text-yellow-800'; default: return 'bg-gray-100 text-gray-800'; } }; if (loading) { return (
Hata!
{error}
ID | Çekim ID | Hesap Adı | Hesap No | Hedef Hesap No | Kullanıcı | Önceki Durum | Yeni Durum | Miktar | İşlem Sonucu | Not | Tarih |
---|---|---|---|---|---|---|---|---|---|---|---|
{log.id} | {log.withdrawalId} | {withdrawals[log.withdrawalId]?.accountName || 'N/A'} | {withdrawals[log.withdrawalId]?.accountNumber || 'N/A'} | {withdrawals[log.withdrawalId]?.targetAccountNumber || 'N/A'} | {users[log.userId]?.username || 'Unknown User'} | {log.previousStatus} | {log.newStatus} | {log.amount.toLocaleString('tr-TR')} ₺ | {log.transactionResult} | {log.note} | {new Date(log.created_at).toLocaleString("tr-TR")} |