Files
Checkmate/Client/src/Pages/Monitors/Details/PaginationTable/index.jsx
T

181 lines
5.0 KiB
React
Raw Normal View History

2024-08-08 13:19:37 -07:00
import PropTypes from "prop-types";
import {
TableContainer,
Table,
TableHead,
TableRow,
TableCell,
TableBody,
2024-08-08 19:27:28 -04:00
PaginationItem,
Pagination,
Paper,
2024-08-08 13:19:37 -07:00
} from "@mui/material";
import { useState, useEffect } from "react";
import { useSelector } from "react-redux";
2024-08-18 17:42:49 -07:00
import { networkService } from "../../../../main";
2024-08-08 13:19:37 -07:00
import { StatusLabel } from "../../../../Components/Label";
2024-08-08 19:27:28 -04:00
import ArrowBackRoundedIcon from "@mui/icons-material/ArrowBackRounded";
import ArrowForwardRoundedIcon from "@mui/icons-material/ArrowForwardRounded";
import { logger } from "../../../../Utils/Logger";
2024-08-27 12:18:25 -04:00
import { useTheme } from "@emotion/react";
2024-08-08 13:19:37 -07:00
const PaginationTable = ({ monitorId, dateRange }) => {
2024-08-27 12:18:25 -04:00
const theme = useTheme();
2024-08-08 13:19:37 -07:00
const { authToken } = useSelector((state) => state.auth);
const [checks, setChecks] = useState([]);
const [checksCount, setChecksCount] = useState(0);
const [paginationController, setPaginationController] = useState({
page: 0,
rowsPerPage: 5,
});
2024-08-10 10:38:14 -07:00
useEffect(() => {
2024-08-20 11:07:34 -07:00
setPaginationController((prevPaginationController) => ({
...prevPaginationController,
2024-08-10 10:38:14 -07:00
page: 0,
2024-08-20 11:07:34 -07:00
}));
}, [dateRange]);
2024-08-10 10:38:14 -07:00
2024-08-08 13:19:37 -07:00
useEffect(() => {
const fetchPage = async () => {
try {
2024-08-18 17:42:49 -07:00
const res = await networkService.getChecksByMonitor(
2024-08-18 17:18:16 -07:00
authToken,
monitorId,
"desc",
null,
dateRange,
null,
paginationController.page,
paginationController.rowsPerPage
2024-08-08 13:19:37 -07:00
);
setChecks(res.data.data.checks);
setChecksCount(res.data.data.checksCount);
} catch (error) {
logger.error(error);
2024-08-08 13:19:37 -07:00
}
};
fetchPage();
}, [
authToken,
monitorId,
dateRange,
2024-08-08 13:19:37 -07:00
paginationController.page,
paginationController.rowsPerPage,
]);
const handlePageChange = (_, newPage) => {
setPaginationController({
...paginationController,
page: newPage - 1, // 0-indexed
2024-08-08 13:19:37 -07:00
});
};
2024-08-08 19:27:28 -04:00
let paginationComponent = <></>;
if (checksCount > paginationController.rowsPerPage) {
paginationComponent = (
<Pagination
count={Math.ceil(checksCount / paginationController.rowsPerPage)}
page={paginationController.page + 1} //0-indexed
2024-08-08 19:27:28 -04:00
onChange={handlePageChange}
shape="rounded"
2024-08-27 12:18:25 -04:00
sx={{
backgroundColor: theme.palette.background.main,
border: 1,
borderColor: theme.palette.border.light,
"& button": {
color: theme.palette.text.tertiary,
borderRadius: theme.shape.borderRadius,
},
"& li:first-of-type button, & li:last-of-type button": {
border: 1,
borderColor: theme.palette.border.light,
},
}}
2024-08-08 19:27:28 -04:00
renderItem={(item) => (
<PaginationItem
slots={{
previous: ArrowBackRoundedIcon,
next: ArrowForwardRoundedIcon,
}}
{...item}
sx={{
"&:focus": {
outline: "none",
},
"& .MuiTouchRipple-root": {
pointerEvents: "none",
display: "none",
},
2024-08-27 12:18:25 -04:00
"&.Mui-selected, &.Mui-selected:hover": {
backgroundColor: theme.palette.background.fill,
},
2024-08-08 19:27:28 -04:00
}}
/>
)}
/>
);
}
2024-08-08 13:19:37 -07:00
return (
2024-08-08 19:27:28 -04:00
<>
2024-08-27 12:18:25 -04:00
<TableContainer
component={Paper}
sx={{
border: `solid 1px ${theme.palette.border.light}`,
borderRadius: theme.shape.borderRadius,
}}
>
2024-08-08 19:27:28 -04:00
<Table>
2024-08-27 12:18:25 -04:00
<TableHead
sx={{
backgroundColor: theme.palette.background.accent,
}}
>
2024-08-08 19:27:28 -04:00
<TableRow>
2024-08-27 12:18:25 -04:00
<TableCell sx={{ color: theme.palette.text.secondary }}>
Status
</TableCell>
<TableCell sx={{ color: theme.palette.text.secondary }}>
Date & Time
</TableCell>
<TableCell sx={{ color: theme.palette.text.secondary }}>
Message
</TableCell>
2024-08-08 19:27:28 -04:00
</TableRow>
</TableHead>
<TableBody>
{checks.map((check) => {
const status = check.status === true ? "up" : "down";
2024-08-08 13:19:37 -07:00
2024-08-08 19:27:28 -04:00
return (
<TableRow key={check._id}>
<TableCell>
<StatusLabel
status={status}
text={status}
customStyles={{ textTransform: "capitalize" }}
/>
</TableCell>
<TableCell>
{new Date(check.createdAt).toLocaleString()}
</TableCell>
<TableCell>{check.statusCode}</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</TableContainer>
{paginationComponent}
</>
2024-08-08 13:19:37 -07:00
);
};
PaginationTable.propTypes = {
monitorId: PropTypes.string.isRequired,
dateRange: PropTypes.string.isRequired,
2024-08-08 13:19:37 -07:00
};
export default PaginationTable;