Implemented pagination for incidents component
This commit is contained in:
1 parent
3924099e3e
commit
26e3f81a91
7 files changed
+131
-176
No files matched your search
@@ -6,10 +6,9 @@ import {
|
||||
TableRow,
|
||||
TableCell,
|
||||
TableBody,
|
||||
TableFooter,
|
||||
TablePagination,
|
||||
Pagination,
|
||||
PaginationItem,
|
||||
Paper,
|
||||
} from "@mui/material";
|
||||
|
||||
import ArrowBackRoundedIcon from "@mui/icons-material/ArrowBackRounded";
|
||||
@@ -20,29 +19,29 @@ import { useSelector } from "react-redux";
|
||||
import axiosInstance from "../../../Utils/axiosConfig";
|
||||
import { StatusLabel } from "../../../Components/Label";
|
||||
|
||||
const IncidentTable = ({ monitorId, filter }) => {
|
||||
const { authToken } = useSelector((state) => state.auth);
|
||||
const IncidentTable = ({ monitors, selectedMonitor, filter }) => {
|
||||
const { authToken, user } = useSelector((state) => state.auth);
|
||||
const [checks, setChecks] = useState([]);
|
||||
const [checksCount, setChecksCount] = useState(0);
|
||||
const [paginationController, setPaginationController] = useState({
|
||||
page: 0,
|
||||
rowsPerPage: 5,
|
||||
rowsPerPage: 12,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const fetchPage = async () => {
|
||||
try {
|
||||
if (monitorId === "0") {
|
||||
return;
|
||||
let url = `/checks/${selectedMonitor}?sortOrder=desc&filter=${filter}&page=${paginationController.page}&rowsPerPage=${paginationController.rowsPerPage}`;
|
||||
|
||||
if (selectedMonitor === "0") {
|
||||
url = `/checks/user/${user._id}?sortOrder=desc&filter=${filter}&page=${paginationController.page}&rowsPerPage=${paginationController.rowsPerPage}`;
|
||||
}
|
||||
const res = await axiosInstance.get(
|
||||
`/checks/${monitorId}?sortOrder=desc&filter=${filter}&page=${paginationController.page}&rowsPerPage=${paginationController.rowsPerPage}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const res = await axiosInstance.get(url, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
},
|
||||
});
|
||||
setChecks(res.data.data.checks);
|
||||
setChecksCount(res.data.data.checksCount);
|
||||
} catch (error) {
|
||||
@@ -52,24 +51,18 @@ const IncidentTable = ({ monitorId, filter }) => {
|
||||
fetchPage();
|
||||
}, [
|
||||
authToken,
|
||||
monitorId,
|
||||
user,
|
||||
selectedMonitor,
|
||||
filter,
|
||||
paginationController.page,
|
||||
paginationController.rowsPerPage,
|
||||
]);
|
||||
|
||||
const handlePageChange = (_, newPage) => {
|
||||
console.log(newPage);
|
||||
setPaginationController({
|
||||
...paginationController,
|
||||
page: newPage,
|
||||
});
|
||||
};
|
||||
|
||||
const handleChangeRowsPerPage = (event) => {
|
||||
setPaginationController({
|
||||
...paginationController,
|
||||
rowsPerPage: parseInt(event.target.value, 10),
|
||||
page: 0,
|
||||
page: newPage - 1, // 0-indexed
|
||||
});
|
||||
};
|
||||
|
||||
@@ -78,7 +71,7 @@ const IncidentTable = ({ monitorId, filter }) => {
|
||||
paginationComponent = (
|
||||
<Pagination
|
||||
count={Math.ceil(checksCount / paginationController.rowsPerPage)}
|
||||
page={paginationController.page + 1}
|
||||
page={paginationController.page + 1} //0-indexed
|
||||
onChange={handlePageChange}
|
||||
shape="rounded"
|
||||
renderItem={(item) => (
|
||||
@@ -105,7 +98,7 @@ const IncidentTable = ({ monitorId, filter }) => {
|
||||
|
||||
return (
|
||||
<>
|
||||
<TableContainer>
|
||||
<TableContainer component={Paper}>
|
||||
<Table>
|
||||
<TableHead>
|
||||
<TableRow>
|
||||
@@ -121,6 +114,7 @@ const IncidentTable = ({ monitorId, filter }) => {
|
||||
|
||||
return (
|
||||
<TableRow key={check._id}>
|
||||
<TableCell>{monitors[check.monitorId]?.name}</TableCell>
|
||||
<TableCell>
|
||||
<StatusLabel
|
||||
status={status}
|
||||
@@ -144,7 +138,8 @@ const IncidentTable = ({ monitorId, filter }) => {
|
||||
};
|
||||
|
||||
IncidentTable.propTypes = {
|
||||
monitorId: PropTypes.string.isRequired,
|
||||
monitors: PropTypes.object.isRequired,
|
||||
selectedMonitor: PropTypes.string.isRequired,
|
||||
filter: PropTypes.string.isRequired,
|
||||
};
|
||||
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { useSelector } from "react-redux";
|
||||
import { Typography, ButtonGroup, Stack, Box, Skeleton } from "@mui/material";
|
||||
import WestRoundedIcon from "@mui/icons-material/WestRounded";
|
||||
import { ButtonGroup, Stack, Skeleton } from "@mui/material";
|
||||
import Button from "../../Components/Button";
|
||||
import axiosInstance from "../../Utils/axiosConfig";
|
||||
import BasicTable from "../../Components/BasicTable";
|
||||
import { StatusLabel } from "../../Components/Label";
|
||||
import { useTheme } from "@emotion/react";
|
||||
import { useNavigate } from "react-router";
|
||||
import Select from "../../Components/Inputs/Select";
|
||||
@@ -122,96 +119,12 @@ const Incidents = () => {
|
||||
/>
|
||||
</ButtonGroup>
|
||||
</Stack>
|
||||
<IncidentTable monitorId={selectedMonitor} filter={filter} />
|
||||
<IncidentTable
|
||||
monitors={monitors}
|
||||
selectedMonitor={selectedMonitor}
|
||||
filter={filter}
|
||||
/>
|
||||
</Stack>
|
||||
// <>
|
||||
// <Stack className="incidents" gap={theme.gap.large}>
|
||||
// {loading ? (
|
||||
// <SkeletonLayout />
|
||||
// ) : false ? (
|
||||
// <>
|
||||
// <Stack direction="row" alignItems="center" gap={theme.gap.medium}>
|
||||
// <Typography component="h1">Incident history for: </Typography>
|
||||
// <Select
|
||||
// id="incidents-select-monitor"
|
||||
// placeholder="All servers"
|
||||
// value={selectedMonitor}
|
||||
// onChange={handleSelect}
|
||||
// items={Object.values(monitors)}
|
||||
// />
|
||||
// {hasAnyIncidents && hasSpecificIncidents && (
|
||||
// <ButtonGroup sx={{ ml: "auto" }}>
|
||||
// <Button
|
||||
// level="secondary"
|
||||
// label="All"
|
||||
// onClick={() => setFilter("all")}
|
||||
// sx={{
|
||||
// backgroundColor:
|
||||
// filter === "all" && theme.palette.otherColors.fillGray,
|
||||
// }}
|
||||
// />
|
||||
// <Button
|
||||
// level="secondary"
|
||||
// label="Down"
|
||||
// onClick={() => setFilter("down")}
|
||||
// sx={{
|
||||
// backgroundColor:
|
||||
// filter === "down" && theme.palette.otherColors.fillGray,
|
||||
// }}
|
||||
// />
|
||||
// <Button
|
||||
// level="secondary"
|
||||
// label="Cannot Resolve"
|
||||
// onClick={() => setFilter("resolve")}
|
||||
// sx={{
|
||||
// backgroundColor:
|
||||
// filter === "resolve" &&
|
||||
// theme.palette.otherColors.fillGray,
|
||||
// }}
|
||||
// />
|
||||
// </ButtonGroup>
|
||||
// )}
|
||||
// </Stack>
|
||||
// {hasAnyIncidents && hasSpecificIncidents ? (
|
||||
// <div>thing</div>
|
||||
// ) : (
|
||||
// <Typography alignSelf="center" mt={theme.gap.xxl}>
|
||||
// The monitor you have selected, has no recorded incidents yet.
|
||||
// </Typography>
|
||||
// )}
|
||||
// </>
|
||||
// ) : (
|
||||
// <Box>
|
||||
// <Button
|
||||
// level="tertiary"
|
||||
// label="Back"
|
||||
// animate="slideLeft"
|
||||
// img={<WestRoundedIcon />}
|
||||
// onClick={() => navigate(-1)}
|
||||
// sx={{
|
||||
// backgroundColor: theme.palette.otherColors.fillGray,
|
||||
// px: theme.gap.ml,
|
||||
// mb: theme.gap.ml,
|
||||
// "& svg.MuiSvgIcon-root": {
|
||||
// mr: theme.gap.small,
|
||||
// fill: theme.palette.otherColors.slateGray,
|
||||
// },
|
||||
// }}
|
||||
// />
|
||||
// <Box
|
||||
// padding={theme.gap.xxl}
|
||||
// gap={theme.gap.small}
|
||||
// border={1}
|
||||
// borderRadius={`${theme.shape.borderRadius}px`}
|
||||
// borderColor={theme.palette.otherColors.graishWhite}
|
||||
// backgroundColor={theme.palette.otherColors.white}
|
||||
// >
|
||||
// <Typography textAlign="center">No incidents, yet.</Typography>
|
||||
// </Box>
|
||||
// </Box>
|
||||
// )}
|
||||
// </Stack>
|
||||
// </>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -6,8 +6,6 @@ import {
|
||||
TableRow,
|
||||
TableCell,
|
||||
TableBody,
|
||||
TableFooter,
|
||||
TablePagination,
|
||||
PaginationItem,
|
||||
Pagination,
|
||||
Paper,
|
||||
@@ -58,16 +56,16 @@ const PaginationTable = ({ monitorId, dateRange }) => {
|
||||
const handlePageChange = (_, newPage) => {
|
||||
setPaginationController({
|
||||
...paginationController,
|
||||
page: newPage - 1,
|
||||
page: newPage - 1, // 0-indexed
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
let paginationComponent = <></>;
|
||||
if (checksCount > paginationController.rowsPerPage) {
|
||||
paginationComponent = (
|
||||
<Pagination
|
||||
count={Math.ceil(checksCount / paginationController.rowsPerPage)}
|
||||
page={paginationController.page + 1}
|
||||
page={paginationController.page + 1} //0-indexed
|
||||
onChange={handlePageChange}
|
||||
shape="rounded"
|
||||
renderItem={(item) => (
|
||||
|
||||
Reference in new issue
Block a user