- update safe checking for string util

- remove required for threshold in joi
-  refine if logic and update no error checking with values due to the change to set null instead of delete error entry
- move customthreshold component to under create infrastructure folder
- correct interval setting and threshold default value and fix typo
This commit is contained in:
Shemy Gan committed 2024-11-15 14:49:27 -05:00
1 parent c3859c7507
commit 2f8638cf04
5 files changed
+29 -24

No files matched your search

@@ -1,6 +1,6 @@
import { Box, Stack, Typography } from "@mui/material"; import { Box, Stack, Typography } from "@mui/material";
import Field from "../Inputs/Field"; import Field from "../../../../Components/Inputs/Field";
import Checkbox from "../Inputs/Checkbox"; import Checkbox from "../../../../Components/Inputs/Checkbox";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
@@ -2,7 +2,7 @@ import { useState } from "react";
import { Box, Stack, Typography } from "@mui/material"; import { Box, Stack, Typography } from "@mui/material";
import LoadingButton from "@mui/lab/LoadingButton"; import LoadingButton from "@mui/lab/LoadingButton";
import { useSelector, useDispatch } from "react-redux"; import { useSelector, useDispatch } from "react-redux";
import { infrastractureMonitorValidation } from "../../../Validation/validation"; import { infrastructureMonitorValidation } from "../../../Validation/validation";
import { import {
createInfrastructureMonitor, createInfrastructureMonitor,
checkInfrastructureEndpointResolution, checkInfrastructureEndpointResolution,
@@ -18,7 +18,7 @@ import Checkbox from "../../../Components/Inputs/Checkbox";
import Breadcrumbs from "../../../Components/Breadcrumbs"; import Breadcrumbs from "../../../Components/Breadcrumbs";
import { buildErrors, hasValidationErrors } from "../../../Validation/error"; import { buildErrors, hasValidationErrors } from "../../../Validation/error";
import { capitalizeFirstLetter } from "../../../Utils/stringUtils"; import { capitalizeFirstLetter } from "../../../Utils/stringUtils";
import { CustomThreshold } from "../../../Components/CustomThreshold"; import { CustomThreshold } from "../CreateMonitor/CustomThreshold";
const CreateInfrastructureMonitor = () => { const CreateInfrastructureMonitor = () => {
const [infrastructureMonitor, setInfrastructureMonitor] = useState({ const [infrastructureMonitor, setInfrastructureMonitor] = useState({
@@ -27,15 +27,15 @@ const CreateInfrastructureMonitor = () => {
notifications: [], notifications: [],
interval: 15, interval: 15,
cpu: false, cpu: false,
usage_cpu: "", usage_cpu: 0,
memory: false, memory: false,
usage_memory: "", usage_memory: 0,
disk: false, disk: false,
usage_disk: "", usage_disk: 0,
secret: "", secret: "",
}); });
const MS_PER_MINUTE = 60000; const MS_PER_SECOND = 1000;
const THRESHOLD_FIELD_PREFIX = "usage_"; const THRESHOLD_FIELD_PREFIX = "usage_";
const HARDWARE_MONITOR_TYPES = ["cpu", "memory", "disk"]; const HARDWARE_MONITOR_TYPES = ["cpu", "memory", "disk"];
const { user, authToken } = useSelector((state) => state.auth); const { user, authToken } = useSelector((state) => state.auth);
@@ -78,7 +78,7 @@ const CreateInfrastructureMonitor = () => {
event.preventDefault(); event.preventDefault();
const { value, id } = event.target; const { value, id } = event.target;
if (id?.startsWith("notify-email-")) return; if (id?.startsWith("notify-email-")) return;
const { error } = infrastractureMonitorValidation.validate( const { error } = infrastructureMonitorValidation.validate(
{ [id ?? appenedID]: value }, { [id ?? appenedID]: value },
{ {
abortEarly: false, abortEarly: false,
@@ -156,10 +156,10 @@ const CreateInfrastructureMonitor = () => {
infrastructureMonitor.name === "" infrastructureMonitor.name === ""
? infrastructureMonitor.url ? infrastructureMonitor.url
: infrastructureMonitor.name, : infrastructureMonitor.name,
interval: infrastructureMonitor.interval * MS_PER_MINUTE, interval: infrastructureMonitor.interval * MS_PER_SECOND,
}; };
delete form.notifications; delete form.notifications;
if (hasValidationErrors(form, infrastractureMonitorValidation, setErrors)) { if (hasValidationErrors(form, infrastructureMonitorValidation, setErrors)) {
return; return;
} else { } else {
const checkEndpointAction = await dispatch( const checkEndpointAction = await dispatch(
+12 -3
View File
@@ -3,6 +3,15 @@
* @param {string} str String whose first letter is to be capitalized * @param {string} str String whose first letter is to be capitalized
* @returns A string with first letter capitalized * @returns A string with first letter capitalized
*/ */
export const capitalizeFirstLetter = (str) =>{ export const capitalizeFirstLetter = (str) => {
return str?.charAt(0).toUpperCase() + str.slice(1); if (str === null || str === undefined) {
} return "";
}
if (typeof str !== "string") {
throw new TypeError("Input must be a string");
}
if (str.length === 0) {
return "";
}
return str.charAt(0).toUpperCase() + str.slice(1);
};
+4 -4
View File
@@ -28,17 +28,17 @@ const hasValidationErrors = (form, validation, setErrors) => {
newErrors[err.path[0]] = err.message ?? "Validation error"; newErrors[err.path[0]] = err.message ?? "Validation error";
} }
// Handle conditionally usage number required cases // Handle conditionally usage number required cases
if (!form.cpu || (form.cpu && form.usage_cpu)) { if (!form.cpu || form.usage_cpu) {
newErrors["usage_cpu"] = null; newErrors["usage_cpu"] = null;
} }
if (!form.memory || (form.memory && form.usage_memory)) { if (!form.memory || form.usage_memory) {
newErrors["usage_memory"] = null; newErrors["usage_memory"] = null;
} }
if (!form.disk || (form.disk && form.usage_disk)) { if (!form.disk || form.usage_disk) {
newErrors["usage_disk"] = null; newErrors["usage_disk"] = null;
} }
}); });
if (Object.keys(newErrors).length > 0) { if (Object.values(newErrors).some(v=> v)) {
setErrors(newErrors); setErrors(newErrors);
return true; return true;
} else { } else {
+2 -6
View File
@@ -1,7 +1,6 @@
import joi from "joi"; import joi from "joi";
import dayjs from "dayjs"; import dayjs from "dayjs";
const THRESHOLD_COMMON_MSG = "Threshold is required.";
const THRESHOLD_COMMON_BASE_MSG = "Threshold must be a number."; const THRESHOLD_COMMON_BASE_MSG = "Threshold must be a number.";
const nameSchema = joi const nameSchema = joi
@@ -176,7 +175,7 @@ const advancedSettingsValidation = joi.object({
pagespeedApiKey: joi.string().allow(""), pagespeedApiKey: joi.string().allow(""),
}); });
const infrastractureMonitorValidation = joi.object({ const infrastructureMonitorValidation = joi.object({
url: joi.string().uri({ allowRelative: true }).trim().messages({ url: joi.string().uri({ allowRelative: true }).trim().messages({
"string.empty": "This field is required.", "string.empty": "This field is required.",
"string.uri": "The URL you provided is not valid.", "string.uri": "The URL you provided is not valid.",
@@ -187,18 +186,15 @@ const infrastractureMonitorValidation = joi.object({
secret: joi.string().trim().messages({ "string.empty": "This field is required." }), secret: joi.string().trim().messages({ "string.empty": "This field is required." }),
usage_cpu: joi.number().messages({ usage_cpu: joi.number().messages({
"number.base": THRESHOLD_COMMON_BASE_MSG, "number.base": THRESHOLD_COMMON_BASE_MSG,
"any.required": THRESHOLD_COMMON_MSG,
}), }),
cpu: joi.boolean(), cpu: joi.boolean(),
memory: joi.boolean(), memory: joi.boolean(),
disk: joi.boolean(), disk: joi.boolean(),
usage_memory: joi.number().messages({ usage_memory: joi.number().messages({
"number.base": THRESHOLD_COMMON_BASE_MSG, "number.base": THRESHOLD_COMMON_BASE_MSG,
"any.required": THRESHOLD_COMMON_MSG,
}), }),
usage_disk: joi.number().messages({ usage_disk: joi.number().messages({
"number.base": THRESHOLD_COMMON_BASE_MSG, "number.base": THRESHOLD_COMMON_BASE_MSG,
"any.required": THRESHOLD_COMMON_MSG,
}), }),
// usage_temperature: joi.number().messages({ // usage_temperature: joi.number().messages({
// "number.base": "Temperature must be a number.", // "number.base": "Temperature must be a number.",
@@ -222,5 +218,5 @@ export {
settingsValidation, settingsValidation,
maintenanceWindowValidation, maintenanceWindowValidation,
advancedSettingsValidation, advancedSettingsValidation,
infrastractureMonitorValidation, infrastructureMonitorValidation,
}; };