import React, { useState, useEffect } from 'react'; const BASE_URL = "http://localhost:8000"; const AdminForm = ({ show, onClose, title }) => { const [users, setUsers] = useState([]); const [formData, setFormData] = useState({ fullName: '', username: '', password: '', // Changed from hashed_password since hashing should be done server-side note: '', role: '', skype: '', email: '', is_active: false, is_admin: false }); const [isLoading, setIsLoading] = useState(false); const [isFormVisible, setIsFormVisible] = useState(true); const [showModal, setShowModal] = useState(false); useEffect(() => { const getAllUsers = async () => { try { const token = localStorage.getItem('token'); // Assuming you store the token in localStorage const response = await fetch(`${BASE_URL}/users`, { headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json', } }); const data = await response.json(); setUsers(data.data); } catch (error) { console.error("Error fetching users:", error); } }; if (show) { getAllUsers(); } }, [show]); const handleChange = (e) => { const { name, value, type, checked } = e.target; setFormData(prev => ({ ...prev, [name]: type === 'checkbox' ? checked : value })); }; const handleSubmit = async (e) => { e.preventDefault(); setIsLoading(true); try { const token = localStorage.getItem('token'); const response = await fetch(`${BASE_URL}/register`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, 'Accept': 'application/json', }, body: JSON.stringify({ ...formData, password: formData.password // Send as password, not hashed_password }) }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); setUsers(prev => [...prev, data]); setIsFormVisible(false); setShowModal(true); } catch (error) { console.error("Error saving data:", error); // You might want to show an error message to the user here } finally { setIsLoading(false); } }; const handleCloseModal = () => { setShowModal(false); window.location.reload(); }; if (!show) return null; return (

{title}

{/* Basic Information */}
{/* Additional Information */}

Please fill in all required information

Thank you for your cooperation

DemSettle ®

{showModal && (

Admin Successfully Added

)}
); }; export default AdminForm;