|
| 1 | +import React, { useMemo, useEffect, useState, useCallback } from 'react'; |
| 2 | +import DataTable, { |
| 3 | + TableContainer, |
| 4 | + Table, |
| 5 | + TableHead, |
| 6 | + TableRow, |
| 7 | + TableHeader, |
| 8 | + TableBody, |
| 9 | + TableCell, |
| 10 | + TableToolbar, |
| 11 | + TableToolbarContent, |
| 12 | +} from 'carbon-components-react/es/components/DataTable'; |
| 13 | +import DataTableSkeleton from 'carbon-components-react/es/components/DataTableSkeleton'; |
| 14 | +import Pagination from 'carbon-components-react/es/components/Pagination'; |
| 15 | +import Search from 'carbon-components-react/es/components/Search'; |
| 16 | +import { useLayoutType, useConfig, usePagination, ConfigurableLink } from '@openmrs/esm-framework'; |
| 17 | +import { ActiveVisitRow, fetchActiveVisits } from './active-visits.resource'; |
| 18 | +import styles from './active-visits.scss'; |
| 19 | +import { useTranslation } from 'react-i18next'; |
| 20 | +import dayjs from 'dayjs'; |
| 21 | + |
| 22 | +const headerData = [ |
| 23 | + { |
| 24 | + id: 0, |
| 25 | + header: 'Visit Time', |
| 26 | + key: 'visitStartTime', |
| 27 | + }, |
| 28 | + { |
| 29 | + id: 1, |
| 30 | + header: 'ID Number', |
| 31 | + key: 'IDNumber', |
| 32 | + }, |
| 33 | + { |
| 34 | + id: 2, |
| 35 | + header: 'Name', |
| 36 | + key: 'name', |
| 37 | + }, |
| 38 | + { |
| 39 | + id: 3, |
| 40 | + header: 'Gender', |
| 41 | + key: 'gender', |
| 42 | + }, |
| 43 | + { |
| 44 | + id: 4, |
| 45 | + header: 'Age', |
| 46 | + key: 'age', |
| 47 | + }, |
| 48 | + { |
| 49 | + id: 5, |
| 50 | + header: 'Visit Type', |
| 51 | + key: 'visitType', |
| 52 | + }, |
| 53 | +]; |
| 54 | + |
| 55 | +function formatDatetime(startDatetime) { |
| 56 | + const todayDate = dayjs(); |
| 57 | + const today = |
| 58 | + dayjs(startDatetime).get('date') === todayDate.get('date') && |
| 59 | + dayjs(startDatetime).get('month') === todayDate.get('month') && |
| 60 | + dayjs(startDatetime).get('year') === todayDate.get('year'); |
| 61 | + if (today) { |
| 62 | + return `Today - ${dayjs(startDatetime).format('HH:mm')}`; |
| 63 | + } else { |
| 64 | + return dayjs(startDatetime).format("DD MMM 'YY - HH:mm"); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +const ActiveVisitsTable = (props) => { |
| 69 | + const { t } = useTranslation(); |
| 70 | + const layout = useLayoutType(); |
| 71 | + const desktopView = layout === 'desktop'; |
| 72 | + const config = useConfig(); |
| 73 | + const [currentPageSize, setPageSize] = useState(config?.activeVisits?.pageSize ?? 10); |
| 74 | + const pageSizes = config?.activeVisits?.pageSizes ?? [10, 20, 50]; |
| 75 | + const [loading, setLoading] = useState(true); |
| 76 | + const [activeVisits, setActiveVisits] = useState<ActiveVisitRow[]>([]); |
| 77 | + const [searchString, setSearchString] = useState(''); |
| 78 | + |
| 79 | + const searchResults = useMemo(() => { |
| 80 | + if (searchString && searchString.trim() !== '') { |
| 81 | + const search = searchString.toLowerCase(); |
| 82 | + return activeVisits.filter((activeVisitRow) => |
| 83 | + Object.keys(activeVisitRow).some((header) => { |
| 84 | + if (header === 'patientUuid') { |
| 85 | + return false; |
| 86 | + } |
| 87 | + return `${activeVisitRow[header]}`.toLowerCase().includes(search); |
| 88 | + }), |
| 89 | + ); |
| 90 | + } else { |
| 91 | + return activeVisits; |
| 92 | + } |
| 93 | + }, [searchString, activeVisits]); |
| 94 | + const { goTo, currentPage, results } = usePagination(searchResults, currentPageSize); |
| 95 | + |
| 96 | + useEffect(() => { |
| 97 | + const activeVisits = fetchActiveVisits().subscribe((data) => { |
| 98 | + const rowData = data.results.map((visit, ind) => ({ |
| 99 | + id: `${ind}`, |
| 100 | + visitStartTime: formatDatetime(visit.startDatetime), |
| 101 | + IDNumber: visit?.patient?.identifiers[0]?.identifier, |
| 102 | + name: visit?.patient?.person?.display, |
| 103 | + gender: visit?.patient?.person?.gender, |
| 104 | + age: visit?.patient?.person?.age, |
| 105 | + visitType: visit?.visitType.display, |
| 106 | + patientUuid: visit?.patient?.uuid, |
| 107 | + })); |
| 108 | + setActiveVisits(rowData); |
| 109 | + setLoading(false); |
| 110 | + }); |
| 111 | + return () => activeVisits.unsubscribe(); |
| 112 | + }, []); |
| 113 | + |
| 114 | + const handleSearch = useCallback((e) => setSearchString(e.target.value), []); |
| 115 | + |
| 116 | + return !loading ? ( |
| 117 | + <div className={styles.activeVisitsContainer}> |
| 118 | + <div className={styles.activeVisitsDetailHeaderContainer}> |
| 119 | + <h4 className={styles.productiveHeading02}>{t('activeVisits', 'Active Visits')}</h4> |
| 120 | + </div> |
| 121 | + <DataTable rows={results} headers={headerData} isSortable> |
| 122 | + {({ rows, headers, getHeaderProps, getTableProps, getBatchActionProps }) => ( |
| 123 | + <TableContainer title="" className={styles.tableContainer}> |
| 124 | + <TableToolbar> |
| 125 | + <TableToolbarContent> |
| 126 | + <Search |
| 127 | + tabIndex={getBatchActionProps().shouldShowBatchActions ? -1 : 0} |
| 128 | + placeholder="Filter table" |
| 129 | + onChange={handleSearch} |
| 130 | + /> |
| 131 | + </TableToolbarContent> |
| 132 | + </TableToolbar> |
| 133 | + <Table {...getTableProps()} useZebraStyles> |
| 134 | + <TableHead> |
| 135 | + <TableRow style={{ height: desktopView ? '2rem' : '3rem' }}> |
| 136 | + {headers.map((header) => ( |
| 137 | + <TableHeader {...getHeaderProps({ header })}>{header.header}</TableHeader> |
| 138 | + ))} |
| 139 | + </TableRow> |
| 140 | + </TableHead> |
| 141 | + <TableBody> |
| 142 | + {rows.map((row, ind) => ( |
| 143 | + <TableRow key={row.id} style={{ height: desktopView ? '2rem' : '3rem' }}> |
| 144 | + {row.cells.map((cell) => ( |
| 145 | + <TableCell key={cell.id}> |
| 146 | + {cell.info.header === 'name' ? ( |
| 147 | + <ConfigurableLink to={`\${openmrsSpaBase}/patient/${results[ind]?.patientUuid}/chart/`}> |
| 148 | + {cell.value} |
| 149 | + </ConfigurableLink> |
| 150 | + ) : ( |
| 151 | + cell.value |
| 152 | + )} |
| 153 | + </TableCell> |
| 154 | + ))} |
| 155 | + </TableRow> |
| 156 | + ))} |
| 157 | + </TableBody> |
| 158 | + </Table> |
| 159 | + {rows.length === 0 && ( |
| 160 | + <p |
| 161 | + style={{ height: desktopView ? '2rem' : '3rem' }} |
| 162 | + className={`${styles.emptyRow} ${styles.bodyLong01}`}> |
| 163 | + {t('noVisitsFound', 'No visits found')} |
| 164 | + </p> |
| 165 | + )} |
| 166 | + <Pagination |
| 167 | + forwardText="" |
| 168 | + backwardText="" |
| 169 | + page={currentPage} |
| 170 | + pageSize={currentPageSize} |
| 171 | + pageSizes={pageSizes} |
| 172 | + totalItems={searchResults.length} |
| 173 | + className={styles.pagination} |
| 174 | + onChange={({ pageSize, page }) => { |
| 175 | + if (pageSize !== currentPageSize) { |
| 176 | + setPageSize(pageSize); |
| 177 | + } |
| 178 | + if (page !== currentPage) { |
| 179 | + goTo(page); |
| 180 | + } |
| 181 | + }} |
| 182 | + /> |
| 183 | + </TableContainer> |
| 184 | + )} |
| 185 | + </DataTable> |
| 186 | + </div> |
| 187 | + ) : ( |
| 188 | + <DataTableSkeleton /> |
| 189 | + ); |
| 190 | +}; |
| 191 | + |
| 192 | +export default ActiveVisitsTable; |
0 commit comments