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 [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 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; } }) ); setUsers(userDetails); 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 searchTermLower = searchTerm.toLowerCase(); return log.explanation.toLowerCase().includes(searchTermLower) || log.transactionType.toLowerCase().includes(searchTermLower) || log.accountId?.toString().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 No | Kullanıcı | İşlem Türü | Miktar | Önceki Bakiye | Yeni Bakiye | Açıklama | Tarih |
---|---|---|---|---|---|---|---|---|
{log.id} | {log.accountId} | {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")} |