Refactored remaining requests

This commit is contained in:
Alex Holliday committed 2024-08-18 17:18:16 -07:00
1 parent f78e5ca94c
commit 92877e20ec
10 files changed
+174 -66

No files matched your search

@@ -176,13 +176,10 @@ const TeamPanel = () => {
setErrors((prev) => ({ ...prev, email: error.details[0].message }));
} else
try {
await axiosInstance.post(
"/auth/invite",
{
email: toInvite.email,
role: toInvite.role,
},
{ headers: { Authorization: `Bearer ${authToken}` } }
await axiosInstance.requestInvitationToken(
authToken,
toInvite.email,
toInvite.role
);
closeInviteModal();
@@ -38,6 +38,7 @@ export const getPageSpeedByUserId = createAsyncThunk(
user._id,
25,
["pagespeed"],
null,
"desc",
false
);
@@ -38,6 +38,7 @@ export const getUptimeMonitorsByUserId = createAsyncThunk(
user._id,
25,
["http", "ping"],
null,
"desc",
true
);
@@ -43,17 +43,30 @@ const IncidentTable = ({ monitors, selectedMonitor, filter }) => {
return;
}
try {
let url = `/checks/${selectedMonitor}?sortOrder=desc&filter=${filter}&page=${paginationController.page}&rowsPerPage=${paginationController.rowsPerPage}`;
let res;
if (selectedMonitor === "0") {
url = `/checks/user/${user._id}?sortOrder=desc&filter=${filter}&page=${paginationController.page}&rowsPerPage=${paginationController.rowsPerPage}`;
res = await axiosInstance.getChecksByUser(
authToken,
user._id,
"desc",
null,
null,
filter,
paginationController.page,
paginationController.rowsPerPage
);
} else {
res = await axiosInstance.getChecksByMonitor(
authToken,
selectedMonitor,
"desc",
null,
null,
filter,
paginationController.page,
paginationController.rowsPerPage
);
}
const res = await axiosInstance.get(url, {
headers: {
Authorization: `Bearer ${authToken}`,
},
});
setChecks(res.data.data.checks);
setChecksCount(res.data.data.checksCount);
} catch (error) {
+8 -7
View File
@@ -48,13 +48,14 @@ const Incidents = () => {
useEffect(() => {
const fetchMonitors = async () => {
setLoading(true);
const res = await axiosInstance.get(
`/monitors/user/${authState.user._id}?status=false&limit=1`,
{
headers: {
Authorization: `Bearer ${authState.authToken}`,
},
}
const res = await axiosInstance.getMonitorsByUserId(
authState.authToken,
authState.user._id,
1,
null,
null,
null,
null
);
// Reduce to a lookup object for 0(1) lookup
@@ -37,13 +37,15 @@ const PaginationTable = ({ monitorId, dateRange }) => {
useEffect(() => {
const fetchPage = async () => {
try {
const res = await axiosInstance.get(
`/checks/${monitorId}?sortOrder=desc&dateRange=${dateRange}&page=${paginationController.page}&rowsPerPage=${paginationController.rowsPerPage}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
const res = await axiosInstance.getChecksByMonitor(
authToken,
monitorId,
"desc",
null,
dateRange,
null,
paginationController.page,
paginationController.rowsPerPage
);
setChecks(res.data.data.checks);
setChecksCount(res.data.data.checksCount);
+12 -14
View File
@@ -121,16 +121,18 @@ const DetailsPage = () => {
const fetchMonitor = useCallback(async () => {
try {
const res = await axiosInstance.get(
`/monitors/stats/${monitorId}?dateRange=${dateRange}&numToDisplay=50&normalize=true`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
const res = await axiosInstance.getStatsByMonitorId(
authToken,
monitorId,
null,
null,
dateRange,
50,
true
);
setMonitor(res.data.data);
} catch (error) {
console.log(error);
console.error("Error fetching monitor of id: " + monitorId);
navigate("/not-found");
}
@@ -142,13 +144,9 @@ const DetailsPage = () => {
useEffect(() => {
const fetchCertificate = async () => {
const res = await axiosInstance.get(
`/monitors/certificate/${monitorId}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
const res = await axiosInstance.getCertificateExpiry(
authToken,
monitorId
);
setCertificateExpiry(res.data.data.certificateDate);
};
+8 -8
View File
@@ -186,15 +186,15 @@ const PageSpeedDetails = () => {
useEffect(() => {
const fetchMonitor = async () => {
try {
const res = await axiosInstance.get(
`/monitors/stats/${monitorId}?sortOrder=desc&limit=50`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
const res = await axiosInstance.getStatsByMonitorId(
authToken,
monitorId,
"desc",
50,
null,
null,
null
);
setMonitor(res.data.data);
setAudits(res.data.data.checks[0].audits);
} catch (error) {
+107 -12
View File
@@ -44,6 +44,7 @@ axiosInstance.getMonitorsByUserId = async (
userId,
limit,
types,
status,
sortOrder,
normalize
) => {
@@ -55,6 +56,7 @@ axiosInstance.getMonitorsByUserId = async (
params.append("type", type);
});
}
if (status) params.append("status", status);
if (sortOrder) params.append("sortOrder", sortOrder);
if (normalize) params.append("normalize", normalize);
@@ -66,6 +68,35 @@ axiosInstance.getMonitorsByUserId = async (
});
};
// **********************************
// Get stats for a monitor
// **********************************
axiosInstance.getStatsByMonitorId = async (
authToken,
monitorId,
sortOrder,
limit,
dateRange,
numToDisplay,
normalize
) => {
const params = new URLSearchParams();
if (sortOrder) params.append("sortOrder", sortOrder);
if (limit) params.append("limit", limit);
if (dateRange) params.append("dateRange", dateRange);
if (numToDisplay) params.append("numToDisplay", numToDisplay);
if (normalize) params.append("normalize", normalize);
return axiosInstance.get(
`/monitors/stats/${monitorId}?${params.toString()}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
);
};
// **********************************
// Updates a single monitor
// **********************************
@@ -90,6 +121,17 @@ axiosInstance.deleteMonitorById = async (authToken, monitorId) => {
});
};
// **********************************
// Get certificate
// **********************************
axiosInstance.getCertificateExpiry = async (authToken, monitorId) => {
return axiosInstance.get(`/monitors/certificate/${monitorId}`, {
headers: {
Authorization: `Bearer ${authToken}`,
},
});
};
// **********************************
// Register a new user
// **********************************
@@ -116,18 +158,6 @@ axiosInstance.updateUser = async (authToken, userId, form) => {
});
};
// **********************************
// Delete an exisiting user
// **********************************
axiosInstance.deleteUser = async (authToken, userId) => {
return axiosInstance.delete(`/auth/user/${userId}`, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
};
// **********************************
// Forgot password request
// **********************************
@@ -167,6 +197,19 @@ axiosInstance.getAllUsers = async (authToken) => {
});
};
// **********************************
// Request Invitation Token
// **********************************
axiosInstance.requestInvitationToken = async (authToken, email, role) => {
return axiosInstance.post(
`/auth/invite`,
{ email, role },
{
headers: { Authorization: `Bearer ${authToken}` },
}
);
};
// **********************************
// Verify Invitation Token
// **********************************
@@ -176,4 +219,56 @@ axiosInstance.verifyInvitationToken = async (token) => {
});
};
// **********************************
// Get all checks for a given monitor
// **********************************
axiosInstance.getChecksByMonitor = async (
authToken,
monitorId,
sortOrder,
limit,
dateRange,
filter,
page,
rowsPerPage
) => {
const params = new URLSearchParams();
if (sortOrder) params.append("sortOrder", sortOrder);
if (limit) params.append("limit", limit);
if (dateRange) params.append("dateRange", dateRange);
if (filter) params.append("filter", filter);
if (page) params.append("page", page);
if (rowsPerPage) params.append("rowsPerPage", rowsPerPage);
return axiosInstance.get(`/checks/${monitorId}?${params.toString()}`, {
headers: { Authorization: `Bearer ${authToken}` },
});
};
// **********************************
// Get all checks for a given user
// **********************************
axiosInstance.getChecksByUser = async (
authToken,
userId,
sortOrder,
limit,
dateRange,
filter,
page,
rowsPerPage
) => {
const params = new URLSearchParams();
if (sortOrder) params.append("sortOrder", sortOrder);
if (limit) params.append("limit", limit);
if (dateRange) params.append("dateRange", dateRange);
if (filter) params.append("filter", filter);
if (page) params.append("page", page);
if (rowsPerPage) params.append("rowsPerPage", rowsPerPage);
return axiosInstance.get(`/checks/user/${userId}?${params.toString()}`, {
headers: { Authorization: `Bearer ${authToken}` },
});
};
export default axiosInstance;
+2 -2
View File
@@ -15,12 +15,12 @@ router.get(
checkController.getChecks
);
router.get("/user/:userId", checkController.getUserChecks);
router.delete(
"/:monitorId",
verifyOwnership(Monitor, "monitorId"),
checkController.deleteChecks
);
router.get("/user/:userId", checkController.getUserChecks);
module.exports = router;