import React, { useState, useEffect } from 'react'; import axios from 'axios'; const BASE_URL = "http://localhost:8000"; const InvestmentTable = () => { const [investments, setInvestments] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [searchTerm, setSearchTerm] = useState(''); const [refs, setRefs] = useState({}); const [users, setUsers] = useState({}); const [updateLoading, setUpdateLoading] = useState(null); const fetchInvestments = async () => { const token = localStorage.getItem('token'); try { setLoading(true); const response = await axios.get(`${BASE_URL}/investment/`, { headers: { 'Authorization': `Bearer ${token}` } }); const sortedInvestments = response.data.data.sort((a, b) => new Date(b.transactionDate) - new Date(a.transactionDate) ); setInvestments(sortedInvestments); setError(null); } catch (error) { console.error("Error fetching investments:", error); setError("Failed to load investments. Please try again later."); } finally { setLoading(false); } }; const fetchRefs = async () => { const token = localStorage.getItem('token'); try { const response = await axios.get(`${BASE_URL}/refs/`, { headers: { 'Authorization': `Bearer ${token}` } }); const refsMap = {}; response.data.forEach(ref => { refsMap[ref.refId] = ref.refName; }); setRefs(refsMap); } catch (error) { console.error("Error fetching refs:", error); } }; const fetchUsers = async () => { const token = localStorage.getItem('token'); try { const response = await axios.get(`${BASE_URL}/users`, { headers: { 'Authorization': `Bearer ${token}` } }); const usersMap = {}; response.data.data.forEach(user => { usersMap[user.id] = user.username; }); setUsers(usersMap); } catch (error) { console.error("Error fetching users:", error); } }; useEffect(() => { fetchInvestments(); fetchRefs(); fetchUsers(); }, []); const handleTakeOwnership = async (investmentId) => { const token = localStorage.getItem('token'); try { await axios.post(`${BASE_URL}/investment/${investmentId}/take-ownership`, null, { headers: { 'Authorization': `Bearer ${token}` } }); await fetchInvestments(); } catch (error) { console.error("Error taking ownership:", error); throw error; } }; const handleStatusUpdate = async (investmentId, newStatus) => { const token = localStorage.getItem('token'); setUpdateLoading(investmentId); try { // First take ownership if not already owned const investment = investments.find(inv => inv.id === investmentId); if (!investment.userID) { await handleTakeOwnership(investmentId); } // Then update the status await axios.patch(`${BASE_URL}/investment/${investmentId}`, { investmentStatus: newStatus, note: newStatus === 'Onaylandı' ? 'Yatırım onaylandı' : 'Yatırım iptal edildi', resultDate: new Date().toISOString() }, { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` } }); await fetchInvestments(); } catch (error) { console.error("Error updating status:", error); setError(error.response?.data?.detail || "Failed to update investment status"); } finally { setUpdateLoading(null); } }; const filteredInvestments = investments.filter(investment => { const searchLower = searchTerm.toLowerCase(); return ( investment.id.toString().includes(searchLower) || (refs[investment.refId] || '').toLowerCase().includes(searchLower) || investment.fullName.toLowerCase().includes(searchLower) || investment.username.toLowerCase().includes(searchLower) || investment.accountNumber.includes(searchLower) ); }); if (loading) { return (
Hata!
{error}
ID | Site | Adı Soyadı | Miktar | Kullanıcı Adı | Yöntem | Hesap Adı | Hesap No | İşlem Tarihi | Sonuç Tarihi | İşlemi Yapan | Durum | İşlemler |
---|---|---|---|---|---|---|---|---|---|---|---|---|
{investment.id} | {refs[investment.refId] || '-'} | {investment.fullName} | ₺{investment.balance.toLocaleString()} | {investment.username} | {investment.method} | {investment.accountName} | {investment.accountNumber} | {new Date(investment.transactionDate).toLocaleString("tr-TR")} | {investment.resultDate ? new Date(investment.resultDate).toLocaleString("tr-TR") : '-'} | {users[investment.userID] || '-'} | {investment.investmentStatus} |
{investment.investmentStatus === 'Beklemede' && (
<>
{!investment.userID && (
)}
{investment.userID && (
<>
>
)}
>
)}
|