Added validation
This commit is contained in:
1 parent
4a14d1c8d9
commit
1aef3300f0
4 files changed
+69
-20
No files matched your search
@@ -43,6 +43,14 @@
|
|||||||
.configure-monitor .config-box .MuiStack-root {
|
.configure-monitor .config-box .MuiStack-root {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
}
|
}
|
||||||
|
.configure-monitor .MuiStack-root:has(span.MuiTypography-root.input-error) {
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
.configure-monitor span.MuiTypography-root.input-error {
|
||||||
|
position: absolute;
|
||||||
|
top: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
.MuiInputBase-root:has(#monitor-interval) {
|
.MuiInputBase-root:has(#monitor-interval) {
|
||||||
height: 34px;
|
height: 34px;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import PauseCircleOutlineIcon from "@mui/icons-material/PauseCircleOutline";
|
|||||||
import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown";
|
import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown";
|
||||||
|
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
|
import { monitorValidation } from "../../../Validation/validation";
|
||||||
|
|
||||||
const formatDurationRounded = (ms) => {
|
const formatDurationRounded = (ms) => {
|
||||||
const seconds = Math.floor(ms / 1000);
|
const seconds = Math.floor(ms / 1000);
|
||||||
@@ -66,6 +67,14 @@ const Configure = () => {
|
|||||||
const { authToken } = useSelector((state) => state.auth);
|
const { authToken } = useSelector((state) => state.auth);
|
||||||
const { monitorId } = useParams();
|
const { monitorId } = useParams();
|
||||||
|
|
||||||
|
const idMap = {
|
||||||
|
"monitor-url": "url",
|
||||||
|
"monitor-name": "name",
|
||||||
|
"monitor-type": "type",
|
||||||
|
"monitor-interval": "interval",
|
||||||
|
};
|
||||||
|
|
||||||
|
const [config, setConfig] = useState();
|
||||||
const [monitor, setMonitor] = useState();
|
const [monitor, setMonitor] = useState();
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -76,17 +85,45 @@ const Configure = () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
let data = res.data.data;
|
let data = res.data.data;
|
||||||
|
setConfig(data);
|
||||||
setMonitor({
|
setMonitor({
|
||||||
name: data.name,
|
name: data.name,
|
||||||
url: data.url.replace(/^https?:\/\//, ""),
|
url: data.url.replace(/^https?:\/\//, ""),
|
||||||
type: data.type,
|
type: data.type,
|
||||||
interval: data.interval / MS_PER_MINUTE,
|
interval: data.interval / MS_PER_MINUTE,
|
||||||
status: data.status,
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
fetchMonitor();
|
fetchMonitor();
|
||||||
}, [monitorId, authToken]);
|
}, [monitorId, authToken]);
|
||||||
|
|
||||||
|
const handleChange = (event) => {
|
||||||
|
const { value, id } = event.target;
|
||||||
|
const name = idMap[id];
|
||||||
|
setMonitor((prev) => ({
|
||||||
|
...prev,
|
||||||
|
[name]: value,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const validation = monitorValidation.validate(
|
||||||
|
{ [name]: value },
|
||||||
|
{ abortEarly: false }
|
||||||
|
);
|
||||||
|
|
||||||
|
setErrors((prev) => {
|
||||||
|
const updatedErrors = { ...prev };
|
||||||
|
|
||||||
|
if (validation.error)
|
||||||
|
updatedErrors[name] = validation.error.details[0].message;
|
||||||
|
else delete updatedErrors[name];
|
||||||
|
return updatedErrors;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
// TODO
|
||||||
|
};
|
||||||
|
|
||||||
const frequencies = [1, 2, 3, 4, 5];
|
const frequencies = [1, 2, 3, 4, 5];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -114,25 +151,25 @@ const Configure = () => {
|
|||||||
<form className="configure-monitor-form" noValidate spellCheck="false">
|
<form className="configure-monitor-form" noValidate spellCheck="false">
|
||||||
<Stack gap={theme.gap.xl}>
|
<Stack gap={theme.gap.xl}>
|
||||||
<Stack direction="row" gap={theme.gap.small} mt={theme.gap.small}>
|
<Stack direction="row" gap={theme.gap.small} mt={theme.gap.small}>
|
||||||
{monitor?.status ? <GreenCheck /> : <RedCheck />}
|
{config?.status ? <GreenCheck /> : <RedCheck />}
|
||||||
<Box>
|
<Box>
|
||||||
<Typography component="h1" sx={{ lineHeight: 1 }}>
|
<Typography component="h1" sx={{ lineHeight: 1 }}>
|
||||||
{monitor?.url.replace(/^https?:\/\//, "") || "..."}
|
{config?.url.replace(/^https?:\/\//, "") || "..."}
|
||||||
</Typography>
|
</Typography>
|
||||||
<Typography mt={theme.gap.small}>
|
<Typography mt={theme.gap.small}>
|
||||||
<Typography
|
<Typography
|
||||||
component="span"
|
component="span"
|
||||||
sx={{
|
sx={{
|
||||||
color: monitor?.status
|
color: config?.status
|
||||||
? "var(--env-var-color-17)"
|
? "var(--env-var-color-17)"
|
||||||
: "var(--env-var-color-24)",
|
: "var(--env-var-color-24)",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Your site is {monitor?.status ? "up" : "down"}.
|
Your site is {config?.status ? "up" : "down"}.
|
||||||
</Typography>{" "}
|
</Typography>{" "}
|
||||||
Checking every {formatDurationRounded(monitor?.interval)}. Last
|
Checking every {formatDurationRounded(config?.interval)}. Last
|
||||||
time checked{" "}
|
time checked{" "}
|
||||||
{formatDurationRounded(getLastChecked(monitor?.checks))} ago.
|
{formatDurationRounded(getLastChecked(config?.checks))} ago.
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
<Stack
|
<Stack
|
||||||
@@ -185,7 +222,9 @@ const Configure = () => {
|
|||||||
id="monitor-url"
|
id="monitor-url"
|
||||||
label="URL to monitor"
|
label="URL to monitor"
|
||||||
placeholder="google.com"
|
placeholder="google.com"
|
||||||
value={monitor?.url}
|
value={monitor?.url || ""}
|
||||||
|
onChange={handleChange}
|
||||||
|
error={errors["url"]}
|
||||||
/>
|
/>
|
||||||
<Field
|
<Field
|
||||||
type="text"
|
type="text"
|
||||||
@@ -193,7 +232,9 @@ const Configure = () => {
|
|||||||
label="Friendly name"
|
label="Friendly name"
|
||||||
isOptional={true}
|
isOptional={true}
|
||||||
placeholder="Google"
|
placeholder="Google"
|
||||||
value={monitor?.name}
|
value={monitor?.name || ""}
|
||||||
|
onChange={handleChange}
|
||||||
|
error={errors["name"]}
|
||||||
/>
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import RadioButton from "../../../Components/RadioButton";
|
|||||||
import Button from "../../../Components/Button";
|
import Button from "../../../Components/Button";
|
||||||
import { Box, MenuItem, Select, Stack, Typography } from "@mui/material";
|
import { Box, MenuItem, Select, Stack, Typography } from "@mui/material";
|
||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector, useDispatch } from "react-redux";
|
||||||
import { createMonitorValidation } from "../../../Validation/validation";
|
import { monitorValidation } from "../../../Validation/validation";
|
||||||
import { createMonitor } from "../../../Features/Monitors/monitorsSlice";
|
import { createMonitor } from "../../../Features/Monitors/monitorsSlice";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
@@ -37,7 +37,7 @@ const CreateMonitor = () => {
|
|||||||
// });
|
// });
|
||||||
//Advanced Settings Form
|
//Advanced Settings Form
|
||||||
const [advancedSettings, setAdvancedSettings] = useState({
|
const [advancedSettings, setAdvancedSettings] = useState({
|
||||||
frequency: 1,
|
interval: 1,
|
||||||
// retries: "",
|
// retries: "",
|
||||||
// codes: "",
|
// codes: "",
|
||||||
// redirects: "",
|
// redirects: "",
|
||||||
@@ -57,7 +57,7 @@ const CreateMonitor = () => {
|
|||||||
[id]: checkbox ? true : value,
|
[id]: checkbox ? true : value,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const validation = createMonitorValidation.validate(
|
const validation = monitorValidation.validate(
|
||||||
{ [id]: value },
|
{ [id]: value },
|
||||||
{ abortEarly: false }
|
{ abortEarly: false }
|
||||||
);
|
);
|
||||||
@@ -92,7 +92,7 @@ const CreateMonitor = () => {
|
|||||||
...checks,
|
...checks,
|
||||||
};
|
};
|
||||||
|
|
||||||
const { error } = createMonitorValidation.validate(monitor, {
|
const { error } = monitorValidation.validate(monitor, {
|
||||||
abortEarly: false,
|
abortEarly: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -107,8 +107,8 @@ const CreateMonitor = () => {
|
|||||||
...monitor,
|
...monitor,
|
||||||
description: monitor.name,
|
description: monitor.name,
|
||||||
userId: user._id,
|
userId: user._id,
|
||||||
// ...advancedSettings, // TODO frequency should be interval, then we can use spread
|
// ...advancedSettings
|
||||||
interval: advancedSettings.frequency * MS_PER_MINUTE,
|
interval: advancedSettings.interval * MS_PER_MINUTE,
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
const action = await dispatch(createMonitor({ authToken, monitor }));
|
const action = await dispatch(createMonitor({ authToken, monitor }));
|
||||||
@@ -333,10 +333,10 @@ const CreateMonitor = () => {
|
|||||||
</Typography>
|
</Typography>
|
||||||
<Select
|
<Select
|
||||||
id="monitor-frequencies"
|
id="monitor-frequencies"
|
||||||
value={advancedSettings.frequency || 1}
|
value={advancedSettings.interval || 1}
|
||||||
inputProps={{ id: "monitor-frequencies-select" }}
|
inputProps={{ id: "monitor-frequencies-select" }}
|
||||||
onChange={(event) =>
|
onChange={(event) =>
|
||||||
handleChange(event, "frequency", setAdvancedSettings)
|
handleChange(event, "interval", setAdvancedSettings)
|
||||||
}
|
}
|
||||||
MenuProps={{
|
MenuProps={{
|
||||||
PaperProps: {
|
PaperProps: {
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ const credentials = joi.object({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const createMonitorValidation = joi.object({
|
const monitorValidation = joi.object({
|
||||||
url: joi
|
url: joi
|
||||||
.string()
|
.string()
|
||||||
.trim()
|
.trim()
|
||||||
@@ -85,7 +85,7 @@ const createMonitorValidation = joi.object({
|
|||||||
.string()
|
.string()
|
||||||
.trim()
|
.trim()
|
||||||
.messages({ "string.empty": "*This field is required." }),
|
.messages({ "string.empty": "*This field is required." }),
|
||||||
frequency: joi.number().messages({
|
interval: joi.number().messages({
|
||||||
"number.base": "*Frequency must be a number.",
|
"number.base": "*Frequency must be a number.",
|
||||||
"any.required": "*Frequency is required.",
|
"any.required": "*Frequency is required.",
|
||||||
}),
|
}),
|
||||||
@@ -106,4 +106,4 @@ const imageValidation = joi.object({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
export { credentials, imageValidation, createMonitorValidation };
|
export { credentials, imageValidation, monitorValidation };
|
||||||
Reference in new issue
Block a user