Files
Checkmate/src/Pages/Uptime/Details/Hooks/useChecksFetch.jsx
T

51 lines
1.3 KiB
React
Raw Normal View History

2025-01-27 14:55:55 -08:00
import { useState } from "react";
import { useEffect } from "react";
import { networkService } from "../../../../main";
2025-02-01 08:13:21 -08:00
import { createToast } from "../../../../Utils/toastUtils";
2025-03-11 10:15:47 -07:00
export const useChecksFetch = ({
monitorId,
monitorType,
dateRange,
page,
rowsPerPage,
}) => {
const [checks, setChecks] = useState(undefined);
const [checksCount, setChecksCount] = useState(undefined);
2025-02-01 08:13:21 -08:00
const [isLoading, setIsLoading] = useState(false);
const [networkError, setNetworkError] = useState(false);
2025-01-27 14:55:55 -08:00
useEffect(() => {
2025-03-11 22:27:43 +05:30
if (!monitorType) {
2025-03-11 10:15:47 -07:00
console.warn("Monitor Type is not provided. Fetching checks will not proceed.");
return;
2025-03-11 22:27:43 +05:30
}
2025-03-11 03:23:04 +05:30
2025-01-27 14:55:55 -08:00
const fetchChecks = async () => {
try {
2025-02-01 08:13:21 -08:00
setIsLoading(true);
2025-01-27 14:55:55 -08:00
const res = await networkService.getChecksByMonitor({
monitorId: monitorId,
2025-03-11 03:23:04 +05:30
type: monitorType,
2025-01-27 14:55:55 -08:00
sortOrder: "desc",
limit: null,
dateRange: dateRange,
filter: null,
page: page,
rowsPerPage: rowsPerPage,
});
setChecks(res.data.data.checks);
setChecksCount(res.data.data.checksCount);
} catch (error) {
2025-02-01 08:13:21 -08:00
setNetworkError(true);
createToast({ body: error.message });
2025-01-27 14:55:55 -08:00
} finally {
2025-02-01 08:13:21 -08:00
setIsLoading(false);
2025-01-27 14:55:55 -08:00
}
};
fetchChecks();
2025-03-11 03:23:04 +05:30
}, [monitorId, monitorType, dateRange, page, rowsPerPage]);
2025-01-27 14:55:55 -08:00
2025-02-01 08:13:21 -08:00
return [checks, checksCount, isLoading, networkError];
2025-01-27 14:55:55 -08:00
};
export default useChecksFetch;