Files
Checkmate/client/src/Hooks/checkHooks.js
T

45 lines
1.2 KiB
JavaScript
Raw Normal View History

2025-01-27 14:55:55 -08:00
import { useState } from "react";
import { useEffect } from "react";
2025-06-12 12:44:35 +08:00
import { networkService } from "../main";
import { createToast } from "../Utils/toastUtils";
const useFetchChecks = ({ 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
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
};
2025-06-12 12:44:35 +08:00
export { useFetchChecks };