Improved Code Quality

This commit is contained in:
singh-kanwarpreet committed 2025-07-29 21:35:30 +05:30
1 parent ad06544a64
commit dbea93f9ba
5 files changed
+63 -47

No files matched your search

+17 -19
View File
@@ -301,29 +301,27 @@ const useCreateMonitor = () => {
};
const useFetchGlobalSettings = () => {
const [isLoading, setIsLoading] = useState(true);
const [globalSettings, setGlobalSettings] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [globalSettings, setGlobalSettings] = useState(null);
useEffect(() => {
const fetchGlobalSettings = async () => {
try {
const res = await networkService.getAppSettings();
setGlobalSettings(res?.data);
} catch (error) {
console.error("Failed to fetch global settings:", error);
createToast({ body: "Failed to load global settings" });
} finally {
setIsLoading(false);
}
};
useEffect(() => {
const fetchGlobalSettings = async () => {
try {
const res = await networkService.getAppSettings();
setGlobalSettings(res?.data);
} catch (error) {
console.error("Failed to fetch global settings:", error);
} finally {
setIsLoading(false);
}
};
fetchGlobalSettings();
}, []);
fetchGlobalSettings();
}, []);
return [globalSettings, isLoading];
return [globalSettings, isLoading];
};
const useDeleteMonitor = () => {
const [isLoading, setIsLoading] = useState(false);
const navigate = useNavigate();
@@ -3,7 +3,6 @@ import { useTheme } from "@emotion/react";
import { useParams } from "react-router-dom";
import { useState, useEffect } from "react";
import { useSelector } from "react-redux";
import axios from "axios";
// Utility and Network
import { infrastructureMonitorValidation } from "../../../Validation/validation";
import { useFetchHardwareMonitorById } from "../../../Hooks/monitorHooks";
@@ -95,25 +94,41 @@ const CreateInfrastructureMonitor = () => {
useEffect(() => {
if (!isCreate || globalSettingsLoading) return;
const globalThresholds = globalSettings?.data?.settings?.globalThresholds;
const globalThresholds = globalSettings?.data?.settings?.globalThresholds || {};
// Define default empty string for all thresholds
const defaultThresholds = {
cpu: "",
memory: "",
disk: "",
temperature: "",
};
setInfrastructureMonitor((prev) => ({
...prev,
cpu: globalThresholds.cpu != null,
usage_cpu: globalThresholds.cpu != null ? globalThresholds.cpu.toString() : "",
usage_cpu:
globalThresholds.cpu != null
? globalThresholds.cpu.toString()
: defaultThresholds.cpu,
memory: globalThresholds.memory != null,
usage_memory:
globalThresholds.memory != null ? globalThresholds.memory.toString() : "",
globalThresholds.memory != null
? globalThresholds.memory.toString()
: defaultThresholds.memory,
disk: globalThresholds.disk != null,
usage_disk: globalThresholds.disk != null ? globalThresholds.disk.toString() : "",
usage_disk:
globalThresholds.disk != null
? globalThresholds.disk.toString()
: defaultThresholds.disk,
temperature: globalThresholds.temperature != null,
usage_temperature:
globalThresholds.temperature != null
? globalThresholds.temperature.toString()
: "",
: defaultThresholds.temperature,
}));
}, [isCreate, globalSettings]);
@@ -15,8 +15,8 @@ const SettingsGlobalThresholds = ({
settingsData,
setSettingsData,
}) => {
const { t } = useTranslation(); // For language translation
const theme = useTheme(); // MUI theme access
const { t } = useTranslation(); // For language translation
const theme = useTheme(); // MUI theme access
// Local state for thresholds
const [thresholds, setThresholds] = useState({
@@ -42,19 +42,18 @@ const SettingsGlobalThresholds = ({
const handleChange = (e, min, max) => {
const { name, value } = e.target;
// Allow empty or numeric value within range
if (
const numValue = parseFloat(value);
const isValidNumber =
value === "" ||
(/^\d+$/.test(value) && Number(value) >= min && Number(value) <= max)
) {
(!isNaN(numValue) && isFinite(numValue) && numValue >= min && numValue <= max);
if (isValidNumber) {
const updatedThresholds = {
...thresholds,
[name]: value,
};
setThresholds(updatedThresholds);
// Update settingsData without enabling/disabling metrics
setSettingsData({
...settingsData,
settings: {
@@ -72,7 +71,10 @@ const SettingsGlobalThresholds = ({
<ConfigBox>
{/* Header and description */}
<Box>
<Typography component="h1" variant="h2">
<Typography
component="h1"
variant="h2"
>
{t("settingsPage.globalThresholds.title", "Global Thresholds")}
</Typography>
<Typography sx={HEADING_SX}>
+6 -6
View File
@@ -66,12 +66,12 @@ const AppSettingsSchema = mongoose.Schema(
default: 1,
},
globalThresholds: {
cpu: { type: Number },
memory: { type: Number},
disk: { type: Number},
temperature: { type: Number},
}
},
cpu: { type: Number },
memory: { type: Number },
disk: { type: Number },
temperature: { type: Number },
},
},
{
timestamps: true,
}
+8 -7
View File
@@ -417,15 +417,16 @@ const updateAppSettingsBodyValidation = joi.object({
systemEmailRequireTLS: joi.boolean(),
systemEmailRejectUnauthorized: joi.boolean(),
globalThresholds: joi.object({
cpu: joi.number().min(1).max(100).allow("").optional(),
memory: joi.number().min(1).max(100).allow("").optional(),
disk: joi.number().min(1).max(100).allow("").optional(),
temperature: joi.number().min(1).max(150).allow("").optional(),
}).optional(),
globalThresholds: joi
.object({
cpu: joi.number().min(1).max(100).allow("").optional(),
memory: joi.number().min(1).max(100).allow("").optional(),
disk: joi.number().min(1).max(100).allow("").optional(),
temperature: joi.number().min(1).max(150).allow("").optional(),
})
.optional(),
});
//****************************************
// Status Page Validation
//****************************************