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