- Extract CustomThreshold component
- Refine Error setting in validation to remove any leftover errors - Add back the await as it is indeed needed even though syntax checker prompts to remove it
This commit is contained in:
1 parent
2e51f025f9
commit
8737dbfd5b
3 files changed
+89
-61
No files matched your search
@@ -0,0 +1,72 @@
|
|||||||
|
import { Box, Stack, Typography } from "@mui/material";
|
||||||
|
import Field from "../Inputs/Field";
|
||||||
|
import Checkbox from "../Inputs/Checkbox";
|
||||||
|
import { useTheme } from "@emotion/react";
|
||||||
|
import PropTypes from "prop-types";
|
||||||
|
|
||||||
|
export const CustomThreshold = ({
|
||||||
|
checkboxId,
|
||||||
|
checkboxLabel,
|
||||||
|
onCheckboxChange,
|
||||||
|
fieldId,
|
||||||
|
onFieldChange,
|
||||||
|
onFieldBlur,
|
||||||
|
alertUnit,
|
||||||
|
infrastructureMonitor,
|
||||||
|
errors
|
||||||
|
}) =>
|
||||||
|
{
|
||||||
|
const theme = useTheme();
|
||||||
|
return (
|
||||||
|
<Stack
|
||||||
|
direction={"row"}
|
||||||
|
sx={{
|
||||||
|
width: "50%",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Box>
|
||||||
|
<Checkbox
|
||||||
|
id={checkboxId}
|
||||||
|
label={checkboxLabel}
|
||||||
|
isChecked={infrastructureMonitor[checkboxId]}
|
||||||
|
onChange={onCheckboxChange}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
<Stack
|
||||||
|
direction={"row"}
|
||||||
|
sx={{
|
||||||
|
justifyContent: "flex-end",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Field
|
||||||
|
type="number"
|
||||||
|
className="field-infrastructure-alert"
|
||||||
|
id={fieldId}
|
||||||
|
value={infrastructureMonitor[fieldId]}
|
||||||
|
onBlur={onFieldBlur}
|
||||||
|
onChange={onFieldChange}
|
||||||
|
error={errors[fieldId]}
|
||||||
|
disabled={!infrastructureMonitor[checkboxId]}
|
||||||
|
></Field>
|
||||||
|
<Typography
|
||||||
|
component="p"
|
||||||
|
m={theme.spacing(3)}
|
||||||
|
>
|
||||||
|
{alertUnit}
|
||||||
|
</Typography>
|
||||||
|
</Stack>
|
||||||
|
</Stack>
|
||||||
|
)}
|
||||||
|
|
||||||
|
CustomThreshold.propTypes = {
|
||||||
|
checkboxId: PropTypes.string.isRequired,
|
||||||
|
checkboxLabel: PropTypes.string.isRequired,
|
||||||
|
onCheckboxChange: PropTypes.func.isRequired,
|
||||||
|
fieldId: PropTypes.string.isRequired,
|
||||||
|
onFieldChange: PropTypes.func.isRequired,
|
||||||
|
onFieldBlur: PropTypes.func.isRequired,
|
||||||
|
alertUnit: PropTypes.string.isRequired,
|
||||||
|
infrastructureMonitor: PropTypes.object.isRequired,
|
||||||
|
errors: PropTypes.object.isRequired,
|
||||||
|
};
|
||||||
@@ -18,6 +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";
|
||||||
|
|
||||||
const CreateInfrastructureMonitor = () => {
|
const CreateInfrastructureMonitor = () => {
|
||||||
const [infrastructureMonitor, setInfrastructureMonitor] = useState({
|
const [infrastructureMonitor, setInfrastructureMonitor] = useState({
|
||||||
@@ -52,55 +53,6 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
const alertErrKeyLen = Object.keys(errors)
|
const alertErrKeyLen = Object.keys(errors)
|
||||||
.filter(k => k.startsWith(THRESHOLD_FIELD_PREFIX)).length
|
.filter(k => k.startsWith(THRESHOLD_FIELD_PREFIX)).length
|
||||||
|
|
||||||
const CustomAlertStack = ({
|
|
||||||
checkboxId,
|
|
||||||
checkboxLabel,
|
|
||||||
fieldId,
|
|
||||||
alertUnit,
|
|
||||||
onChange,
|
|
||||||
onBlur,
|
|
||||||
}) => (
|
|
||||||
<Stack
|
|
||||||
direction={"row"}
|
|
||||||
sx={{
|
|
||||||
width: "50%",
|
|
||||||
justifyContent: "space-between",
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Box>
|
|
||||||
<Checkbox
|
|
||||||
id={checkboxId}
|
|
||||||
label={checkboxLabel}
|
|
||||||
isChecked={infrastructureMonitor[checkboxId]}
|
|
||||||
onChange={handleCustomAlertCheckChange}
|
|
||||||
/>
|
|
||||||
</Box>
|
|
||||||
<Stack
|
|
||||||
direction={"row"}
|
|
||||||
sx={{
|
|
||||||
justifyContent: "flex-end",
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Field
|
|
||||||
type="number"
|
|
||||||
className="field-infrastructure-alert"
|
|
||||||
id={fieldId}
|
|
||||||
value={infrastructureMonitor[fieldId]}
|
|
||||||
onBlur={onBlur}
|
|
||||||
onChange={onChange}
|
|
||||||
error={errors[fieldId]}
|
|
||||||
disabled={!infrastructureMonitor[checkboxId]}
|
|
||||||
></Field>
|
|
||||||
<Typography
|
|
||||||
component="p"
|
|
||||||
m={theme.spacing(3)}
|
|
||||||
>
|
|
||||||
{alertUnit}
|
|
||||||
</Typography>
|
|
||||||
</Stack>
|
|
||||||
</Stack>
|
|
||||||
);
|
|
||||||
|
|
||||||
const handleCustomAlertCheckChange = (event) => {
|
const handleCustomAlertCheckChange = (event) => {
|
||||||
const { value, id } = event.target;
|
const { value, id } = event.target;
|
||||||
setInfrastructureMonitor((prev) => {
|
setInfrastructureMonitor((prev) => {
|
||||||
@@ -209,7 +161,7 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
if (hasValidationErrors(form, infrastractureMonitorValidation, setErrors)) {
|
if (hasValidationErrors(form, infrastractureMonitorValidation, setErrors)) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
const checkEndpointAction = dispatch(
|
const checkEndpointAction = await dispatch(
|
||||||
checkInfrastructureEndpointResolution({ authToken, monitorURL: form.url })
|
checkInfrastructureEndpointResolution({ authToken, monitorURL: form.url })
|
||||||
);
|
);
|
||||||
if (checkEndpointAction.meta.requestStatus === "rejected") {
|
if (checkEndpointAction.meta.requestStatus === "rejected") {
|
||||||
@@ -219,7 +171,7 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
setErrors({ url: "The entered URL is not reachable." });
|
setErrors({ url: "The entered URL is not reachable." });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const action = dispatch(
|
const action = await dispatch(
|
||||||
createInfrastructureMonitor({ authToken, monitor: generatePayload(form) })
|
createInfrastructureMonitor({ authToken, monitor: generatePayload(form) })
|
||||||
);
|
);
|
||||||
if (action.meta.requestStatus === "fulfilled") {
|
if (action.meta.requestStatus === "fulfilled") {
|
||||||
@@ -370,18 +322,20 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
</Box>
|
</Box>
|
||||||
<Stack gap={theme.spacing(6)}>
|
<Stack gap={theme.spacing(6)}>
|
||||||
{HARDWARE_MONITOR_TYPES.map((type, idx) => (
|
{HARDWARE_MONITOR_TYPES.map((type, idx) => (
|
||||||
<CustomAlertStack
|
<CustomThreshold
|
||||||
key={idx}
|
key={idx}
|
||||||
checkboxId={type}
|
checkboxId={type}
|
||||||
checkboxLabel={
|
checkboxLabel={
|
||||||
type !== "cpu" ? capitalizeFirstLetter(type) : type.toUpperCase()
|
type !== "cpu" ? capitalizeFirstLetter(type) : type.toUpperCase()
|
||||||
}
|
}
|
||||||
checkboxValue={""}
|
onCheckboxChange={handleCustomAlertCheckChange}
|
||||||
fieldId={THRESHOLD_FIELD_PREFIX + type}
|
fieldId={THRESHOLD_FIELD_PREFIX + type}
|
||||||
fieldValue={infrastructureMonitor[THRESHOLD_FIELD_PREFIX + type] ?? ""}
|
fieldValue={infrastructureMonitor[THRESHOLD_FIELD_PREFIX + type] ?? ""}
|
||||||
onChange={handleChange}
|
onFieldChange={handleChange}
|
||||||
onBlur={handleBlur}
|
onFieldBlur={handleBlur}
|
||||||
alertUnit="%"
|
alertUnit="%"
|
||||||
|
infrastructureMonitor={infrastructureMonitor}
|
||||||
|
errors={errors}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
{alertErrKeyLen > 0 && (
|
{alertErrKeyLen > 0 && (
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const buildErrors = (prev, id, error) => {
|
const buildErrors = (prev, id, error) => {
|
||||||
const updatedErrors = { ...prev };
|
const updatedErrors = { ...prev };
|
||||||
if (error) {
|
if (error) {
|
||||||
updatedErrors[id] = error.details[0].message?? "Validation error";
|
updatedErrors[id] = error.details[0].message ?? "Validation error";
|
||||||
} else {
|
} else {
|
||||||
delete updatedErrors[id];
|
delete updatedErrors[id];
|
||||||
}
|
}
|
||||||
@@ -27,22 +27,24 @@ 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.cpu && form.usage_cpu)) {
|
||||||
delete newErrors["usage_cpu"];
|
delete newErrors["usage_cpu"];
|
||||||
}
|
}
|
||||||
if (!form.memory || (form.memory && form.usage_memory)) {
|
if (!form.memory || (form.memory && form.usage_memory)) {
|
||||||
delete newErrors["usage_memory"];
|
delete newErrors["usage_memory"];
|
||||||
}
|
}
|
||||||
if (!form.disk || (form.disk && form.usage_disk)) {
|
if (!form.disk || (form.disk && form.usage_disk)) {
|
||||||
delete newErrors["usage_disk"];
|
delete newErrors["usage_disk"];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (Object.keys(newErrors).length > 0) {
|
if (Object.keys(newErrors).length > 0) {
|
||||||
setErrors(newErrors);
|
setErrors(newErrors);
|
||||||
return true;
|
return true;
|
||||||
} else return false;
|
} else {
|
||||||
|
setErrors({});
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user