- refine code

This commit is contained in:
Shemy Gan committed 2024-11-12 09:12:12 -05:00
1 parent 11d71060b2
commit a9771d941d
2 files changed
+33 -77

No files matched your search

@@ -17,6 +17,7 @@ import Select from "../../../Components/Inputs/Select";
import Checkbox from "../../../Components/Inputs/Checkbox"; 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";
const CreateInfrastructureMonitor = () => { const CreateInfrastructureMonitor = () => {
const [infrastructureMonitor, setInfrastructureMonitor] = useState({ const [infrastructureMonitor, setInfrastructureMonitor] = useState({
@@ -50,8 +51,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
console.log("alertErrKeyLen")
console.log(alertErrKeyLen)
const CustomAlertStack = ({ const CustomAlertStack = ({
checkboxId, checkboxId,
@@ -89,7 +88,7 @@ const CreateInfrastructureMonitor = () => {
value={infrastructureMonitor[fieldId]} value={infrastructureMonitor[fieldId]}
onBlur={onBlur} onBlur={onBlur}
onChange={onChange} onChange={onChange}
error={errors[`${fieldId}`]} error={errors[fieldId]}
disabled={!infrastructureMonitor[checkboxId]} disabled={!infrastructureMonitor[checkboxId]}
></Field> ></Field>
<Typography <Typography
@@ -111,14 +110,14 @@ const CreateInfrastructureMonitor = () => {
return { return {
...prev, ...prev,
...newState, ...newState,
[`${THRESHOLD_FIELD_PREFIX}${id}`]: newState[id] [THRESHOLD_FIELD_PREFIX+ id]: newState[id]
? prev[`${THRESHOLD_FIELD_PREFIX}${id}`] ? prev[THRESHOLD_FIELD_PREFIX+ id]
: "", : "",
}; };
}); });
// Remove the error if unchecked // Remove the error if unchecked
setErrors((prev) => { setErrors((prev) => {
return buildErrors(prev, [`${THRESHOLD_FIELD_PREFIX}${id}`]); return buildErrors(prev, [THRESHOLD_FIELD_PREFIX+ id]);
}); });
}; };
@@ -178,11 +177,11 @@ const CreateInfrastructureMonitor = () => {
const generatePayload = (form) => { const generatePayload = (form) => {
let thresholds = {}; let thresholds = {};
Object.keys(form) Object.keys(form)
.filter((k) => k.startsWith(`${THRESHOLD_FIELD_PREFIX}`)) .filter((k) => k.startsWith(THRESHOLD_FIELD_PREFIX))
.map((k) => { .map((k) => {
if (form[k]) thresholds[k] = form[k]; if (form[k]) thresholds[k] = form[k];
delete form[k]; delete form[k];
delete form[k.substring(`${THRESHOLD_FIELD_PREFIX}`.length)]; delete form[k.substring(THRESHOLD_FIELD_PREFIX.length)];
}); });
form = { form = {
@@ -370,72 +369,21 @@ const CreateInfrastructureMonitor = () => {
</Typography> </Typography>
</Box> </Box>
<Stack gap={theme.spacing(6)}> <Stack gap={theme.spacing(6)}>
<CustomAlertStack {HARDWARE_MONITOR_TYPES.map((type, idx) => (
checkboxId={`${HARDWARE_MONITOR_TYPES[0]}`} <CustomAlertStack
checkboxLabel="CPU" key={idx}
checkboxValue={""} checkboxId={type}
fieldId={`${THRESHOLD_FIELD_PREFIX}` + `${HARDWARE_MONITOR_TYPES[0]}`} checkboxLabel={
fieldValue={ type !== "cpu" ? capitalizeFirstLetter(type) : type.toUpperCase()
infrastructureMonitor[ }
`${THRESHOLD_FIELD_PREFIX}` + `${HARDWARE_MONITOR_TYPES[0]}` checkboxValue={""}
] ?? "" fieldId={THRESHOLD_FIELD_PREFIX + type}
} fieldValue={infrastructureMonitor[THRESHOLD_FIELD_PREFIX + type] ?? ""}
onChange={handleChange} onChange={handleChange}
onBlur={handleBlur} onBlur={handleBlur}
alertUnit="%" alertUnit="%"
/> />
<CustomAlertStack ))}
checkboxId={`${HARDWARE_MONITOR_TYPES[1]}`}
checkboxLabel="Memory"
checkboxValue={""}
fieldId={`${THRESHOLD_FIELD_PREFIX}` + `${HARDWARE_MONITOR_TYPES[1]}`}
fieldValue={
infrastructureMonitor[
`${THRESHOLD_FIELD_PREFIX}` + `${HARDWARE_MONITOR_TYPES[1]}`
] ?? ""
}
onChange={handleChange}
onBlur={handleBlur}
alertUnit="%"
/>
<CustomAlertStack
checkboxId={`${HARDWARE_MONITOR_TYPES[2]}`}
checkboxLabel="Disk"
checkboxValue={""}
fieldId={`${THRESHOLD_FIELD_PREFIX}` + `${HARDWARE_MONITOR_TYPES[2]}`}
fieldValue={
infrastructureMonitor[
`${THRESHOLD_FIELD_PREFIX}` + `${HARDWARE_MONITOR_TYPES[2]}`
] ?? ""
}
onChange={handleChange}
onBlur={handleBlur}
alertUnit="%"
/>
{/* <CustomAlertStack
checkboxId="customize-temperature"
checkboxLabel="Temperature"
checkboxValue={true}
onChange={handleChange}
onBlur={handleBlur}
alertUnit="°C"
/>
<CustomAlertStack
checkboxId="customize-systemload"
checkboxLabel="System load"
checkboxValue={true}
onChange={handleChange}
onBlur={handleBlur}
alertUnit="%"
/>
<CustomAlertStack
checkboxId="customize-swap"
checkboxLabel="Swap used"
checkboxValue={""}
onChange={handleChange}
onBlur={handleBlur}
alertUnit="%"
/> */}
{alertErrKeyLen > 0 && ( {alertErrKeyLen > 0 && (
<Typography <Typography
component="span" component="span"
@@ -446,9 +394,14 @@ const CreateInfrastructureMonitor = () => {
opacity: 0.8, opacity: 0.8,
}} }}
> >
{errors[`${THRESHOLD_FIELD_PREFIX}cpu`] ?? {
errors[`${THRESHOLD_FIELD_PREFIX}memory`] ?? errors[
errors[`${THRESHOLD_FIELD_PREFIX}disk`]} THRESHOLD_FIELD_PREFIX +
HARDWARE_MONITOR_TYPES.filter(
(type) => errors[THRESHOLD_FIELD_PREFIX + type]
)[0]
]
}
</Typography> </Typography>
)} )}
</Stack> </Stack>
+3
View File
@@ -0,0 +1,3 @@
export const capitalizeFirstLetter = (str) =>{
return str.charAt(0).toUpperCase() + str.slice(1);
}