import React, { useState, useEffect } from 'react'; import { Link } from 'react-router-dom'; import { patientService } from '../../services/patientService'; import Loading from '../../components/Loading'; import ErrorAlert from '../../components/ErrorAlert'; function PatientList() { const user = JSON.parse(localStorage.getItem('user') || '{}'); const isAdmin = user.role === 'ADMIN'; const [patients, setPatients] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [searchTerm, setSearchTerm] = useState(''); useEffect(() => { fetchPatients(); }, []); const fetchPatients = async () => { try { setLoading(true); const response = await patientService.getAllPatients(); setPatients(response.data); } catch (err) { setError('Failed to fetch patients'); console.error(err); } finally { setLoading(false); } }; const filteredPatients = patients.filter(patient => patient.firstName.toLowerCase().includes(searchTerm.toLowerCase()) || patient.lastName.toLowerCase().includes(searchTerm.toLowerCase()) || patient.embg.includes(searchTerm) ); if (loading) return ; return (

Patients

{isAdmin && ( e.currentTarget.style.background = '#93c5fd'} onMouseLeave={(e) => e.currentTarget.style.background = '#bfdbfe'}> Add Patient )}
{error && setError(null)} />}
setSearchTerm(e.target.value)} />
{filteredPatients.map(patient => ( ))}
Name EMBG Email Phone Actions
{patient.firstName} {patient.lastName} {patient.embg} {patient.emailAddress} {patient.phoneNumber} View
); } export default PatientList;