import React, { useState, useEffect } from 'react'; import axios from 'axios'; const BASE_URL = "http://localhost:8000"; const BankaUpdateForm = ({ show, onClose, title, accountId }) => { const [formData, setFormData] = useState({ bankName: '', accountName: '', accountNumber: '', accountExplanation: '', isActive: false, isBlocked: false, isInvest: false, isPull: false, minInvest: '', maxInvest: '', minPull: '', maxPull: '', maxTransfer: '', maxAmount: '' }); const [showModal, setShowModal] = useState(false); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); useEffect(() => { if (accountId) { fetchAccountDetails(); } }, [accountId]); const fetchAccountDetails = async () => { setIsLoading(true); setError(null); try { const token = localStorage.getItem('token'); if (!token) { throw new Error('No authentication token found'); } const response = await axios.get(`${BASE_URL}/bank/accounts`, { headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json', } }); const account = response.data.data.find(acc => acc.accountId === accountId); if (!account) { throw new Error('Account not found'); } setFormData({ bankName: account.bankName || '', accountName: account.accountName || '', accountNumber: account.accountNumber || '', accountExplanation: account.accountExplanation || '', isActive: Boolean(account.isActive), isBlocked: Boolean(account.isBlocked), isInvest: Boolean(account.isInvest), isPull: Boolean(account.isPull), minInvest: account.minInvest ? Number(account.minInvest) : '', maxInvest: account.maxInvest ? Number(account.maxInvest) : '', minPull: account.minPull ? Number(account.minPull) : '', maxPull: account.maxPull ? Number(account.maxPull) : '', maxTransfer: account.maxTransfer ? Number(account.maxTransfer) : '', maxAmount: account.maxAmount ? Number(account.maxAmount) : '' }); } catch (error) { console.error('Error fetching account details:', error); setError(error.message || 'Failed to fetch account details'); } finally { setIsLoading(false); } }; const handleUpdate = async (e) => { e.preventDefault(); setIsLoading(true); setError(null); try { const token = localStorage.getItem('token'); if (!token) { throw new Error('No authentication token found'); } const updateData = { bankName: formData.bankName || null, accountName: formData.accountName || null, accountNumber: formData.accountNumber ? String(formData.accountNumber) : null, accountExplanation: formData.accountExplanation || null, isActive: Boolean(formData.isActive), isBlocked: Boolean(formData.isBlocked), isInvest: Boolean(formData.isInvest), isPull: Boolean(formData.isPull), maxInvest: formData.maxInvest ? Number(formData.maxInvest) : null, minInvest: formData.minInvest ? Number(formData.minInvest) : null, maxPull: formData.maxPull ? Number(formData.maxPull) : null, minPull: formData.minPull ? Number(formData.minPull) : null, maxTransfer: formData.maxTransfer ? Number(formData.maxTransfer) : null, maxAmount: formData.maxAmount ? Number(formData.maxAmount) : null }; const cleanedData = Object.fromEntries( Object.entries(updateData).filter(([_, value]) => value !== null) ); await axios.patch( `${BASE_URL}/bank/${accountId}`, cleanedData, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, 'Accept': 'application/json', } } ); setShowModal(true); } catch (error) { console.error('Error updating account:', error); setError(error.response?.data?.detail || 'Failed to update account'); } finally { setIsLoading(false); } }; const handleInputChange = (e) => { const { name, value, type, checked } = e.target; setFormData(prev => ({ ...prev, [name]: type === 'checkbox' ? checked : type === 'number' ? (value === '' ? '' : Number(value)) : value })); }; const handleCloseModal = () => { setShowModal(false); onClose(); window.location.reload(); }; if (!show) return null; if (isLoading) { return (

Loading...

); } return (

{title}

{error && (
Error! {error}
)}
{/* Bank Information */}
{/* Account Settings */}

Please fill in all required information

Thank you for your cooperation

DemSettle ®

{showModal && (

Bank Account Successfully Updated

)}
); }; export default BankaUpdateForm;