From da7d9c02c870856bf721a99d95df91493b189278 Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Wed, 30 Jul 2025 00:25:53 +0530 Subject: [PATCH] Used Parent State And Set Minor Changes --- client/src/Hooks/monitorHooks.js | 2 +- .../Settings/SettingsGlobalThresholds.jsx | 43 +++++-------------- 2 files changed, 11 insertions(+), 34 deletions(-) diff --git a/client/src/Hooks/monitorHooks.js b/client/src/Hooks/monitorHooks.js index 1d57ffd5f..616a37877 100644 --- a/client/src/Hooks/monitorHooks.js +++ b/client/src/Hooks/monitorHooks.js @@ -302,7 +302,7 @@ const useCreateMonitor = () => { const useFetchGlobalSettings = () => { const [isLoading, setIsLoading] = useState(true); - const [globalSettings, setGlobalSettings] = useState(null); + const [globalSettings, setGlobalSettings] = useState(undefined); useEffect(() => { const fetchGlobalSettings = async () => { try { diff --git a/client/src/Pages/Settings/SettingsGlobalThresholds.jsx b/client/src/Pages/Settings/SettingsGlobalThresholds.jsx index 96f14e62d..b4d2581d2 100644 --- a/client/src/Pages/Settings/SettingsGlobalThresholds.jsx +++ b/client/src/Pages/Settings/SettingsGlobalThresholds.jsx @@ -6,7 +6,6 @@ import TextInput from "../../Components/Inputs/TextInput"; import { useTheme } from "@emotion/react"; import { PropTypes } from "prop-types"; -import { useState, useEffect } from "react"; import { useTranslation } from "react-i18next"; const SettingsGlobalThresholds = ({ @@ -18,26 +17,7 @@ const SettingsGlobalThresholds = ({ const { t } = useTranslation(); // For language translation const theme = useTheme(); // MUI theme access - // Local state for thresholds - const [thresholds, setThresholds] = useState({ - cpu: "", - memory: "", - disk: "", - temperature: "", - }); - - // Load existing thresholds from settingsData - - if (settingsData?.settings?.globalThresholds) { - setThresholds({ - cpu: settingsData.settings.globalThresholds.cpu || "", - memory: settingsData.settings.globalThresholds.memory || "", - disk: settingsData.settings.globalThresholds.disk || "", - temperature: settingsData.settings.globalThresholds.temperature || "", - }); - } - - // Handles input change and updates state & parent data + // Handles input change and updates parent state const handleChange = (e, min, max) => { const { name, value } = e.target; @@ -47,19 +27,16 @@ const SettingsGlobalThresholds = ({ (!isNaN(numValue) && isFinite(numValue) && numValue >= min && numValue <= max); if (isValidNumber) { - const updatedThresholds = { - ...thresholds, - [name]: value, - }; - - setThresholds(updatedThresholds); - setSettingsData({ - ...settingsData, + setSettingsData((prev) => ({ + ...prev, settings: { - ...settingsData.settings, - globalThresholds: updatedThresholds, + ...prev.settings, + globalThresholds: { + ...prev.settings?.globalThresholds, + [name]: value, + }, }, - }); + })); } }; @@ -98,7 +75,7 @@ const SettingsGlobalThresholds = ({ label={label} placeholder={`${min} - ${max}`} type="number" - value={thresholds[name]} + value={settingsData?.settings?.globalThresholds?.[name] || ""} onChange={(e) => handleChange(e, min, max)} /> ))}