import React, { useState, useEffect } from 'react'; import { Button, Table, Modal, Form } from 'react-bootstrap'; import 'bootstrap/dist/css/bootstrap.min.css'; import { SERVER_URL } from '../../constants.js'; import { useParams } from "react-router-dom"; import { getToken } from '../../utils/session.js'; const styles = { primaryButton: { backgroundColor: "var(--light-blue)", borderColor: "var(--light-blue)", }, infoButton: { backgroundColor: "var(--light-blue)", color: 'white', borderColor: "var(--light-blue)", }, modalHeader: { backgroundColor: "var(--blue)", color: 'white', }, }; const UserManageScreen = () => { const { tenantCode } = useParams(); const [showModal, setShowModal] = useState(false); const [users, setUsers] = useState([]); const [userEmail, setUserEmail] = useState(''); const [userPassword, setUserPassword] = useState(''); const [selectedUserIndex, setSelectedUserIndex] = useState(null); const [emailError, setEmailError] = useState(''); const [passwordError, setPasswordError] = useState(''); useEffect(() => { if (getToken()) { fetchUsers(); } }, []); const authHeaders = () => { const token = getToken(); return { 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}` } : {}), }; }; const fetchUsers = async () => { try { const requestBody = JSON.stringify({ roleName: 'ROLE_USER' }); const response = await fetch(`${SERVER_URL}/api/v1/admin/${tenantCode}`, { method: 'POST', headers: authHeaders(), body: requestBody }); if (response.ok) { const data = await response.json(); setUsers(data); } else { console.error('Unsuccessful API call'); } } catch (error) { console.error('Error while making API call:', error); } }; const validateEmail = (email) => { const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return re.test(String(email).toLowerCase()); }; const handleAddUser = async () => { const requestBody = { email: userEmail, password: userPassword, roleName: 'ROLE_USER' }; try { const response = await fetch(`${SERVER_URL}/api/v1/admin/${tenantCode}/user`, { method: 'POST', headers: authHeaders(), body: JSON.stringify(requestBody) }); if (response.ok) { const data = await response.json(); setUsers([...users, data]); setShowModal(false); setUserEmail(''); setUserPassword(''); } else { console.error('Unsuccessful API call'); } } catch (error) { console.error('Error while making API call:', error); } }; const handleEditUser = async () => { if (!validateEmail(userEmail)) { setEmailError('Invalid email address'); return; } setEmailError(''); try { const updatedUser = { email: userEmail }; const response = await fetch(`${SERVER_URL}/api/v1/admin/${tenantCode}/user/${users[selectedUserIndex].id}`, { method: 'PUT', headers: authHeaders(), body: JSON.stringify(updatedUser), }); if (response.ok) { const updatedUsers = [...users]; updatedUsers[selectedUserIndex].email = userEmail; setUsers(updatedUsers); setShowModal(false); setUserEmail(''); setUserPassword(''); } else { console.error('Unsuccessful API call'); } } catch (error) { console.error('Error while making API call:', error); } }; const handleDeleteUser = async (userId) => { try { const response = await fetch(`${SERVER_URL}/api/v1/admin/${tenantCode}/user/${userId}`, { method: 'DELETE', headers: authHeaders(), }); if (response.ok) { const updatedUsers = users.filter(user => user.id !== userId); setUsers(updatedUsers); } else { console.error('Unsuccessful API call'); } } catch (error) { console.error('Error while making API call:', error); } }; const handleEditClick = (index) => { const user = users[index]; setSelectedUserIndex(index); setUserEmail(user.email); setShowModal(true); }; return (
These accounts have ROLE_USER and cannot log into the admin app. Use Manage administrators to create admin logins.
| ID | Actions | |
|---|---|---|
| {user.id} | {user.email} | {' '} |