Updated Global THreshold Code Quality
This commit is contained in:
1 parent
bf5d7f3c53
commit
8d105e636d
2 files changed
+50
-48
No files matched your search
@@ -300,6 +300,31 @@ const useCreateMonitor = () => {
|
|||||||
return [createMonitor, isLoading];
|
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 useDeleteMonitor = () => {
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@@ -499,6 +524,7 @@ export {
|
|||||||
useFetchUptimeMonitorById,
|
useFetchUptimeMonitorById,
|
||||||
useFetchHardwareMonitorById,
|
useFetchHardwareMonitorById,
|
||||||
useCreateMonitor,
|
useCreateMonitor,
|
||||||
|
useFetchGlobalSettings,
|
||||||
useDeleteMonitor,
|
useDeleteMonitor,
|
||||||
useUpdateMonitor,
|
useUpdateMonitor,
|
||||||
usePauseMonitor,
|
usePauseMonitor,
|
||||||
|
|||||||
@@ -11,7 +11,11 @@ import { capitalizeFirstLetter } from "../../../Utils/stringUtils";
|
|||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useGetNotificationsByTeamId } from "../../../Hooks/useNotifications";
|
import { useGetNotificationsByTeamId } from "../../../Hooks/useNotifications";
|
||||||
import NotificationsConfig from "../../../Components/NotificationConfig";
|
import NotificationsConfig from "../../../Components/NotificationConfig";
|
||||||
import { useUpdateMonitor, useCreateMonitor } from "../../../Hooks/monitorHooks";
|
import {
|
||||||
|
useUpdateMonitor,
|
||||||
|
useCreateMonitor,
|
||||||
|
useFetchGlobalSettings,
|
||||||
|
} from "../../../Hooks/monitorHooks";
|
||||||
|
|
||||||
// MUI
|
// MUI
|
||||||
import { Box, Stack, Typography, Button, ButtonGroup } from "@mui/material";
|
import { Box, Stack, Typography, Button, ButtonGroup } from "@mui/material";
|
||||||
@@ -65,6 +69,7 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
useGetNotificationsByTeamId();
|
useGetNotificationsByTeamId();
|
||||||
const [updateMonitor, isUpdating] = useUpdateMonitor();
|
const [updateMonitor, isUpdating] = useUpdateMonitor();
|
||||||
const [createMonitor, isCreating] = useCreateMonitor();
|
const [createMonitor, isCreating] = useCreateMonitor();
|
||||||
|
const [globalSettings, globalSettingsLoading] = useFetchGlobalSettings();
|
||||||
|
|
||||||
// State
|
// State
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
@@ -87,59 +92,30 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Populate form fields if editing
|
// Populate form fields if editing
|
||||||
// Populate global thresholds from /settings if creating
|
|
||||||
const { authToken } = useSelector((state) => state.auth);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchGlobalThresholds = async () => {
|
if (!isCreate || globalSettingsLoading) return;
|
||||||
if (!isCreate) return;
|
const globalThresholds = globalSettings?.data?.settings?.globalThresholds;
|
||||||
|
|
||||||
try {
|
setInfrastructureMonitor((prev) => ({
|
||||||
const token = authToken;
|
...prev,
|
||||||
if (!token) {
|
cpu: globalThresholds.cpu != null,
|
||||||
console.warn("No token found for fetching global thresholds.");
|
usage_cpu: globalThresholds.cpu != null ? globalThresholds.cpu.toString() : "",
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await axios.get(`${import.meta.env.VITE_APP_API_BASE_URL}/settings`, {
|
memory: globalThresholds.memory != null,
|
||||||
headers: {
|
usage_memory:
|
||||||
Authorization: `Bearer ${token}`,
|
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) {
|
temperature: globalThresholds.temperature != null,
|
||||||
console.warn("No global thresholds found in /settings response.");
|
usage_temperature:
|
||||||
return;
|
globalThresholds.temperature != null
|
||||||
}
|
? globalThresholds.temperature.toString()
|
||||||
|
: "",
|
||||||
setInfrastructureMonitor((prev) => ({
|
}));
|
||||||
...prev,
|
}, [isCreate, globalSettings]);
|
||||||
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]);
|
|
||||||
|
|
||||||
// Handlers
|
// Handlers
|
||||||
const onSubmit = async (event) => {
|
const onSubmit = async (event) => {
|
||||||
|
|||||||
Reference in new issue
Block a user