Merge pull request #2469 from bluewave-labs/refactor/useFetchChecks
Refactor: useFetchChecks
This commit is contained in:
3 files changed
+112
-70
No files matched your search
@@ -2,8 +2,57 @@ import { useState, useEffect } from "react";
|
|||||||
import { networkService } from "../main";
|
import { networkService } from "../main";
|
||||||
import { createToast } from "../Utils/toastUtils";
|
import { createToast } from "../Utils/toastUtils";
|
||||||
|
|
||||||
const useFetchChecks = ({
|
const useFetchChecksTeam = ({
|
||||||
teamId,
|
status,
|
||||||
|
sortOrder,
|
||||||
|
limit,
|
||||||
|
dateRange,
|
||||||
|
filter,
|
||||||
|
page,
|
||||||
|
rowsPerPage,
|
||||||
|
enabled = true,
|
||||||
|
}) => {
|
||||||
|
const [checks, setChecks] = useState(undefined);
|
||||||
|
const [checksCount, setChecksCount] = useState(undefined);
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [networkError, setNetworkError] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchChecks = async () => {
|
||||||
|
if (!enabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
status,
|
||||||
|
sortOrder,
|
||||||
|
limit,
|
||||||
|
dateRange,
|
||||||
|
filter,
|
||||||
|
page,
|
||||||
|
rowsPerPage,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
setIsLoading(true);
|
||||||
|
const res = await networkService.getChecksByTeam(config);
|
||||||
|
setChecks(res.data.data.checks);
|
||||||
|
setChecksCount(res.data.data.checksCount);
|
||||||
|
} catch (error) {
|
||||||
|
setNetworkError(true);
|
||||||
|
createToast({ body: error.message });
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchChecks();
|
||||||
|
}, [status, sortOrder, limit, dateRange, filter, page, rowsPerPage, enabled]);
|
||||||
|
|
||||||
|
return [checks, checksCount, isLoading, networkError];
|
||||||
|
};
|
||||||
|
|
||||||
|
const useFetchChecksByMonitor = ({
|
||||||
monitorId,
|
monitorId,
|
||||||
type,
|
type,
|
||||||
status,
|
status,
|
||||||
@@ -13,6 +62,7 @@ const useFetchChecks = ({
|
|||||||
filter,
|
filter,
|
||||||
page,
|
page,
|
||||||
rowsPerPage,
|
rowsPerPage,
|
||||||
|
enabled = true,
|
||||||
}) => {
|
}) => {
|
||||||
const [checks, setChecks] = useState(undefined);
|
const [checks, setChecks] = useState(undefined);
|
||||||
const [checksCount, setChecksCount] = useState(undefined);
|
const [checksCount, setChecksCount] = useState(undefined);
|
||||||
@@ -21,40 +71,25 @@ const useFetchChecks = ({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchChecks = async () => {
|
const fetchChecks = async () => {
|
||||||
if (!type && !teamId) {
|
if (!enabled || !type) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const method = monitorId
|
const config = {
|
||||||
? networkService.getChecksByMonitor
|
monitorId,
|
||||||
: networkService.getChecksByTeam;
|
type,
|
||||||
|
status,
|
||||||
const config = monitorId
|
sortOrder,
|
||||||
? {
|
limit,
|
||||||
monitorId,
|
dateRange,
|
||||||
type,
|
filter,
|
||||||
status,
|
page,
|
||||||
sortOrder,
|
rowsPerPage,
|
||||||
limit,
|
};
|
||||||
dateRange,
|
|
||||||
filter,
|
|
||||||
page,
|
|
||||||
rowsPerPage,
|
|
||||||
}
|
|
||||||
: {
|
|
||||||
status,
|
|
||||||
teamId,
|
|
||||||
sortOrder,
|
|
||||||
limit,
|
|
||||||
dateRange,
|
|
||||||
filter,
|
|
||||||
page,
|
|
||||||
rowsPerPage,
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
const res = await method(config);
|
const res = await networkService.getChecksByMonitor(config);
|
||||||
setChecks(res.data.data.checks);
|
setChecks(res.data.data.checks);
|
||||||
setChecksCount(res.data.data.checksCount);
|
setChecksCount(res.data.data.checksCount);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -68,7 +103,6 @@ const useFetchChecks = ({
|
|||||||
fetchChecks();
|
fetchChecks();
|
||||||
}, [
|
}, [
|
||||||
monitorId,
|
monitorId,
|
||||||
teamId,
|
|
||||||
type,
|
type,
|
||||||
status,
|
status,
|
||||||
sortOrder,
|
sortOrder,
|
||||||
@@ -77,9 +111,10 @@ const useFetchChecks = ({
|
|||||||
filter,
|
filter,
|
||||||
page,
|
page,
|
||||||
rowsPerPage,
|
rowsPerPage,
|
||||||
|
enabled,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return [checks, checksCount, isLoading, networkError];
|
return [checks, checksCount, isLoading, networkError];
|
||||||
};
|
};
|
||||||
|
|
||||||
export { useFetchChecks };
|
export { useFetchChecksByMonitor, useFetchChecksTeam };
|
||||||
@@ -10,10 +10,11 @@ import NetworkError from "../../../../Components/GenericFallback/NetworkError";
|
|||||||
//Utils
|
//Utils
|
||||||
import { formatDateWithTz } from "../../../../Utils/timeUtils";
|
import { formatDateWithTz } from "../../../../Utils/timeUtils";
|
||||||
import { useSelector } from "react-redux";
|
import { useSelector } from "react-redux";
|
||||||
import { useState, useEffect } from "react";
|
import { useState } from "react";
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useFetchChecks } from "../../../../Hooks/checkHooks";
|
import { useFetchChecksTeam } from "../../../../Hooks/checkHooks";
|
||||||
|
import { useFetchChecksByMonitor } from "../../../../Hooks/checkHooks";
|
||||||
|
|
||||||
const IncidentTable = ({
|
const IncidentTable = ({
|
||||||
shouldRender,
|
shouldRender,
|
||||||
@@ -26,26 +27,41 @@ const IncidentTable = ({
|
|||||||
const uiTimezone = useSelector((state) => state.ui.timezone);
|
const uiTimezone = useSelector((state) => state.ui.timezone);
|
||||||
|
|
||||||
//Local state
|
//Local state
|
||||||
const [teamId, setTeamId] = useState(undefined);
|
|
||||||
const [monitorId, setMonitorId] = useState(undefined);
|
|
||||||
|
|
||||||
const [page, setPage] = useState(0);
|
const [page, setPage] = useState(0);
|
||||||
const [rowsPerPage, setRowsPerPage] = useState(10);
|
const [rowsPerPage, setRowsPerPage] = useState(10);
|
||||||
const selectedMonitorDetails = monitors?.[selectedMonitor];
|
const selectedMonitorDetails = monitors?.[selectedMonitor];
|
||||||
const selectedMonitorType = selectedMonitorDetails?.type;
|
const selectedMonitorType = selectedMonitorDetails?.type;
|
||||||
|
|
||||||
const [checks, checksCount, isLoading, networkError] = useFetchChecks({
|
const [checksMonitor, checksCountMonitor, isLoadingMonitor, networkErrorMonitor] =
|
||||||
status: false,
|
useFetchChecksByMonitor({
|
||||||
monitorId,
|
monitorId: selectedMonitor === "0" ? undefined : selectedMonitor,
|
||||||
teamId,
|
type: selectedMonitorType,
|
||||||
type: selectedMonitorType,
|
status: false,
|
||||||
sortOrder: "desc",
|
sortOrder: "desc",
|
||||||
limit: null,
|
limit: null,
|
||||||
dateRange,
|
dateRange,
|
||||||
filter: filter,
|
filter: filter,
|
||||||
page: page,
|
page: page,
|
||||||
rowsPerPage: rowsPerPage,
|
rowsPerPage: rowsPerPage,
|
||||||
});
|
enabled: selectedMonitor !== "0",
|
||||||
|
});
|
||||||
|
|
||||||
|
const [checksTeam, checksCountTeam, isLoadingTeam, networkErrorTeam] =
|
||||||
|
useFetchChecksTeam({
|
||||||
|
status: false,
|
||||||
|
sortOrder: "desc",
|
||||||
|
limit: null,
|
||||||
|
dateRange,
|
||||||
|
filter: filter,
|
||||||
|
page: page,
|
||||||
|
rowsPerPage: rowsPerPage,
|
||||||
|
enabled: selectedMonitor === "0",
|
||||||
|
});
|
||||||
|
|
||||||
|
const checks = selectedMonitor === "0" ? checksTeam : checksMonitor;
|
||||||
|
const checksCount = selectedMonitor === "0" ? checksCountTeam : checksCountMonitor;
|
||||||
|
const isLoading = isLoadingTeam || isLoadingMonitor;
|
||||||
|
const networkError = selectedMonitor === "0" ? networkErrorTeam : networkErrorMonitor;
|
||||||
|
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
@@ -58,16 +74,6 @@ const IncidentTable = ({
|
|||||||
setRowsPerPage(event.target.value);
|
setRowsPerPage(event.target.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (selectedMonitor === "0") {
|
|
||||||
setTeamId("placeholder"); // TODO this isn't needed any longer, fix hook
|
|
||||||
setMonitorId(undefined);
|
|
||||||
} else {
|
|
||||||
setMonitorId(selectedMonitor);
|
|
||||||
setTeamId(undefined);
|
|
||||||
}
|
|
||||||
}, [selectedMonitor]);
|
|
||||||
|
|
||||||
const headers = [
|
const headers = [
|
||||||
{
|
{
|
||||||
id: "monitorName",
|
id: "monitorName",
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import { useTheme } from "@emotion/react";
|
|||||||
import { useIsAdmin } from "../../../Hooks/useIsAdmin";
|
import { useIsAdmin } from "../../../Hooks/useIsAdmin";
|
||||||
import { useFetchUptimeMonitorById } from "../../../Hooks/monitorHooks";
|
import { useFetchUptimeMonitorById } from "../../../Hooks/monitorHooks";
|
||||||
import useCertificateFetch from "./Hooks/useCertificateFetch";
|
import useCertificateFetch from "./Hooks/useCertificateFetch";
|
||||||
import { useFetchChecks } from "../../../Hooks/checkHooks";
|
import { useFetchChecksByMonitor } from "../../../Hooks/checkHooks";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
@@ -66,16 +66,17 @@ const UptimeDetails = () => {
|
|||||||
|
|
||||||
const monitorType = monitor?.type;
|
const monitorType = monitor?.type;
|
||||||
|
|
||||||
const [checks, checksCount, checksAreLoading, checksNetworkError] = useFetchChecks({
|
const [checks, checksCount, checksAreLoading, checksNetworkError] =
|
||||||
monitorId,
|
useFetchChecksByMonitor({
|
||||||
type: monitorType,
|
monitorId,
|
||||||
sortOrder: "desc",
|
type: monitorType,
|
||||||
limit: null,
|
sortOrder: "desc",
|
||||||
dateRange,
|
limit: null,
|
||||||
filter: null,
|
dateRange,
|
||||||
page,
|
filter: null,
|
||||||
rowsPerPage,
|
page,
|
||||||
});
|
rowsPerPage,
|
||||||
|
});
|
||||||
|
|
||||||
// Handlers
|
// Handlers
|
||||||
const triggerUpdate = () => {
|
const triggerUpdate = () => {
|
||||||
|
|||||||
Reference in new issue
Block a user