implement sorting

This commit is contained in:
Alex Holliday
2024-09-18 10:39:47 +08:00
parent 7e1a5b6ec8
commit 2d2b101eb7
3 changed files with 23 additions and 3 deletions
@@ -151,7 +151,10 @@ const MonitorTable = ({ isAdmin }) => {
"desc", "desc",
true, true,
page, page,
rowsPerPage rowsPerPage,
null,
"createdAt",
"desc"
); );
setMonitors(res?.data?.data?.monitors ?? []); setMonitors(res?.data?.data?.monitors ?? []);
setMonitorCount(res?.data?.data?.monitorCount ?? 0); setMonitorCount(res?.data?.data?.monitorCount ?? 0);
+7 -1
View File
@@ -101,7 +101,10 @@ class NetworkService {
checkOrder, checkOrder,
normalize, normalize,
page, page,
rowsPerPage rowsPerPage,
filter,
field,
order
) { ) {
const params = new URLSearchParams(); const params = new URLSearchParams();
@@ -116,6 +119,9 @@ class NetworkService {
if (normalize) params.append("normalize", normalize); if (normalize) params.append("normalize", normalize);
if (page) params.append("page", page); if (page) params.append("page", page);
if (rowsPerPage) params.append("rowsPerPage", rowsPerPage); if (rowsPerPage) params.append("rowsPerPage", rowsPerPage);
if (filter) params.append("filter", filter);
if (field) params.append("field", field);
if (order) params.append("order", order);
return this.axiosInstance.get( return this.axiosInstance.get(
`/monitors/team/${teamId}?${params.toString()}`, `/monitors/team/${teamId}?${params.toString()}`,
+12 -1
View File
@@ -356,9 +356,13 @@ const getMonitorsByTeamId = async (req, res) => {
page, page,
rowsPerPage, rowsPerPage,
filter, filter,
field,
order,
} = req.query || {}; } = req.query || {};
const monitorQuery = { teamId: req.params.teamId, type }; const monitorQuery = { teamId: req.params.teamId, type };
// Add filter if provided // Add filter if provided
// $options: "i" makes the search case-insensitive
if (filter !== undefined) { if (filter !== undefined) {
monitorQuery.$or = [ monitorQuery.$or = [
{ name: { $regex: filter, $options: "i" } }, { name: { $regex: filter, $options: "i" } },
@@ -385,9 +389,16 @@ const getMonitorsByTeamId = async (req, res) => {
checkOrder = -1; checkOrder = -1;
} else checkOrder = -1; } else checkOrder = -1;
// Sort order for monitors
let sort = {};
if (field !== undefined && order !== undefined) {
sort[field] = order === "asc" ? 1 : -1;
}
const monitors = await Monitor.find(monitorQuery) const monitors = await Monitor.find(monitorQuery)
.skip(skip) .skip(skip)
.limit(rowsPerPage); .limit(rowsPerPage)
.sort(sort);
// Early return if limit is set to -1, indicating we don't want any checks // Early return if limit is set to -1, indicating we don't want any checks
if (limit === "-1") { if (limit === "-1") {