import { useEffect, useState, useMemo } from 'react'; import { fetchSyncedPatients } from '../services/api'; import { UserCircle, Mail, Phone, CalendarClock, ShieldCheck, Search } from 'lucide-react'; import { createColumnHelper } from '@tanstack/react-table'; import { Table } from '../components/Table'; import Pagination from '../components/Pagination'; const columnHelper = createColumnHelper(); const columns = [ columnHelper.accessor('full_name', { header: 'Patient', size: 350, cell: info => { const patient = info.row.original; return (
{patient.full_name}
ID: {patient.hospital_patient_id}
); } }), columnHelper.accessor('contact_number', { header: 'Contact', size: 200, cell: info => (
{info.getValue() || 'N/A'}
) }), columnHelper.accessor('email', { header: 'Email', size: 250, cell: info => (
{info.getValue() || 'N/A'}
) }), columnHelper.accessor('created_at', { header: 'Synced At', size: 200, cell: info => (
{new Date(info.getValue()).toLocaleString()}
) }) ]; export default function Patients() { const [patients, setPatients] = useState([]); const [loading, setLoading] = useState(true); const [currentPage, setCurrentPage] = useState(1); const [itemsPerPage, setItemsPerPage] = useState(10); const [searchQuery, setSearchQuery] = useState(''); useEffect(() => { fetchSyncedPatients() .then(data => { setPatients(data); setLoading(false); }) .catch(err => { console.error('Error fetching synced patients:', err); setLoading(false); }); }, []); const filteredPatients = useMemo(() => { const safePatients = Array.isArray(patients) ? patients : []; if (!searchQuery.trim()) return safePatients; const lowerQuery = searchQuery.toLowerCase(); return safePatients.filter(patient => patient && ( (patient.full_name && String(patient.full_name).toLowerCase().includes(lowerQuery)) || (patient.hospital_patient_id && String(patient.hospital_patient_id).toLowerCase().includes(lowerQuery)) || (patient.contact_number && String(patient.contact_number).toLowerCase().includes(lowerQuery)) || (patient.email && String(patient.email).toLowerCase().includes(lowerQuery)) ) ); }, [patients, searchQuery]); if (loading) { return (
); } const totalItems = filteredPatients.length; const totalPages = Math.max(1, Math.ceil(totalItems / itemsPerPage)); const currentData = filteredPatients.slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage); return (

Hospital Patient Directory

Securely Synced via AI Integration

{ setSearchQuery(e.target.value); setCurrentPage(1); // Reset page on search }} placeholder="Search by name, ID or contact..." className="w-full py-2.5 bg-white border border-slate-200 hover:border-slate-300 rounded-xl focus:outline-none focus:ring-4 focus:ring-emerald-500/10 focus:border-emerald-500 shadow-sm placeholder:text-slate-400 transition-all text-slate-800 text-[14px]" style={{ paddingLeft: '44px', paddingRight: '16px' }} />
{filteredPatients.length} Records
{(!Array.isArray(patients) || patients.length === 0) ? (

No Patients Found

No patients have been synced from the hospital yet.

) : filteredPatients.length === 0 ? (

No Matches Found

No patients match your search "{searchQuery}".

) : ( item.id} emptyMessage="No patients found" /> )} {totalItems > 0 && ( setCurrentPage(page)} onItemsPerPageChange={(limit) => { setItemsPerPage(limit); setCurrentPage(1); }} /> )} ); }