From 8d105e636db84b64678dfcfd0662098542c1a0ee Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Tue, 29 Jul 2025 00:42:56 +0530 Subject: [PATCH] Updated Global THreshold Code Quality --- client/src/Hooks/monitorHooks.js | 26 +++++++ .../src/Pages/Infrastructure/Create/index.jsx | 72 +++++++------------ 2 files changed, 50 insertions(+), 48 deletions(-) diff --git a/client/src/Hooks/monitorHooks.js b/client/src/Hooks/monitorHooks.js index 4f0e9c049..6b8cb983b 100644 --- a/client/src/Hooks/monitorHooks.js +++ b/client/src/Hooks/monitorHooks.js @@ -300,6 +300,31 @@ const useCreateMonitor = () => { return [createMonitor, isLoading]; }; +const useFetchGlobalSettings = () => { + const [isLoading, setIsLoading] = useState(true); + const [globalSettings, setGlobalSettings] = useState(null); + + useEffect(() => { + const fetchGlobalSettings = async () => { + try { + const res = await networkService.getAppSettings(); + setGlobalSettings(res?.data); + setIsLoading(false); + } catch (error) { + console.error("Failed to fetch global settings:", error); + } finally { + setIsLoading(false); + } + }; + + fetchGlobalSettings(); + }, []); + + return [globalSettings, isLoading]; +}; + + + const useDeleteMonitor = () => { const [isLoading, setIsLoading] = useState(false); const navigate = useNavigate(); @@ -499,6 +524,7 @@ export { useFetchUptimeMonitorById, useFetchHardwareMonitorById, useCreateMonitor, + useFetchGlobalSettings, useDeleteMonitor, useUpdateMonitor, usePauseMonitor, diff --git a/client/src/Pages/Infrastructure/Create/index.jsx b/client/src/Pages/Infrastructure/Create/index.jsx index fa6ac4fcb..151383cfb 100644 --- a/client/src/Pages/Infrastructure/Create/index.jsx +++ b/client/src/Pages/Infrastructure/Create/index.jsx @@ -11,7 +11,11 @@ import { capitalizeFirstLetter } from "../../../Utils/stringUtils"; import { useTranslation } from "react-i18next"; import { useGetNotificationsByTeamId } from "../../../Hooks/useNotifications"; import NotificationsConfig from "../../../Components/NotificationConfig"; -import { useUpdateMonitor, useCreateMonitor } from "../../../Hooks/monitorHooks"; +import { + useUpdateMonitor, + useCreateMonitor, + useFetchGlobalSettings, +} from "../../../Hooks/monitorHooks"; // MUI import { Box, Stack, Typography, Button, ButtonGroup } from "@mui/material"; @@ -65,6 +69,7 @@ const CreateInfrastructureMonitor = () => { useGetNotificationsByTeamId(); const [updateMonitor, isUpdating] = useUpdateMonitor(); const [createMonitor, isCreating] = useCreateMonitor(); + const [globalSettings, globalSettingsLoading] = useFetchGlobalSettings(); // State const [errors, setErrors] = useState({}); @@ -87,59 +92,30 @@ const CreateInfrastructureMonitor = () => { }); // Populate form fields if editing - // Populate global thresholds from /settings if creating - const { authToken } = useSelector((state) => state.auth); useEffect(() => { - const fetchGlobalThresholds = async () => { - if (!isCreate) return; + if (!isCreate || globalSettingsLoading) return; + const globalThresholds = globalSettings?.data?.settings?.globalThresholds; - try { - const token = authToken; - if (!token) { - console.warn("No token found for fetching global thresholds."); - return; - } + setInfrastructureMonitor((prev) => ({ + ...prev, + cpu: globalThresholds.cpu != null, + usage_cpu: globalThresholds.cpu != null ? globalThresholds.cpu.toString() : "", - const response = await axios.get(`${import.meta.env.VITE_APP_API_BASE_URL}/settings`, { - headers: { - Authorization: `Bearer ${token}`, - }, - }); + memory: globalThresholds.memory != null, + usage_memory: + globalThresholds.memory != null ? globalThresholds.memory.toString() : "", - const globalThresholds = response.data?.data?.settings?.globalThresholds; + disk: globalThresholds.disk != null, + usage_disk: globalThresholds.disk != null ? globalThresholds.disk.toString() : "", - if (!globalThresholds) { - console.warn("No global thresholds found in /settings response."); - return; - } - - setInfrastructureMonitor((prev) => ({ - ...prev, - cpu: globalThresholds.cpu != null, - usage_cpu: globalThresholds.cpu != null ? globalThresholds.cpu.toString() : "", - - memory: globalThresholds.memory != null, - usage_memory: - globalThresholds.memory != null ? globalThresholds.memory.toString() : "", - - disk: globalThresholds.disk != null, - usage_disk: - globalThresholds.disk != null ? globalThresholds.disk.toString() : "", - - temperature: globalThresholds.temperature != null, - usage_temperature: - globalThresholds.temperature != null - ? globalThresholds.temperature.toString() - : "", - })); - } catch (error) { - console.error("Failed to fetch global thresholds:", error); - } - }; - - fetchGlobalThresholds(); - }, [isCreate, user]); + temperature: globalThresholds.temperature != null, + usage_temperature: + globalThresholds.temperature != null + ? globalThresholds.temperature.toString() + : "", + })); + }, [isCreate, globalSettings]); // Handlers const onSubmit = async (event) => {