import React, { useState, useEffect } from 'react'; import axios from 'axios'; const BASE_URL = "http://localhost:8000"; const InvestmentLogsTable = () => { const [logs, setLogs] = useState([]); const [users, setUsers] = useState({}); const [investments, setInvestments] = 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 fetchInvestmentDetails = async (investmentId) => { const token = localStorage.getItem('token'); try { const response = await axios.get(`${BASE_URL}/investment/${investmentId}`, { headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json' } }); return response.data; } catch (error) { console.error(`Error fetching investment ${investmentId}:`, error); return null; } }; const fetchLogs = async () => { const token = localStorage.getItem('token'); try { setLoading(true); const response = await axios.get(`${BASE_URL}/investment/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 investment details for each unique investmentId const uniqueInvestmentIds = [...new Set(sortedLogs.map(log => log.investmentId))]; const investmentDetails = {}; await Promise.all( uniqueInvestmentIds.map(async (investmentId) => { const investment = await fetchInvestmentDetails(investmentId); if (investment) { investmentDetails[investmentId] = investment; } }) ); setUsers(userDetails); setInvestments(investmentDetails); setLogs(sortedLogs); setError(null); } catch (error) { console.error("Error fetching logs:", error); setError("Failed to load investment logs. Please try again later."); } finally { setLoading(false); } }; useEffect(() => { fetchLogs(); }, []); const filteredLogs = logs.filter(log => { const userName = users[log.userId]?.username || ''; const investmentInfo = investments[log.investmentId]; const searchTermLower = searchTerm.toLowerCase(); return log.note.toLowerCase().includes(searchTermLower) || userName.toLowerCase().includes(searchTermLower) || investmentInfo?.accountName?.toLowerCase().includes(searchTermLower) || investmentInfo?.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 | Yatırım ID | Hesap Adı | Hesap No | Kullanıcı | Önceki Durum | Yeni Durum | Miktar | İşlem Sonucu | Not | Tarih |
---|---|---|---|---|---|---|---|---|---|---|
{log.id} | {log.investmentId} | {investments[log.investmentId]?.accountName || 'N/A'} | {investments[log.investmentId]?.accountNumber || '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")} |