Add pagination to

This commit is contained in:
Alex Holliday committed 2024-09-04 17:37:30 -07:00
1 parent 717923fff4
commit d32a2a7efa
4 files changed
+31 -12

No files matched your search

@@ -19,6 +19,10 @@ import { useState, useEffect } from "react";
import { logger } from "../../../../Utils/Logger"; import { logger } from "../../../../Utils/Logger";
import Host from "../host"; import Host from "../host";
import { StatusLabel } from "../../../../Components/Label"; import { StatusLabel } from "../../../../Components/Label";
import { jwtDecode } from "jwt-decode";
import { useSelector } from "react-redux";
import { networkService } from "../../../../main";
const MonitorTable = ({ teamId }) => { const MonitorTable = ({ teamId }) => {
const [paginationController, setPaginationController] = useState({ const [paginationController, setPaginationController] = useState({
page: 0, page: 0,
@@ -26,6 +30,7 @@ const MonitorTable = ({ teamId }) => {
}); });
const [monitors, setMonitors] = useState([]); const [monitors, setMonitors] = useState([]);
const [monitorCount, setMonitorCount] = useState(0); const [monitorCount, setMonitorCount] = useState(0);
const authState = useSelector((state) => state.auth);
const handlePageChange = (_, newPage) => { const handlePageChange = (_, newPage) => {
setPaginationController({ setPaginationController({
@@ -36,22 +41,21 @@ const MonitorTable = ({ teamId }) => {
useEffect(() => { useEffect(() => {
const fetchPage = async () => { const fetchPage = async () => {
if (!monitors || Object.keys(monitors).length === 0) {
return;
}
try { try {
console.log("DO NETWORK"); const { authToken } = authState;
const user = jwtDecode(authToken);
const res = await networkService.getMonitorsByTeamId( const res = await networkService.getMonitorsByTeamId(
token, authToken,
user.teamId, user.teamId,
25, 25,
["http", "ping"], ["http", "ping"],
null, null,
"desc", "desc",
true true,
paginationController.page,
paginationController.rowsPerPage
); );
console.log(res.data.data);
setMonitors([]); setMonitors([]);
setMonitorCount(0); setMonitorCount(0);
} catch (error) { } catch (error) {
@@ -59,7 +63,7 @@ const MonitorTable = ({ teamId }) => {
} }
}; };
fetchPage(); fetchPage();
}, [monitors]); }, []);
let paginationComponent = <></>; let paginationComponent = <></>;
if (monitorCount > paginationController.rowsPerPage) { if (monitorCount > paginationController.rowsPerPage) {
+5 -1
View File
@@ -80,7 +80,9 @@ class NetworkService {
types, types,
status, status,
sortOrder, sortOrder,
normalize normalize,
page,
rowsPerPage
) { ) {
const params = new URLSearchParams(); const params = new URLSearchParams();
@@ -93,6 +95,8 @@ class NetworkService {
if (status) params.append("status", status); if (status) params.append("status", status);
if (sortOrder) params.append("sortOrder", sortOrder); if (sortOrder) params.append("sortOrder", sortOrder);
if (normalize) params.append("normalize", normalize); if (normalize) params.append("normalize", normalize);
if (page) params.append("page", page);
if (rowsPerPage) params.append("rowsPerPage", rowsPerPage);
return this.axiosInstance.get( return this.axiosInstance.get(
`/monitors/team/${teamId}?${params.toString()}`, `/monitors/team/${teamId}?${params.toString()}`,
+11 -2
View File
@@ -393,9 +393,16 @@ const getMonitorById = async (monitorId) => {
*/ */
const getMonitorsByTeamId = async (req, res) => { const getMonitorsByTeamId = async (req, res) => {
try { try {
let { limit, type, status, sortOrder, normalize } = req.query || {}; let { limit, type, status, sortOrder, normalize, page, rowsPerPage } =
req.query || {};
const monitorQuery = { teamId: req.params.teamId }; const monitorQuery = { teamId: req.params.teamId };
// Pagination
let skip = 0;
if (page && rowsPerPage) {
skip = page * rowsPerPage;
}
if (type !== undefined) { if (type !== undefined) {
const types = Array.isArray(type) ? type : [type]; const types = Array.isArray(type) ? type : [type];
monitorQuery.type = { $in: types }; monitorQuery.type = { $in: types };
@@ -411,7 +418,9 @@ const getMonitorsByTeamId = async (req, res) => {
// This effectively removes limit, returning all checks // This effectively removes limit, returning all checks
if (limit === undefined) limit = 0; if (limit === undefined) limit = 0;
const monitors = await Monitor.find(monitorQuery); const monitors = await Monitor.find(monitorQuery)
.skip(skip)
.limit(rowsPerPage);
// Map each monitor to include its associated checks // Map each monitor to include its associated checks
const monitorsWithChecks = await Promise.all( const monitorsWithChecks = await Promise.all(
monitors.map(async (monitor) => { monitors.map(async (monitor) => {
+2
View File
@@ -176,6 +176,8 @@ const getMonitorsByTeamIdQueryValidation = joi.object({
joi.string().valid("http", "ping", "pagespeed"), joi.string().valid("http", "ping", "pagespeed"),
joi.array().items(joi.string().valid("http", "ping", "pagespeed")) joi.array().items(joi.string().valid("http", "ping", "pagespeed"))
), ),
page: joi.number(),
rowsPerPage: joi.number(),
}); });
const getMonitorStatsByIdParamValidation = joi.object({ const getMonitorStatsByIdParamValidation = joi.object({