Use name instead of extra param

This commit is contained in:
Alex Holliday committed 2024-12-10 11:30:38 +08:00
1 parent f64e4d04ef
commit 06cb22e4d8
1 file changed
+25 -23
@@ -1,16 +1,12 @@
// React, Redux, Router // React, Redux, Router
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import { useNavigate, useParams } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { useEffect } from "react";
import { useState } from "react"; import { useState } from "react";
import { useSelector, useDispatch } from "react-redux"; import { useSelector, useDispatch } from "react-redux";
// Utility and Network // Utility and Network
import { infrastructureMonitorValidation } from "../../../Validation/validation"; import { infrastructureMonitorValidation } from "../../../Validation/validation";
import { import { createInfrastructureMonitor } from "../../../Features/InfrastructureMonitors/infrastructureMonitorsSlice";
createInfrastructureMonitor,
checkInfrastructureEndpointResolution,
} from "../../../Features/InfrastructureMonitors/infrastructureMonitorsSlice";
import { capitalizeFirstLetter } from "../../../Utils/stringUtils"; import { capitalizeFirstLetter } from "../../../Utils/stringUtils";
// MUI // MUI
@@ -165,34 +161,34 @@ const CreateInfrastructureMonitor = () => {
} }
}; };
const handleChange = (event, formName) => { const handleChange = (event) => {
const { value } = event.target; const { value, name } = event.target;
setInfrastructureMonitor({ setInfrastructureMonitor({
...infrastructureMonitor, ...infrastructureMonitor,
[formName]: value, [name]: value,
}); });
const { error } = infrastructureMonitorValidation.validate( const { error } = infrastructureMonitorValidation.validate(
{ [formName]: value }, { [name]: value },
{ abortEarly: false } { abortEarly: false }
); );
setErrors((prev) => ({ setErrors((prev) => ({
...prev, ...prev,
...(error ? { [formName]: error.details[0].message } : { [formName]: undefined }), ...(error ? { [name]: error.details[0].message } : { [name]: undefined }),
})); }));
}; };
const handleCheckboxChange = (event, formName) => { const handleCheckboxChange = (event) => {
console.log(event.target.checked, formName); const { name } = event.target;
const { checked } = event.target; const { checked } = event.target;
setInfrastructureMonitor({ setInfrastructureMonitor({
...infrastructureMonitor, ...infrastructureMonitor,
[formName]: checked, [name]: checked,
}); });
}; };
const handleBlur = (event) => { const handleBlur = (event) => {
console.log("hanldeBlur", event); console.log("handleBlur", event);
}; };
const handleNotifications = (event, type) => { const handleNotifications = (event, type) => {
@@ -277,12 +273,13 @@ const CreateInfrastructureMonitor = () => {
<TextInput <TextInput
type="url" type="url"
id="url" id="url"
name="url"
startAdornment={<HttpAdornment https={https} />} startAdornment={<HttpAdornment https={https} />}
placeholder={"localhost:59232/api/v1/metrics"} placeholder={"localhost:59232/api/v1/metrics"}
label="Server URL" label="Server URL"
https={https} https={https}
value={infrastructureMonitor.url} value={infrastructureMonitor.url}
onChange={(event) => handleChange(event, "url")} onChange={handleChange}
error={errors["url"] ? true : false} error={errors["url"] ? true : false}
helperText={errors["url"]} helperText={errors["url"]}
/> />
@@ -308,19 +305,21 @@ const CreateInfrastructureMonitor = () => {
<TextInput <TextInput
type="text" type="text"
id="name" id="name"
name="name"
label="Display name" label="Display name"
placeholder="Google" placeholder="Google"
isOptional={true} isOptional={true}
value={infrastructureMonitor.name} value={infrastructureMonitor.name}
onChange={(event) => handleChange(event, "name")} onChange={handleChange}
error={errors["name"]} error={errors["name"]}
/> />
<TextInput <TextInput
type="text" type="text"
id="secret" id="secret"
name="secret"
label="Authorization secret" label="Authorization secret"
value={infrastructureMonitor.secret} value={infrastructureMonitor.secret}
onChange={(event) => handleChange(event, "secret")} onChange={handleChange}
error={errors["secret"] ? true : false} error={errors["secret"] ? true : false}
helperText={errors["secret"]} helperText={errors["secret"]}
/> />
@@ -363,17 +362,19 @@ const CreateInfrastructureMonitor = () => {
infrastructureMonitor={infrastructureMonitor} infrastructureMonitor={infrastructureMonitor}
errors={errors} errors={errors}
checkboxId={metric} checkboxId={metric}
checkboxName={metric}
checkboxLabel={ checkboxLabel={
metric !== "cpu" metric !== "cpu"
? capitalizeFirstLetter(metric) ? capitalizeFirstLetter(metric)
: metric.toUpperCase() : metric.toUpperCase()
} }
onCheckboxChange={(event) => handleCheckboxChange(event, metric)} onCheckboxChange={handleCheckboxChange}
isChecked={infrastructureMonitor[metric]} isChecked={infrastructureMonitor[metric]}
fieldId={METRIC_PREFIX + metric} fieldId={METRIC_PREFIX + metric}
fieldName={METRIC_PREFIX + metric}
fieldValue={infrastructureMonitor[METRIC_PREFIX + metric]} fieldValue={infrastructureMonitor[METRIC_PREFIX + metric]}
onFieldChange={(event) => handleChange(event, METRIC_PREFIX + metric)} onFieldChange={handleChange}
onFieldBlur={(event) => handleBlur(event, METRIC_PREFIX + metric)} onFieldBlur={handleBlur}
alertUnit={metric == "temperature" ? "°C" : "%"} alertUnit={metric == "temperature" ? "°C" : "%"}
/> />
); );
@@ -401,10 +402,11 @@ const CreateInfrastructureMonitor = () => {
<Stack gap={theme.spacing(12)}> <Stack gap={theme.spacing(12)}>
<Select <Select
id="interval" id="interval"
name="interval"
label="Check frequency" label="Check frequency"
value={infrastructureMonitor.interval || 15} value={infrastructureMonitor.interval || 15}
onChange={(e) => handleChange(e, "interval")} onChange={handleChange}
onBlur={(e) => handleBlur(e, "interval")} onBlur={handleBlur}
items={SELECT_VALUES} items={SELECT_VALUES}
/> />
</Stack> </Stack>