Excised insanity from Basic Table

This commit is contained in:
Alex Holliday committed 2024-07-16 13:21:45 -07:00
1 parent 67410fc9e7
commit cc9c439c8d
3 files changed
+95 -125

No files matched your search

+73 -88
View File
@@ -17,45 +17,52 @@ import PropTypes from "prop-types";
* *
* @component * @component
* @param {Object} props - Component props. * @param {Object} props - Component props.
* @param {Array} props.headers - Array of header titles for the table. * @param {Object} props.data - Data for the table including columns and rows.
* @param {Array} props.rowItems - Array of row data objects for the table. * @param {Array} props.data.cols - Array of objects for column headers.
* @param {boolean} props.paginated - Flag to enable pagination. * @param {number} props.data.cols[].id - Unique identifier for the column.
* @param {function} props.handleClick - Function to handle click on row. * @param {string} props.data.cols[].name - Name of the column to display as header.
* @param {Array} props.data.rows - Array of row objects.
* @param {number} props.data.rows[].id - Unique identifier for the row.
* @param {Array} props.data.rows[].data - Array of cell data objects for the row.
* @param {number} props.data.rows[].data[].id - Unique identifier for the cell.
* @param {JSX.Element} props.data.rows[].data[].data - The content to display in the cell.
* @param {functio} props.data.rows.data.handleClick - Function to call when the row is clicked.
* @param {boolean} [props.paginated=false] - Flag to enable pagination.
* *
* @example * @example
* const data = {
* cols: [
* { id: 1, name: "First Col" },
* { id: 2, name: "Second Col" },
* { id: 3, name: "Third Col" },
* { id: 4, name: "Fourth Col" },
* ],
* rows: [
* {
* id: 1,
* data: [
* { id: 1, data: <div>Data for Row 1 Col 1</div> },
* { id: 2, data: <div>Data for Row 1 Col 2</div> },
* { id: 3, data: <div>Data for Row 1 Col 3</div> },
* { id: 4, data: <div>Data for Row 1 Col 4</div> },
* ],
* },
* {
* id: 2,
* data: [
* { id: 5, data: <div>Data for Row 2 Col 1</div> },
* { id: 6, data: <div>Data for Row 2 Col 2</div> },
* { id: 7, data: <div>Data for Row 2 Col 3</div> },
* { id: 8, data: <div>Data for Row 2 Col 4</div> },
* ],
* },
* ],
* };
* *
* Headers is an array of objets that have a unique ID and a name * <BasicTable data={data} rows={rows} paginated={true} />
* Rows is an array of objects that have a unique ID and some data to display
* Row data can be any valid React node
* Rows are split into groups according to the number of headers, so if you have 3 headers then
* Rows will go under the index % 3 column.
* ie: Row 0 -> col 0 % 3 = 0
* ie: Row 1 -> col 1 % 3 = 1
* ie: Row 2 -> col 2 % 3 = 2
* ie: Row 3 -> col 3 % 3 = 0
*
* Thus items are put in their correct columns
*
*
* const headers = [
* { id: 1, name: "ID" },
* { id: 2, name: "Name" },
* { id: 3, name: "Age" },
* ];
* const rows = [
* { id: 1, data: <span>999</span> },
* { id: 2, data: <div>Alex Holliday</div> },
* { id: 3, data: <div>5</div> },
* // More rows...
* ];
*
* ROW CLICK HANDLING
* If you want to set a listener for a row click, you can add a handleClick function to FIRST ITEM IN EACH ROW
*
* <BasicTable headers={headers} rows={rows} paginated={true} />
*/ */
const BasicTable = ({ headers, rowItems, paginated }) => { const BasicTable = ({ data, paginated }) => {
// Add headers to props validation // Add headers to props validation
const [page, setPage] = useState(0); const [page, setPage] = useState(0);
@@ -70,19 +77,13 @@ const BasicTable = ({ headers, rowItems, paginated }) => {
setPage(0); // Reset to first page after changing rows per page setPage(0); // Reset to first page after changing rows per page
}; };
const numOfCols = headers.length; let paginatedData = [];
if (paginated) {
const groupedRowItems = rowItems.reduce((acc, item, idx) => { paginatedData = data.rows.slice(
const rowIndex = Math.floor((item.id - 1) / numOfCols); page * rowsPerPage,
if (!acc[rowIndex]) acc[rowIndex] = { items: [], idx }; page * rowsPerPage + rowsPerPage
acc[rowIndex].items.push({ item }); );
return acc; }
}, {});
const paginatedRowsKeys = Object.keys(groupedRowItems).slice(
page * rowsPerPage,
page * rowsPerPage + rowsPerPage
);
return ( return (
<> <>
@@ -90,56 +91,52 @@ const BasicTable = ({ headers, rowItems, paginated }) => {
<Table> <Table>
<TableHead> <TableHead>
<TableRow> <TableRow>
{headers.map((header) => { {data.cols.map((col) => (
return <TableCell key={header.id}>{header.name}</TableCell>; <TableCell key={col.id}>{col.name}</TableCell>
})} ))}
</TableRow> </TableRow>
</TableHead> </TableHead>
<TableBody> <TableBody>
{paginated === false && {paginated === false &&
Object.keys(groupedRowItems).map((key) => { data.rows.map((row) => {
return ( return (
<TableRow <TableRow
onClick={() => { sx={{ cursor: row.handleClick ? "pointer" : "default" }}
groupedRowItems[key].items[0].item.handleClick key={row.id}
? groupedRowItems[key].items[0].item.handleClick() onClick={row.handleClick ? row.handleClick : null}
: null;
}}
key={groupedRowItems[key].idx}
> >
{groupedRowItems[key].items.map(({ item }) => { {row.data.map((cell) => {
return <TableCell key={item.id}>{item.data}</TableCell>; return <TableCell key={cell.id}>{cell.data}</TableCell>;
})} })}
</TableRow> </TableRow>
); );
})} })}
{paginated === true && {paginated === true &&
paginatedRowsKeys.map((key) => ( paginatedData.map((row) => {
<TableRow return (
onClick={() => { <TableRow
groupedRowItems[key].items[0].item.handleClick sx={{ cursor: row.handleClick ? "pointer" : "default" }}
? groupedRowItems[key].items[0].item.handleClick() key={row.id}
: null; onClick={row.handleClick ? row.handleClick : null}
}} >
key={groupedRowItems[key].idx} {row.data.map((cell) => {
> return <TableCell key={cell.id}>{cell.data}</TableCell>;
{groupedRowItems[key].items.map(({ item }) => ( })}
<TableCell key={item.id}>{item.data}</TableCell> </TableRow>
))} );
</TableRow> })}
))}
</TableBody> </TableBody>
</Table> </Table>
</TableContainer> </TableContainer>
{paginated === true && ( {paginated === true && (
<TablePagination <TablePagination
rowsPerPageOptions={[1, 5, 10, 25]}
component="div" component="div"
count={Object.keys(groupedRowItems).length} count={data.rows.length}
rowsPerPage={rowsPerPage} rowsPerPage={rowsPerPage}
page={page} page={page}
onPageChange={handleChangePage} onPageChange={handleChangePage}
onRowsPerPageChange={handleChangeRowsPerPage} onRowsPerPageChange={handleChangeRowsPerPage}
rowsPerPageOptions={[1, 5, 10]}
/> />
)} )}
</> </>
@@ -147,20 +144,8 @@ const BasicTable = ({ headers, rowItems, paginated }) => {
}; };
BasicTable.propTypes = { BasicTable.propTypes = {
headers: PropTypes.arrayOf( data: PropTypes.object.isRequired,
PropTypes.shape({
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
name: PropTypes.string.isRequired,
})
).isRequired,
rowItems: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
data: PropTypes.node,
})
).isRequired,
paginated: PropTypes.bool, paginated: PropTypes.bool,
handleClick: PropTypes.func,
}; };
export default BasicTable; export default BasicTable;
+22 -36
View File
@@ -83,14 +83,17 @@ const Status = ({ params }) => {
const MonitorTable = ({ monitors = [] }) => { const MonitorTable = ({ monitors = [] }) => {
const navigate = useNavigate(); const navigate = useNavigate();
const headers = [ const data = {
{ id: 1, name: "Host" }, cols: [
{ id: 2, name: "Status" }, { id: 1, name: "Host" },
{ id: 3, name: "Response Time" }, { id: 2, name: "Status" },
{ id: 4, name: "Actions" }, { id: 3, name: "Response Time" },
]; { id: 4, name: "Actions" },
const data = []; ],
monitors.forEach((monitor) => { rows: [],
};
data.rows = monitors.map((monitor, idx) => {
const params = { const params = {
url: monitor.url, url: monitor.url,
title: monitor.name, title: monitor.name,
@@ -109,37 +112,20 @@ const MonitorTable = ({ monitors = [] }) => {
? "var(--env-var-color-17)" ? "var(--env-var-color-17)"
: "var(--env-var-color-19)", : "var(--env-var-color-19)",
}; };
data.push({
id: data.length + 1,
data: <Host params={params} />,
handleClick: () => {
navigate(`/monitors/${monitor._id}`);
},
});
data.push({
id: data.length + 1,
data: <Status params={params} />,
});
data.push({
id: data.length + 1,
data: <Host params={params} />,
handleClick: () => {
console.log(monitor._id);
},
});
data.push({
id: data.length + 1,
data: <Status params={params} />,
});
data.push({
id: data.length + 1,
data: <ResponseTimeChart checks={monitor.checks} />, return {
}); id: monitor._id,
data.push({ id: data.length + 1, itemId: monitor._id, data: "TODO" }); handleClick: () => navigate(`/monitors/${monitor._id}`),
data: [
{ id: idx, data: <Host params={params} /> },
{ id: idx + 1, data: <Status params={params} /> },
{ id: idx + 2, data: <ResponseTimeChart checks={monitor.checks} /> },
{ id: idx + 3, data: "TODO" },
],
};
}); });
return <BasicTable headers={headers} rowItems={data} paginated={true} />; return <BasicTable data={data} paginated={true} />;
}; };
MonitorTable.propTypes = { MonitorTable.propTypes = {
-1
View File
@@ -27,7 +27,6 @@ const DetailsPage = () => {
const theme = useTheme(); const theme = useTheme();
// Example monitor data (replace with actual data or fetch from API)
/** /**
* Function to calculate uptime duration based on the most recent check. * Function to calculate uptime duration based on the most recent check.
* @param {Array} checks Array of check objects. * @param {Array} checks Array of check objects.