client/src/Pages/Infrastructure/Create/index.jsx

This commit is contained in:
singh-kanwarpreet committed 2025-07-30 19:18:24 +05:30
1 parent d4ecb26ffe
commit 0ecbf48f5d
1 file changed
+35 -30
@@ -93,10 +93,7 @@ const CreateInfrastructureMonitor = () => {
// Populate form fields if editing // Populate form fields if editing
useEffect(() => { useEffect(() => {
if (monitor == undefined && (!isCreate || globalSettingsLoading)) return;
const globalThresholds = globalSettings?.data?.settings?.globalThresholds; const globalThresholds = globalSettings?.data?.settings?.globalThresholds;
// Define default empty string for all thresholds
const defaultThresholds = { const defaultThresholds = {
cpu: "", cpu: "",
memory: "", memory: "",
@@ -104,37 +101,45 @@ const CreateInfrastructureMonitor = () => {
temperature: "", temperature: "",
}; };
if (isCreate) {
// If global settings are not loaded yet, use default thresholds
if (globalSettingsLoading) return;
// Create mode: use global thresholds
setInfrastructureMonitor((prev) => ({ setInfrastructureMonitor((prev) => ({
...prev, ...prev,
url: monitor != undefined ? monitor.url.replace(/^https?:\/\//, "") : "", url: "",
name: monitor != undefined ? monitor.name || "" : "", name: "",
notifications: monitor != undefined ? monitor.notifications : [], notifications: [],
interval: monitor != undefined ? monitor.interval / MS_PER_MINUTE : 0.25, interval: 0.25,
cpu: globalThresholds.cpu != null, cpu: globalThresholds?.cpu != null,
usage_cpu: usage_cpu: globalThresholds?.cpu?.toString() || defaultThresholds.cpu,
globalThresholds.cpu != null memory: globalThresholds?.memory != null,
? globalThresholds.cpu.toString() usage_memory: globalThresholds?.memory?.toString() || defaultThresholds.memory,
: defaultThresholds.cpu, disk: globalThresholds?.disk != null,
usage_disk: globalThresholds?.disk?.toString() || defaultThresholds.disk,
memory: globalThresholds.memory != null, temperature: globalThresholds?.temperature != null,
usage_memory:
globalThresholds.memory != null
? globalThresholds.memory.toString()
: defaultThresholds.memory,
disk: globalThresholds.disk != null,
usage_disk:
globalThresholds.disk != null
? globalThresholds.disk.toString()
: defaultThresholds.disk,
temperature: globalThresholds.temperature != null,
usage_temperature: usage_temperature:
globalThresholds.temperature != null globalThresholds?.temperature?.toString() || defaultThresholds.temperature,
? globalThresholds.temperature.toString()
: defaultThresholds.temperature,
})); }));
}, [isCreate, globalSettings, monitor, globalSettingsLoading]); } else if (monitor) {
// Edit mode: use monitor only, ignore global thresholds
setInfrastructureMonitor((prev) => ({
...prev,
url: monitor.url?.replace(/^https?:\/\//, "") || "",
name: monitor.name || "",
notifications: monitor.notifications || [],
interval: monitor.interval / MS_PER_MINUTE,
cpu: !!monitor.thresholds?.usage_cpu ?? false,
usage_cpu: monitor.thresholds?.usage_cpu * 100?.toString() || "",
memory: !!monitor.thresholds?.usage_memory ?? false,
usage_memory: monitor.thresholds?.usage_memory * 100?.toString() || "",
disk: !!monitor.thresholds?.usage_disk ?? false,
usage_disk: monitor.thresholds?.usage_disk * 100?.toString() || "",
temperature: !!monitor.thresholds?.usage_temperature ?? false,
usage_temperature: monitor.thresholds?.usage_temperature * 100?.toString() || "",
}));
}
}, [isCreate, monitor, globalSettings, globalSettingsLoading]);
// Handlers // Handlers
const onSubmit = async (event) => { const onSubmit = async (event) => {