Added some basic validation
This commit is contained in:
1 parent
0ef7e014ed
commit
f3e222ac3a
2 files changed
+109
-33
No files matched your search
@@ -5,34 +5,42 @@ import React, { useState } from "react";
|
|||||||
import RadioButton from "../../Components/RadioButton";
|
import RadioButton from "../../Components/RadioButton";
|
||||||
import CustomizableCheckBox from "../../Components/Checkbox/CustomizableCheckbox";
|
import CustomizableCheckBox from "../../Components/Checkbox/CustomizableCheckbox";
|
||||||
import Button from "../../Components/Button";
|
import Button from "../../Components/Button";
|
||||||
import { MenuItem, Select } from "@mui/material";
|
import { MenuItem, Select, Typography } from "@mui/material";
|
||||||
|
import { useSelector } from "react-redux";
|
||||||
|
import { createMonitorValidation } from "../../Validation/validation";
|
||||||
|
|
||||||
const CreateNewMonitor = () => {
|
const CreateNewMonitor = () => {
|
||||||
|
const { user } = useSelector((state) => state.auth);
|
||||||
|
const [errors, setErrors] = useState({});
|
||||||
|
|
||||||
//General Settings Form
|
//General Settings Form
|
||||||
const [generalSettings, setGeneralSettings] = useState({ url: "", name: "" });
|
const [generalSettings, setGeneralSettings] = useState({ url: "", name: "" });
|
||||||
//Checks Form
|
//Checks Form
|
||||||
const [checks, setChecks] = useState({ type: "", port: "" });
|
const [checks, setChecks] = useState({
|
||||||
//Incidents Form
|
type: "",
|
||||||
const [notifications, setNotifications] = useState({
|
// port: ""
|
||||||
viaSms: false,
|
|
||||||
viaEmail: false,
|
|
||||||
viaOther: false,
|
|
||||||
email: "",
|
|
||||||
});
|
});
|
||||||
|
//Incidents Form
|
||||||
|
// const [notifications, setNotifications] = useState({
|
||||||
|
// viaSms: false,
|
||||||
|
// viaEmail: false,
|
||||||
|
// viaOther: false,
|
||||||
|
// email: "",
|
||||||
|
// });
|
||||||
//Advanced Settings Form
|
//Advanced Settings Form
|
||||||
const [advancedSettings, setAdvancedSettings] = useState({
|
const [advancedSettings, setAdvancedSettings] = useState({
|
||||||
frequency: "",
|
frequency: "",
|
||||||
retries: "",
|
// retries: "",
|
||||||
codes: "",
|
// codes: "",
|
||||||
redirects: "",
|
// redirects: "",
|
||||||
});
|
});
|
||||||
//Proxy Settings Form
|
//Proxy Settings Form
|
||||||
const [proxy, setProxy] = useState({
|
// const [proxy, setProxy] = useState({
|
||||||
enabled: false,
|
// enabled: false,
|
||||||
protocol: "",
|
// protocol: "",
|
||||||
address: "",
|
// address: "",
|
||||||
proxy_port: "",
|
// proxy_port: "",
|
||||||
});
|
// });
|
||||||
|
|
||||||
const handleChange = (event, id, setState, checkbox) => {
|
const handleChange = (event, id, setState, checkbox) => {
|
||||||
const { value } = event.target;
|
const { value } = event.target;
|
||||||
@@ -40,19 +48,59 @@ const CreateNewMonitor = () => {
|
|||||||
...prev,
|
...prev,
|
||||||
[id]: checkbox ? true : value,
|
[id]: checkbox ? true : value,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
const validation = createMonitorValidation.validate(
|
||||||
|
{ [id]: value },
|
||||||
|
{ abortEarly: false }
|
||||||
|
);
|
||||||
|
|
||||||
|
setErrors((prev) => {
|
||||||
|
const updatedErrors = { ...prev };
|
||||||
|
|
||||||
|
if (validation.error) {
|
||||||
|
updatedErrors[id] = validation.error.details[0].message;
|
||||||
|
} else {
|
||||||
|
delete updatedErrors[id];
|
||||||
|
}
|
||||||
|
return updatedErrors;
|
||||||
|
});
|
||||||
};
|
};
|
||||||
const handleCheck = (id, setState) => {
|
// const handleCheck = (id, setState) => {
|
||||||
setState((prev) => ({
|
// setState((prev) => ({
|
||||||
...prev,
|
// ...prev,
|
||||||
[id]: !prev[id],
|
// [id]: !prev[id],
|
||||||
}));
|
// }));
|
||||||
|
// };
|
||||||
|
|
||||||
|
const handleCreateNewMonitor = (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
//obj to submit
|
||||||
|
const monitor = {
|
||||||
|
...generalSettings,
|
||||||
|
...advancedSettings,
|
||||||
|
userId: user._id,
|
||||||
|
...checks,
|
||||||
|
};
|
||||||
|
|
||||||
|
const { error } = createMonitorValidation.validate(monitor, {
|
||||||
|
abortEarly: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
const newErrors = {};
|
||||||
|
error.details.forEach((err) => {
|
||||||
|
newErrors[err.path[0]] = err.message;
|
||||||
|
});
|
||||||
|
setErrors(newErrors);
|
||||||
|
} else {
|
||||||
|
//TODO - submit logic
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCreateNewMonitor = () => {
|
console.log(errors);
|
||||||
|
|
||||||
}
|
//select values
|
||||||
|
// const ports = ["Port 1", "Port 2", "Port 3"];
|
||||||
const ports = ["Port 1", "Port 2", "Port 3"];
|
|
||||||
const frequencies = [1, 2, 3, 4, 5];
|
const frequencies = [1, 2, 3, 4, 5];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -81,6 +129,13 @@ const CreateNewMonitor = () => {
|
|||||||
handleChange(event, "url", setGeneralSettings)
|
handleChange(event, "url", setGeneralSettings)
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
{errors["url"] ? (
|
||||||
|
<Typography variant="h5" component="p" className="input-error">
|
||||||
|
{errors["url"]}
|
||||||
|
</Typography>
|
||||||
|
) : (
|
||||||
|
""
|
||||||
|
)}
|
||||||
<div className="monitors-gaps-medium"></div>
|
<div className="monitors-gaps-medium"></div>
|
||||||
<FlexibileTextField
|
<FlexibileTextField
|
||||||
id="monitor-name"
|
id="monitor-name"
|
||||||
@@ -92,6 +147,13 @@ const CreateNewMonitor = () => {
|
|||||||
handleChange(event, "name", setGeneralSettings)
|
handleChange(event, "name", setGeneralSettings)
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
{errors["name"] ? (
|
||||||
|
<Typography variant="h5" component="p" className="input-error">
|
||||||
|
{errors["name"]}
|
||||||
|
</Typography>
|
||||||
|
) : (
|
||||||
|
""
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
@@ -113,8 +175,8 @@ const CreateNewMonitor = () => {
|
|||||||
title="HTTP/website monitoring"
|
title="HTTP/website monitoring"
|
||||||
desc="Use HTTP(s) to monitor your website or API endpoint."
|
desc="Use HTTP(s) to monitor your website or API endpoint."
|
||||||
value="http"
|
value="http"
|
||||||
checked={checks.monitor === "http"}
|
checked={checks.type === "http"}
|
||||||
onChange={(event) => handleChange(event, "monitor", setChecks)}
|
onChange={(event) => handleChange(event, "type", setChecks)}
|
||||||
/>
|
/>
|
||||||
<div className="monitors-gaps-medium"></div>
|
<div className="monitors-gaps-medium"></div>
|
||||||
<RadioButton
|
<RadioButton
|
||||||
@@ -122,8 +184,8 @@ const CreateNewMonitor = () => {
|
|||||||
title="Ping monitoring"
|
title="Ping monitoring"
|
||||||
desc="Check whether your server is available or not."
|
desc="Check whether your server is available or not."
|
||||||
value="ping"
|
value="ping"
|
||||||
checked={checks.monitor === "ping"}
|
checked={checks.type === "ping"}
|
||||||
onChange={(event) => handleChange(event, "monitor", setChecks)}
|
onChange={(event) => handleChange(event, "type", setChecks)}
|
||||||
/>
|
/>
|
||||||
<div className="monitors-gaps-medium"></div>
|
<div className="monitors-gaps-medium"></div>
|
||||||
{/* TODO */}
|
{/* TODO */}
|
||||||
@@ -132,8 +194,8 @@ const CreateNewMonitor = () => {
|
|||||||
title="Port monitoring"
|
title="Port monitoring"
|
||||||
desc="Monitor a specific service on your server."
|
desc="Monitor a specific service on your server."
|
||||||
value="port"
|
value="port"
|
||||||
checked={checks.monitor === "port"}
|
checked={checks.type === "port"}
|
||||||
onChange={(event) => handleChange(event, "monitor", setChecks)}
|
onChange={(event) => handleChange(event, "type", setChecks)}
|
||||||
/>
|
/>
|
||||||
<div className="monitors-gaps-small-plus"></div>
|
<div className="monitors-gaps-small-plus"></div>
|
||||||
<div className="monitors-dropdown-holder">
|
<div className="monitors-dropdown-holder">
|
||||||
@@ -153,6 +215,13 @@ const CreateNewMonitor = () => {
|
|||||||
))}
|
))}
|
||||||
</Select>
|
</Select>
|
||||||
</div> */}
|
</div> */}
|
||||||
|
{errors["type"] ? (
|
||||||
|
<Typography variant="h5" component="p" className="input-error">
|
||||||
|
{errors["type"]}
|
||||||
|
</Typography>
|
||||||
|
) : (
|
||||||
|
""
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
@@ -320,7 +389,7 @@ const CreateNewMonitor = () => {
|
|||||||
level="primary"
|
level="primary"
|
||||||
label="Create new monitor"
|
label="Create new monitor"
|
||||||
sx={{ width: "210px", fontSize: "var(--env-var-font-size-medium)" }}
|
sx={{ width: "210px", fontSize: "var(--env-var-font-size-medium)" }}
|
||||||
onClick={() => handleCreateNewMonitor()}
|
onClick={handleCreateNewMonitor}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@@ -132,7 +132,14 @@ const editPasswordValidation = joi.object({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const createMonitorValidation = joi.object({
|
||||||
|
url: joi.string().trim().messages({ "string.empty": "*Url is required." }),
|
||||||
|
name: joi.string().trim().messages({ "string.empty": "*Name is required." }),
|
||||||
|
type: joi.string().trim().messages({ "string.empty": "*Type is required." }),
|
||||||
|
});
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
createMonitorValidation,
|
||||||
registerValidation,
|
registerValidation,
|
||||||
loginValidation,
|
loginValidation,
|
||||||
recoveryValidation,
|
recoveryValidation,
|
||||||
|
|||||||
Reference in new issue
Block a user