import React, { useEffect, useState } from 'react'; import { HiBars3BottomLeft } from "react-icons/hi2"; import { FaCheck } from "react-icons/fa6"; import '../App.css'; import InvestmentForm from './YatırımForm'; import axios from 'axios'; import DateInput from './DateInput'; const BASE_URL = "http://localhost:8000/api"; const YatırımContent = () => { const [showFilterMenu, setShowFilterMenu] = useState(true); const [showModal, setShowModal] = useState(false); const [bankAccounts, setBankAccounts] = useState([]); const [filteredAccounts, setFilteredAccounts] = useState([]); const [selectedAccount, setSelectedAccount] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [minAmount, setMinAmount] = useState(''); const [maxAmount, setMaxAmount] = useState(''); const [selectedDate, setSelectedDate] = useState(''); const [autoUpdate, setAutoUpdate] = useState(true); useEffect(() => { fetchBankAccounts(); if (autoUpdate) { const interval = setInterval(fetchBankAccounts, 30000); // Auto-update every 30 seconds return () => clearInterval(interval); } }, [autoUpdate]); useEffect(() => { applyFilters(); }, [bankAccounts, selectedAccount, minAmount, maxAmount, selectedDate]); const fetchBankAccounts = async () => { const token = localStorage.getItem('token'); setLoading(true); try { const response = await axios.get(`${BASE_URL}/bank/accounts`, { headers: { 'Authorization': `Bearer ${token}` } }); const accounts = response.data.data; setBankAccounts(accounts); setError(null); } catch (err) { setError('Banka hesapları yüklenirken bir hata oluştu'); console.error('Error fetching bank accounts:', err); } finally { setLoading(false); } }; const applyFilters = () => { let filtered = [...bankAccounts]; // Filter by selected account if (selectedAccount) { filtered = filtered.filter(account => account.accountId === selectedAccount); } // Filter by amount range if (minAmount !== '') { filtered = filtered.filter(account => account.accountBalance >= parseFloat(minAmount)); } if (maxAmount !== '') { filtered = filtered.filter(account => account.accountBalance <= parseFloat(maxAmount)); } // Filter by date if (selectedDate) { const filterDate = new Date(selectedDate).toISOString().split('T')[0]; filtered = filtered.filter(account => { const accountDate = new Date(account.created_at).toISOString().split('T')[0]; return accountDate === filterDate; }); } setFilteredAccounts(filtered); }; const toggleFilterMenu = () => { setShowFilterMenu(prevState => !prevState); }; const toggleAutoUpdate = () => { setAutoUpdate(prev => !prev); }; const handleDateChange = (date) => { setSelectedDate(date); }; const clearFilters = () => { setSelectedAccount(''); setMinAmount(''); setMaxAmount(''); setSelectedDate(''); setFilteredAccounts(bankAccounts); }; return (
{showModal && ( setShowModal(false)} /> )}
{!showFilterMenu && (
Banka Hesapları
{error && ( {error} )}

Tutar Aralığı

setMinAmount(e.target.value)} className="rounded-sm text-center w-28 p-1 text-xs border border-gray-300 focus:outline-none focus:ring-2 focus:ring-emerald-500" /> ve setMaxAmount(e.target.value)} className="rounded-sm w-28 text-center p-1 text-xs border border-gray-300 focus:outline-none focus:ring-2 focus:ring-emerald-500" />
Otomatik Güncelleştirme {autoUpdate ? 'Açık' : 'Kapalı'}
)} {/* Display filtered accounts summary */}
Toplam Hesap: {filteredAccounts.length} {filteredAccounts.length > 0 && ( Toplam Bakiye: {filteredAccounts.reduce((sum, account) => sum + account.accountBalance, 0).toLocaleString()} ₺ )}
); }; export default YatırımContent;