Change validation error toast to a user friendly message
This commit is contained in:
1 parent
d8e2b06eca
commit
2b6c2bad6a
1 file changed
+356
-352
@@ -17,370 +17,374 @@ import { getUptimeMonitorById } from "../../../Features/UptimeMonitors/uptimeMon
|
|||||||
import "./index.css";
|
import "./index.css";
|
||||||
|
|
||||||
const CreateMonitor = () => {
|
const CreateMonitor = () => {
|
||||||
const MS_PER_MINUTE = 60000;
|
const MS_PER_MINUTE = 60000;
|
||||||
const { user, authToken } = useSelector((state) => state.auth);
|
const { user, authToken } = useSelector((state) => state.auth);
|
||||||
const { monitors } = useSelector((state) => state.uptimeMonitors);
|
const { monitors } = useSelector((state) => state.uptimeMonitors);
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
|
||||||
const idMap = {
|
const idMap = {
|
||||||
"monitor-url": "url",
|
"monitor-url": "url",
|
||||||
"monitor-name": "name",
|
"monitor-name": "name",
|
||||||
"monitor-checks-http": "type",
|
"monitor-checks-http": "type",
|
||||||
"monitor-checks-ping": "type",
|
"monitor-checks-ping": "type",
|
||||||
"notify-email-default": "notification-email",
|
"notify-email-default": "notification-email",
|
||||||
};
|
};
|
||||||
|
|
||||||
const { monitorId } = useParams();
|
const { monitorId } = useParams();
|
||||||
const [monitor, setMonitor] = useState({
|
const [monitor, setMonitor] = useState({
|
||||||
url: "",
|
url: "",
|
||||||
name: "",
|
name: "",
|
||||||
type: "http",
|
type: "http",
|
||||||
notifications: [],
|
notifications: [],
|
||||||
interval: 1,
|
interval: 1,
|
||||||
});
|
});
|
||||||
const [https, setHttps] = useState(true);
|
const [https, setHttps] = useState(true);
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchMonitor = async () => {
|
const fetchMonitor = async () => {
|
||||||
if (monitorId) {
|
if (monitorId) {
|
||||||
const action = await dispatch(
|
const action = await dispatch(getUptimeMonitorById({ authToken, monitorId }));
|
||||||
getUptimeMonitorById({ authToken, monitorId })
|
|
||||||
);
|
|
||||||
|
|
||||||
if (action.payload.success) {
|
if (action.payload.success) {
|
||||||
const data = action.payload.data;
|
const data = action.payload.data;
|
||||||
const { name, ...rest } = data; //data.name is read-only
|
const { name, ...rest } = data; //data.name is read-only
|
||||||
if (rest.type === "http") {
|
if (rest.type === "http") {
|
||||||
const url = new URL(rest.url);
|
const url = new URL(rest.url);
|
||||||
rest.url = url.host;
|
rest.url = url.host;
|
||||||
}
|
}
|
||||||
rest.name = `${name} (Clone)`;
|
rest.name = `${name} (Clone)`;
|
||||||
rest.interval /= MS_PER_MINUTE;
|
rest.interval /= MS_PER_MINUTE;
|
||||||
setMonitor({
|
setMonitor({
|
||||||
...rest,
|
...rest,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
navigate("/not-found", { replace: true });
|
navigate("/not-found", { replace: true });
|
||||||
createToast({
|
createToast({
|
||||||
body: "There was an error cloning the monitor.",
|
body: "There was an error cloning the monitor.",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
fetchMonitor();
|
fetchMonitor();
|
||||||
}, [monitorId, authToken, monitors]);
|
}, [monitorId, authToken, monitors]);
|
||||||
|
|
||||||
const handleChange = (event, name) => {
|
const handleChange = (event, name) => {
|
||||||
const { value, id } = event.target;
|
const { value, id } = event.target;
|
||||||
if (!name) name = idMap[id];
|
if (!name) name = idMap[id];
|
||||||
|
|
||||||
if (name.includes("notification-")) {
|
if (name.includes("notification-")) {
|
||||||
name = name.replace("notification-", "");
|
name = name.replace("notification-", "");
|
||||||
let hasNotif = monitor.notifications.some(
|
let hasNotif = monitor.notifications.some(
|
||||||
(notification) => notification.type === name
|
(notification) => notification.type === name
|
||||||
);
|
);
|
||||||
setMonitor((prev) => {
|
setMonitor((prev) => {
|
||||||
const notifs = [...prev.notifications];
|
const notifs = [...prev.notifications];
|
||||||
if (hasNotif) {
|
if (hasNotif) {
|
||||||
return {
|
return {
|
||||||
...prev,
|
...prev,
|
||||||
notifications: notifs.filter((notif) => notif.type !== name),
|
notifications: notifs.filter((notif) => notif.type !== name),
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
...prev,
|
...prev,
|
||||||
notifications: [
|
notifications: [
|
||||||
...notifs,
|
...notifs,
|
||||||
name === "email"
|
name === "email"
|
||||||
? { type: name, address: value }
|
? { type: name, address: value }
|
||||||
: // TODO - phone number
|
: // TODO - phone number
|
||||||
{ type: name, phone: value },
|
{ type: name, phone: value },
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
setMonitor((prev) => ({
|
setMonitor((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
[name]: value,
|
[name]: value,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const { error } = monitorValidation.validate(
|
const { error } = monitorValidation.validate(
|
||||||
{ [name]: value },
|
{ [name]: value },
|
||||||
{ abortEarly: false }
|
{ abortEarly: false }
|
||||||
);
|
);
|
||||||
console.log(error);
|
console.log(error);
|
||||||
setErrors((prev) => {
|
setErrors((prev) => {
|
||||||
const updatedErrors = { ...prev };
|
const updatedErrors = { ...prev };
|
||||||
if (error) updatedErrors[name] = error.details[0].message;
|
if (error) updatedErrors[name] = error.details[0].message;
|
||||||
else delete updatedErrors[name];
|
else delete updatedErrors[name];
|
||||||
return updatedErrors;
|
return updatedErrors;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCreateMonitor = async (event) => {
|
const handleCreateMonitor = async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
//obj to submit
|
//obj to submit
|
||||||
let form = {
|
let form = {
|
||||||
url:
|
url:
|
||||||
//preprending protocol for url
|
//preprending protocol for url
|
||||||
monitor.type === "http"
|
monitor.type === "http"
|
||||||
? `http${https ? "s" : ""}://` + monitor.url
|
? `http${https ? "s" : ""}://` + monitor.url
|
||||||
: monitor.url,
|
: monitor.url,
|
||||||
name: monitor.name === "" ? monitor.url : monitor.name,
|
name: monitor.name === "" ? monitor.url : monitor.name,
|
||||||
type: monitor.type,
|
type: monitor.type,
|
||||||
interval: monitor.interval * MS_PER_MINUTE,
|
interval: monitor.interval * MS_PER_MINUTE,
|
||||||
};
|
};
|
||||||
|
|
||||||
const { error } = monitorValidation.validate(form, {
|
const { error } = monitorValidation.validate(form, {
|
||||||
abortEarly: false,
|
abortEarly: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
const newErrors = {};
|
const newErrors = {};
|
||||||
error.details.forEach((err) => {
|
error.details.forEach((err) => {
|
||||||
newErrors[err.path[0]] = err.message;
|
newErrors[err.path[0]] = err.message;
|
||||||
});
|
});
|
||||||
setErrors(newErrors);
|
setErrors(newErrors);
|
||||||
createToast({ body: "Error validation data." });
|
createToast({ body: "Please check your input for errors and try again" });
|
||||||
} else {
|
} else {
|
||||||
form = {
|
form = {
|
||||||
...form,
|
...form,
|
||||||
description: form.name,
|
description: form.name,
|
||||||
teamId: user.teamId,
|
teamId: user.teamId,
|
||||||
userId: user._id,
|
userId: user._id,
|
||||||
notifications: monitor.notifications,
|
notifications: monitor.notifications,
|
||||||
};
|
};
|
||||||
const action = await dispatch(
|
const action = await dispatch(createUptimeMonitor({ authToken, monitor: form }));
|
||||||
createUptimeMonitor({ authToken, monitor: form })
|
if (action.meta.requestStatus === "fulfilled") {
|
||||||
);
|
createToast({ body: "Monitor created successfully!" });
|
||||||
if (action.meta.requestStatus === "fulfilled") {
|
navigate("/monitors");
|
||||||
createToast({ body: "Monitor created successfully!" });
|
} else {
|
||||||
navigate("/monitors");
|
createToast({ body: "Failed to create monitor." });
|
||||||
} else {
|
}
|
||||||
createToast({ body: "Failed to create monitor." });
|
}
|
||||||
}
|
};
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
//select values
|
//select values
|
||||||
const frequencies = [
|
const frequencies = [
|
||||||
{ _id: 1, name: "1 minute" },
|
{ _id: 1, name: "1 minute" },
|
||||||
{ _id: 2, name: "2 minutes" },
|
{ _id: 2, name: "2 minutes" },
|
||||||
{ _id: 3, name: "3 minutes" },
|
{ _id: 3, name: "3 minutes" },
|
||||||
{ _id: 4, name: "4 minutes" },
|
{ _id: 4, name: "4 minutes" },
|
||||||
{ _id: 5, name: "5 minutes" },
|
{ _id: 5, name: "5 minutes" },
|
||||||
];
|
];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box className="create-monitor">
|
<Box className="create-monitor">
|
||||||
<Breadcrumbs
|
<Breadcrumbs
|
||||||
list={[
|
list={[
|
||||||
{ name: "monitors", path: "/monitors" },
|
{ name: "monitors", path: "/monitors" },
|
||||||
{ name: "create", path: `/monitors/create` },
|
{ name: "create", path: `/monitors/create` },
|
||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
<Stack
|
<Stack
|
||||||
component="form"
|
component="form"
|
||||||
className="create-monitor-form"
|
className="create-monitor-form"
|
||||||
onSubmit={handleCreateMonitor}
|
onSubmit={handleCreateMonitor}
|
||||||
noValidate
|
noValidate
|
||||||
spellCheck="false"
|
spellCheck="false"
|
||||||
gap={theme.spacing(12)}
|
gap={theme.spacing(12)}
|
||||||
mt={theme.spacing(6)}
|
mt={theme.spacing(6)}
|
||||||
>
|
>
|
||||||
<Typography component="h1" variant="h1">
|
<Typography
|
||||||
<Typography component="span" fontSize="inherit">
|
component="h1"
|
||||||
Create your{" "}
|
variant="h1"
|
||||||
</Typography>
|
>
|
||||||
<Typography
|
<Typography
|
||||||
component="span"
|
component="span"
|
||||||
variant="h2"
|
fontSize="inherit"
|
||||||
fontSize="inherit"
|
>
|
||||||
fontWeight="inherit"
|
Create your{" "}
|
||||||
>
|
</Typography>
|
||||||
monitor
|
<Typography
|
||||||
</Typography>
|
component="span"
|
||||||
</Typography>
|
variant="h2"
|
||||||
<ConfigBox>
|
fontSize="inherit"
|
||||||
<Box>
|
fontWeight="inherit"
|
||||||
<Typography component="h2">General settings</Typography>
|
>
|
||||||
<Typography component="p">
|
monitor
|
||||||
Here you can select the URL of the host, together with the type of
|
</Typography>
|
||||||
monitor.
|
</Typography>
|
||||||
</Typography>
|
<ConfigBox>
|
||||||
</Box>
|
<Box>
|
||||||
<Stack gap={theme.spacing(15)}>
|
<Typography component="h2">General settings</Typography>
|
||||||
<Field
|
<Typography component="p">
|
||||||
type={monitor.type === "http" ? "url" : "text"}
|
Here you can select the URL of the host, together with the type of monitor.
|
||||||
id="monitor-url"
|
</Typography>
|
||||||
label="URL to monitor"
|
</Box>
|
||||||
https={https}
|
<Stack gap={theme.spacing(15)}>
|
||||||
placeholder="google.com"
|
<Field
|
||||||
value={monitor.url}
|
type={monitor.type === "http" ? "url" : "text"}
|
||||||
onChange={handleChange}
|
id="monitor-url"
|
||||||
error={errors["url"]}
|
label="URL to monitor"
|
||||||
/>
|
https={https}
|
||||||
<Field
|
placeholder="google.com"
|
||||||
type="text"
|
value={monitor.url}
|
||||||
id="monitor-name"
|
onChange={handleChange}
|
||||||
label="Display name"
|
error={errors["url"]}
|
||||||
isOptional={true}
|
/>
|
||||||
placeholder="Google"
|
<Field
|
||||||
value={monitor.name}
|
type="text"
|
||||||
onChange={handleChange}
|
id="monitor-name"
|
||||||
error={errors["name"]}
|
label="Display name"
|
||||||
/>
|
isOptional={true}
|
||||||
</Stack>
|
placeholder="Google"
|
||||||
</ConfigBox>
|
value={monitor.name}
|
||||||
<ConfigBox>
|
onChange={handleChange}
|
||||||
<Box>
|
error={errors["name"]}
|
||||||
<Typography component="h2">Checks to perform</Typography>
|
/>
|
||||||
<Typography component="p">
|
</Stack>
|
||||||
You can always add or remove checks after adding your site.
|
</ConfigBox>
|
||||||
</Typography>
|
<ConfigBox>
|
||||||
</Box>
|
<Box>
|
||||||
<Stack gap={theme.spacing(12)}>
|
<Typography component="h2">Checks to perform</Typography>
|
||||||
<Stack gap={theme.spacing(6)}>
|
<Typography component="p">
|
||||||
<Radio
|
You can always add or remove checks after adding your site.
|
||||||
id="monitor-checks-http"
|
</Typography>
|
||||||
title="Website monitoring"
|
</Box>
|
||||||
desc="Use HTTP(s) to monitor your website or API endpoint."
|
<Stack gap={theme.spacing(12)}>
|
||||||
size="small"
|
<Stack gap={theme.spacing(6)}>
|
||||||
value="http"
|
<Radio
|
||||||
checked={monitor.type === "http"}
|
id="monitor-checks-http"
|
||||||
onChange={(event) => handleChange(event)}
|
title="Website monitoring"
|
||||||
/>
|
desc="Use HTTP(s) to monitor your website or API endpoint."
|
||||||
{monitor.type === "http" ? (
|
size="small"
|
||||||
<ButtonGroup sx={{ ml: theme.spacing(16) }}>
|
value="http"
|
||||||
<Button
|
checked={monitor.type === "http"}
|
||||||
variant="group"
|
onChange={(event) => handleChange(event)}
|
||||||
filled={https.toString()}
|
/>
|
||||||
onClick={() => setHttps(true)}
|
{monitor.type === "http" ? (
|
||||||
>
|
<ButtonGroup sx={{ ml: theme.spacing(16) }}>
|
||||||
HTTPS
|
<Button
|
||||||
</Button>
|
variant="group"
|
||||||
<Button
|
filled={https.toString()}
|
||||||
variant="group"
|
onClick={() => setHttps(true)}
|
||||||
filled={(!https).toString()}
|
>
|
||||||
onClick={() => setHttps(false)}
|
HTTPS
|
||||||
>
|
</Button>
|
||||||
HTTP
|
<Button
|
||||||
</Button>
|
variant="group"
|
||||||
</ButtonGroup>
|
filled={(!https).toString()}
|
||||||
) : (
|
onClick={() => setHttps(false)}
|
||||||
""
|
>
|
||||||
)}
|
HTTP
|
||||||
</Stack>
|
</Button>
|
||||||
<Radio
|
</ButtonGroup>
|
||||||
id="monitor-checks-ping"
|
) : (
|
||||||
title="Ping monitoring"
|
""
|
||||||
desc="Check whether your server is available or not."
|
)}
|
||||||
size="small"
|
</Stack>
|
||||||
value="ping"
|
<Radio
|
||||||
checked={monitor.type === "ping"}
|
id="monitor-checks-ping"
|
||||||
onChange={(event) => handleChange(event)}
|
title="Ping monitoring"
|
||||||
/>
|
desc="Check whether your server is available or not."
|
||||||
{errors["type"] ? (
|
size="small"
|
||||||
<Box className="error-container">
|
value="ping"
|
||||||
<Typography
|
checked={monitor.type === "ping"}
|
||||||
component="p"
|
onChange={(event) => handleChange(event)}
|
||||||
className="input-error"
|
/>
|
||||||
color={theme.palette.error.text}
|
{errors["type"] ? (
|
||||||
>
|
<Box className="error-container">
|
||||||
{errors["type"]}
|
<Typography
|
||||||
</Typography>
|
component="p"
|
||||||
</Box>
|
className="input-error"
|
||||||
) : (
|
color={theme.palette.error.text}
|
||||||
""
|
>
|
||||||
)}
|
{errors["type"]}
|
||||||
</Stack>
|
</Typography>
|
||||||
</ConfigBox>
|
</Box>
|
||||||
<ConfigBox>
|
) : (
|
||||||
<Box>
|
""
|
||||||
<Typography component="h2">Incident notifications</Typography>
|
)}
|
||||||
<Typography component="p">
|
</Stack>
|
||||||
When there is an incident, notify users.
|
</ConfigBox>
|
||||||
</Typography>
|
<ConfigBox>
|
||||||
</Box>
|
<Box>
|
||||||
<Stack gap={theme.spacing(6)}>
|
<Typography component="h2">Incident notifications</Typography>
|
||||||
<Typography component="p">When there is a new incident,</Typography>
|
<Typography component="p">
|
||||||
<Checkbox
|
When there is an incident, notify users.
|
||||||
id="notify-sms"
|
</Typography>
|
||||||
label="Notify via SMS (coming soon)"
|
</Box>
|
||||||
isChecked={false}
|
<Stack gap={theme.spacing(6)}>
|
||||||
value=""
|
<Typography component="p">When there is a new incident,</Typography>
|
||||||
onChange={() => logger.warn("disabled")}
|
<Checkbox
|
||||||
isDisabled={true}
|
id="notify-sms"
|
||||||
/>
|
label="Notify via SMS (coming soon)"
|
||||||
<Checkbox
|
isChecked={false}
|
||||||
id="notify-email-default"
|
value=""
|
||||||
label={`Notify via email (to ${user.email})`}
|
onChange={() => logger.warn("disabled")}
|
||||||
isChecked={monitor.notifications.some(
|
isDisabled={true}
|
||||||
(notification) => notification.type === "email"
|
/>
|
||||||
)}
|
<Checkbox
|
||||||
value={user?.email}
|
id="notify-email-default"
|
||||||
onChange={(event) => handleChange(event)}
|
label={`Notify via email (to ${user.email})`}
|
||||||
/>
|
isChecked={monitor.notifications.some(
|
||||||
<Checkbox
|
(notification) => notification.type === "email"
|
||||||
id="notify-email"
|
)}
|
||||||
label="Also notify via email to multiple addresses (coming soon)"
|
value={user?.email}
|
||||||
isChecked={false}
|
onChange={(event) => handleChange(event)}
|
||||||
value=""
|
/>
|
||||||
onChange={() => logger.warn("disabled")}
|
<Checkbox
|
||||||
isDisabled={true}
|
id="notify-email"
|
||||||
/>
|
label="Also notify via email to multiple addresses (coming soon)"
|
||||||
{monitor.notifications.some(
|
isChecked={false}
|
||||||
(notification) => notification.type === "emails"
|
value=""
|
||||||
) ? (
|
onChange={() => logger.warn("disabled")}
|
||||||
<Box mx={theme.spacing(16)}>
|
isDisabled={true}
|
||||||
<Field
|
/>
|
||||||
id="notify-email-list"
|
{monitor.notifications.some(
|
||||||
type="text"
|
(notification) => notification.type === "emails"
|
||||||
placeholder="name@gmail.com"
|
) ? (
|
||||||
value=""
|
<Box mx={theme.spacing(16)}>
|
||||||
onChange={() => logger.warn("disabled")}
|
<Field
|
||||||
/>
|
id="notify-email-list"
|
||||||
<Typography mt={theme.spacing(4)}>
|
type="text"
|
||||||
You can separate multiple emails with a comma
|
placeholder="name@gmail.com"
|
||||||
</Typography>
|
value=""
|
||||||
</Box>
|
onChange={() => logger.warn("disabled")}
|
||||||
) : (
|
/>
|
||||||
""
|
<Typography mt={theme.spacing(4)}>
|
||||||
)}
|
You can separate multiple emails with a comma
|
||||||
</Stack>
|
</Typography>
|
||||||
</ConfigBox>
|
</Box>
|
||||||
<ConfigBox>
|
) : (
|
||||||
<Box>
|
""
|
||||||
<Typography component="h2">Advanced settings</Typography>
|
)}
|
||||||
</Box>
|
</Stack>
|
||||||
<Stack gap={theme.spacing(12)}>
|
</ConfigBox>
|
||||||
<Select
|
<ConfigBox>
|
||||||
id="monitor-interval"
|
<Box>
|
||||||
label="Check frequency"
|
<Typography component="h2">Advanced settings</Typography>
|
||||||
value={monitor.interval || 1}
|
</Box>
|
||||||
onChange={(event) => handleChange(event, "interval")}
|
<Stack gap={theme.spacing(12)}>
|
||||||
items={frequencies}
|
<Select
|
||||||
/>
|
id="monitor-interval"
|
||||||
</Stack>
|
label="Check frequency"
|
||||||
</ConfigBox>
|
value={monitor.interval || 1}
|
||||||
<Stack direction="row" justifyContent="flex-end">
|
onChange={(event) => handleChange(event, "interval")}
|
||||||
<Button
|
items={frequencies}
|
||||||
variant="contained"
|
/>
|
||||||
color="primary"
|
</Stack>
|
||||||
onClick={handleCreateMonitor}
|
</ConfigBox>
|
||||||
disabled={Object.keys(errors).length !== 0 && true}
|
<Stack
|
||||||
>
|
direction="row"
|
||||||
Create monitor
|
justifyContent="flex-end"
|
||||||
</Button>
|
>
|
||||||
</Stack>
|
<Button
|
||||||
</Stack>
|
variant="contained"
|
||||||
</Box>
|
color="primary"
|
||||||
);
|
onClick={handleCreateMonitor}
|
||||||
|
disabled={Object.keys(errors).length !== 0 && true}
|
||||||
|
>
|
||||||
|
Create monitor
|
||||||
|
</Button>
|
||||||
|
</Stack>
|
||||||
|
</Stack>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default CreateMonitor;
|
export default CreateMonitor;
|
||||||
Reference in new issue
Block a user