import React, { useState, useEffect } from 'react'; import axios from 'axios'; const BASE_URL = "http://localhost:8000"; const BankLogsTable = () => { const [logs, setLogs] = useState([]); const [users, setUsers] = useState({}); const [accounts, setAccounts] = 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 fetchAccountDetails = async (accountId) => { const token = localStorage.getItem('token'); try { const response = await axios.get(`${BASE_URL}/bank/${accountId}`, { headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json' } }); return response.data; } catch (error) { console.error(`Error fetching account ${accountId}:`, error); return null; } }; const fetchLogs = async () => { const token = localStorage.getItem('token'); try { setLoading(true); const response = await axios.get(`${BASE_URL}/bank/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 account details for each unique accountId const uniqueAccountIds = [...new Set(sortedLogs.map(log => log.accountId))]; const accountDetails = {}; await Promise.all( uniqueAccountIds.map(async (accountId) => { const account = await fetchAccountDetails(accountId); if (account) { accountDetails[accountId] = account; } }) ); setUsers(userDetails); setAccounts(accountDetails); setLogs(sortedLogs); setError(null); } catch (error) { console.error("Error fetching logs:", error); setError("Failed to load bank logs. Please try again later."); } finally { setLoading(false); } }; useEffect(() => { fetchLogs(); }, []); const filteredLogs = logs.filter(log => { const userName = users[log.userId]?.username || ''; const accountName = accounts[log.accountId]?.accountName || ''; const searchTermLower = searchTerm.toLowerCase(); return log.explanation.toLowerCase().includes(searchTermLower) || log.transactionType.toLowerCase().includes(searchTermLower) || accountName.toLowerCase().includes(searchTermLower) || userName.toLowerCase().includes(searchTermLower); }); const getTransactionTypeClass = (type) => { return type === 'Ekle' ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'; }; if (loading) { return (
Hata!
{error}
ID | Hesap Adı | Hesap No | Kullanıcı | İşlem Türü | Miktar | Önceki Bakiye | Yeni Bakiye | Açıklama | Tarih |
---|---|---|---|---|---|---|---|---|---|
{log.id} | {accounts[log.accountId]?.accountName || 'Unknown Account'} | {accounts[log.accountId]?.accountNumber || 'N/A'} | {users[log.userId]?.username || 'Unknown User'} | {log.transactionType} | {log.amount.toLocaleString('tr-TR')} ₺ | {log.previousBalance.toLocaleString('tr-TR')} ₺ | {log.newBalance.toLocaleString('tr-TR')} ₺ | {log.explanation} | {new Date(log.created_at).toLocaleString("tr-TR")} |