Files
Checkmate/Client/src/Pages/Incidents/IncidentTable/index.jsx
T

230 lines
6.6 KiB
React
Raw Normal View History

import PropTypes from "prop-types";
import {
TableContainer,
Table,
TableHead,
TableRow,
TableCell,
TableBody,
2024-08-09 10:01:28 -07:00
Pagination,
PaginationItem,
Paper,
Typography,
Box,
} from "@mui/material";
2024-08-09 10:01:28 -07:00
import ArrowBackRoundedIcon from "@mui/icons-material/ArrowBackRounded";
import ArrowForwardRoundedIcon from "@mui/icons-material/ArrowForwardRounded";
import { useState, useEffect } from "react";
import { useSelector } from "react-redux";
2024-08-18 17:42:49 -07:00
import { networkService } from "../../../main";
import { StatusLabel } from "../../../Components/Label";
import { logger } from "../../../Utils/Logger";
2024-08-28 10:28:02 -04:00
import { useTheme } from "@emotion/react";
const IncidentTable = ({ monitors, selectedMonitor, filter }) => {
2024-08-28 10:28:02 -04:00
const theme = useTheme();
const { authToken, user } = useSelector((state) => state.auth);
const [checks, setChecks] = useState([]);
const [checksCount, setChecksCount] = useState(0);
const [paginationController, setPaginationController] = useState({
page: 0,
rowsPerPage: 12,
});
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
}));
}, [filter, selectedMonitor]);
2024-08-10 10:38:14 -07:00
useEffect(() => {
const fetchPage = async () => {
if (!monitors || Object.keys(monitors).length === 0) {
return;
}
try {
2024-08-18 17:18:16 -07:00
let res;
if (selectedMonitor === "0") {
res = await networkService.getChecksByTeam(
2024-08-18 17:18:16 -07:00
authToken,
user.teamId,
2024-08-18 17:18:16 -07:00
"desc",
null,
null,
filter,
paginationController.page,
paginationController.rowsPerPage
);
} else {
2024-08-18 17:42:49 -07:00
res = await networkService.getChecksByMonitor(
2024-08-18 17:18:16 -07:00
authToken,
selectedMonitor,
"desc",
null,
null,
filter,
paginationController.page,
paginationController.rowsPerPage
);
}
setChecks(res.data.data.checks);
setChecksCount(res.data.data.checksCount);
} catch (error) {
logger.error(error);
}
};
fetchPage();
}, [
authToken,
user,
monitors,
selectedMonitor,
filter,
paginationController.page,
paginationController.rowsPerPage,
]);
const handlePageChange = (_, newPage) => {
setPaginationController({
...paginationController,
page: newPage - 1, // 0-indexed
});
};
2024-08-09 10:01:28 -07:00
let paginationComponent = <></>;
if (checksCount > paginationController.rowsPerPage) {
paginationComponent = (
<Pagination
count={Math.ceil(checksCount / paginationController.rowsPerPage)}
page={paginationController.page + 1} //0-indexed
2024-08-09 10:01:28 -07:00
onChange={handlePageChange}
shape="rounded"
2024-08-28 10:28:02 -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-09 10:01:28 -07:00
renderItem={(item) => (
<PaginationItem
slots={{
previous: ArrowBackRoundedIcon,
next: ArrowForwardRoundedIcon,
}}
{...item}
sx={{
"&:focus": {
outline: "none",
},
2024-08-28 10:28:02 -04:00
"& .MuiTouchRipple-root": {
pointerEvents: "none",
display: "none",
},
"&.Mui-selected, &.Mui-selected:hover": {
backgroundColor: theme.palette.background.fill,
},
2024-08-09 10:01:28 -07:00
}}
/>
)}
/>
);
}
2024-08-28 10:28:02 -04:00
let sharedStyles = {
border: 1,
borderColor: theme.palette.border.light,
borderRadius: theme.shape.borderRadius,
backgroundColor: theme.palette.background.main,
p: theme.spacing(30),
};
return (
2024-08-09 10:01:28 -07:00
<>
{checks?.length === 0 && selectedMonitor === "0" ? (
2024-08-28 10:28:02 -04:00
<Box sx={{ ...sharedStyles }}>
<Typography textAlign="center" color={theme.palette.text.secondary}>
No incidents recorded yet.
</Typography>
</Box>
) : checks?.length === 0 ? (
2024-08-28 10:28:02 -04:00
<Box sx={{ ...sharedStyles }}>
<Typography textAlign="center" color={theme.palette.text.secondary}>
2024-08-12 10:02:54 -07:00
The monitor you have selected has no recorded incidents yet.
</Typography>
</Box>
) : (
<>
2024-08-28 10:28:02 -04:00
<TableContainer
component={Paper}
sx={{
border: `solid 1px ${theme.palette.border.light}`,
borderRadius: theme.shape.borderRadius,
}}
>
<Table>
2024-08-28 10:28:02 -04:00
<TableHead
sx={{
backgroundColor: theme.palette.background.accent,
}}
>
<TableRow
sx={{
"& > .MuiTableCell-root": {
color: theme.palette.text.secondary,
},
}}
>
<TableCell>Monitor Name</TableCell>
<TableCell>Status</TableCell>
<TableCell>Date & Time</TableCell>
<TableCell>Message</TableCell>
2024-08-09 10:01:28 -07:00
</TableRow>
</TableHead>
<TableBody>
{checks.map((check) => {
const status = check.status === true ? "up" : "down";
return (
<TableRow key={check._id}>
<TableCell>{monitors[check.monitorId]?.name}</TableCell>
<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-09 10:01:28 -07:00
</>
);
};
IncidentTable.propTypes = {
monitors: PropTypes.object.isRequired,
selectedMonitor: PropTypes.string.isRequired,
filter: PropTypes.string.isRequired,
};
export default IncidentTable;