diff --git a/client/src/pages/patients/PatientsTable.jsx b/client/src/pages/patients/PatientsTable.jsx index 3e1e111e..efd38ad0 100644 --- a/client/src/pages/patients/PatientsTable.jsx +++ b/client/src/pages/patients/PatientsTable.jsx @@ -1,20 +1,15 @@ import PropTypes from 'prop-types'; -import { useState, useContext } from 'react'; -import { Link } from 'react-router-dom'; -import { Paper, Table, ActionIcon, Menu, Modal, Button } from '@mantine/core'; +import { useState, useContext, useMemo, useCallback } from 'react'; +import { Paper, Table, Modal, Button } from '@mantine/core'; import { useDisclosure } from '@mantine/hooks'; import { useDeletePatient } from './useDeletePatient'; import { notifications } from '@mantine/notifications'; -import { - IconDotsVertical, - IconUser, - IconQrcode, - IconTrash, -} from '@tabler/icons-react'; import classes from './Patients.module.css'; import Context from '../../Context'; +import PatientTableRow from './PatientTableRow'; + const patientTableProps = { headers: PropTypes.arrayOf( PropTypes.shape({ @@ -41,14 +36,16 @@ const patientTableProps = { export default function PatientsTable({ headers, data }) { const [opened, { open, close }] = useDisclosure(false); const [selectedPatient, setSelectedPatient] = useState(null); - const { mutate: deletePatient } = useDeletePatient(); + const { mutateAsync: deletePatient, isPending } = useDeletePatient(); const { user } = useContext(Context); - const showDeleteConfirmation = (patient) => { - setSelectedPatient(patient); - open(); - }; - + const showDeleteConfirmation = useCallback( + (patient) => { + setSelectedPatient(patient); + open(); + }, + [open], + ); const confirmPatientDeletion = async () => { try { await deletePatient(selectedPatient.id); @@ -65,9 +62,40 @@ export default function PatientsTable({ headers, data }) { color: 'red', }); } + if (!isPending) { + setSelectedPatient(null); + close(); + } + }; + + const cancelPatientDeletion = () => { + setSelectedPatient(null); close(); }; + const emptyStateRow = useMemo( + () => ( + + No patients found. + + ), + [headers.length], + ); + + const headersMemo = useMemo(() => headers, [headers]); + + const patientRows = useMemo(() => { + return data?.map((patient) => ( + + )); + }, [data, headers, user.role, showDeleteConfirmation]); + return ( <> @@ -80,74 +108,33 @@ export default function PatientsTable({ headers, data }) { > - {headers.map((header) => ( + {headersMemo.map((header) => ( {header.text} ))} - {data?.length > 0 ? ( - data?.map((patient) => ( - - {headers.map((header) => ( - - {patient[header.key]} - - ))} - - - - - - - - - } - component={Link} - to={`/patients/${patient.id}`} - > - View/Edit - - }> - Reprint QR Code - - {user?.role === 'ADMIN' && ( - } - color="red" - onClick={() => - showDeleteConfirmation({ - id: patient.id, - name: patient.name, - }) - } - > - Delete - - )} - - - - - )) - ) : ( - - - No patients found. - - - )} + {data?.length > 0 ? patientRows : emptyStateRow}

Are you sure you want to delete this patient record?

- -