import { useState } from 'react'; import { Button, Card, Spinner, Table } from 'react-bootstrap'; import clsx from 'clsx'; import { exportTableToXlsx } from './exportXlsx'; import { formatCell, headerRows, rowValue } from './helpers'; export default function ReportSection({ title, table, loading, error, filename, stickyFirstColumn }) { const [exporting, setExporting] = useState(false); const [exportError, setExportError] = useState(''); const { columns, rows, total, bordered } = table; const headers = headerRows(columns); const handleExport = async () => { setExporting(true); setExportError(''); try { await exportTableToXlsx(table, filename); } catch (err) { setExportError(err.message || 'Unable to export report'); } finally { setExporting(false); } }; const cellClass = (index) => clsx(stickyFirstColumn && index === 0 && 'sticky-first-col', columns[index]?.className); return (
{title}
{error ?
{error}
: null} {exportError ?
{exportError}
: null} {loading ? (
) : null} {columns.length ? (
{headers.map((cells, rowIndex) => ( {cells.map((cell) => ( ))} ))} {rows.length ? ( rows.map((row, rowIndex) => ( {columns.map((column, index) => ( ))} )) ) : ( )} {total ? ( {total.map((value, index) => ( ))} ) : null}
1 && 'text-center')} > {cell.label}
{formatCell(rowValue(row, column), column.format)}
No data
{formatCell(value, columns[index]?.format)}
) : (
{loading ? 'Loading...' : 'No data'}
)}
); }