From dc6c947043649365a2a4f484c5d5d8bcaac74ed6 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 14 Mar 2025 10:06:10 -0700 Subject: [PATCH] add hook for fetching monitors with summary --- src/Hooks/useFetchMonitorsWithSummary.js | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/Hooks/useFetchMonitorsWithSummary.js diff --git a/src/Hooks/useFetchMonitorsWithSummary.js b/src/Hooks/useFetchMonitorsWithSummary.js new file mode 100644 index 000000000..009984738 --- /dev/null +++ b/src/Hooks/useFetchMonitorsWithSummary.js @@ -0,0 +1,36 @@ +import { useEffect, useState } from "react"; +import { networkService } from "../main"; +import { createToast } from "../Utils/toastUtils"; + +export const useFetchMonitorsWithSummary = ({ teamId, types }) => { + const [isLoading, setIsLoading] = useState(false); + const [monitors, setMonitors] = useState(undefined); + const [monitorsSummary, setMonitorsSummary] = useState(undefined); + const [networkError, setNetworkError] = useState(false); + + useEffect(() => { + const fetchMonitors = async () => { + try { + setIsLoading(true); + const res = await networkService.getMonitorsWithSummaryByTeamId({ + teamId, + types, + }); + const { monitors, summary } = res?.data?.data ?? {}; + setMonitors(monitors); + setMonitorsSummary(summary); + } catch (error) { + setNetworkError(true); + createToast({ + body: error.message, + }); + } finally { + setIsLoading(false); + } + }; + fetchMonitors(); + }, [teamId, types]); + return [monitors, monitorsSummary, isLoading, networkError]; +}; + +export default useFetchMonitorsWithSummary;