Add docker container to create monitor options. Add map for labels and placeholders
This commit is contained in:
1 parent
a14e88ef29
commit
206d1f3909
1 file changed
+61
-32
@@ -1,10 +1,10 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { Box, Button, ButtonGroup, Stack, Typography } from "@mui/material";
|
import { Box, Button, ButtonGroup, Stack, Typography } from "@mui/material";
|
||||||
import LoadingButton from '@mui/lab/LoadingButton';
|
import LoadingButton from "@mui/lab/LoadingButton";
|
||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector, useDispatch } from "react-redux";
|
||||||
import { monitorValidation } from "../../../Validation/validation";
|
import { monitorValidation } from "../../../Validation/validation";
|
||||||
import { createUptimeMonitor } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
import { createUptimeMonitor } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
||||||
import { checkEndpointResolution } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice"
|
import { checkEndpointResolution } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
||||||
import { useNavigate, useParams } from "react-router-dom";
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import { createToast } from "../../../Utils/toastUtils";
|
import { createToast } from "../../../Utils/toastUtils";
|
||||||
@@ -19,21 +19,40 @@ 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, isLoading } = useSelector((state) => state.uptimeMonitors);
|
const { monitors, isLoading } = 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",
|
||||||
|
"monitor-checks-docker": "type",
|
||||||
"notify-email-default": "notification-email",
|
"notify-email-default": "notification-email",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const monitorTypeMaps = {
|
||||||
|
http: {
|
||||||
|
label: "URL to monitor",
|
||||||
|
placeholder: "google.com",
|
||||||
|
namePlaceholder: "Google",
|
||||||
|
},
|
||||||
|
ping: {
|
||||||
|
label: "IP address to monitor",
|
||||||
|
placeholder: "1.1.1.1",
|
||||||
|
namePlaceholder: "Google",
|
||||||
|
},
|
||||||
|
docker: {
|
||||||
|
label: "Container ID",
|
||||||
|
placeholder: "abc123",
|
||||||
|
namePlaceholder: "My Container",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const { monitorId } = useParams();
|
const { monitorId } = useParams();
|
||||||
const [monitor, setMonitor] = useState({
|
const [monitor, setMonitor] = useState({
|
||||||
url: "",
|
url: "",
|
||||||
@@ -112,7 +131,6 @@ const CreateMonitor = () => {
|
|||||||
{ [name]: value },
|
{ [name]: value },
|
||||||
{ abortEarly: false }
|
{ abortEarly: false }
|
||||||
);
|
);
|
||||||
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;
|
||||||
@@ -148,16 +166,18 @@ const CreateMonitor = () => {
|
|||||||
setErrors(newErrors);
|
setErrors(newErrors);
|
||||||
createToast({ body: "Error validation data." });
|
createToast({ body: "Error validation data." });
|
||||||
} else {
|
} else {
|
||||||
if (monitor.type === "http") {
|
if (monitor.type === "http") {
|
||||||
const checkEndpointAction = await dispatch(
|
const checkEndpointAction = await dispatch(
|
||||||
checkEndpointResolution({ authToken, monitorURL: form.url })
|
checkEndpointResolution({ authToken, monitorURL: form.url })
|
||||||
)
|
);
|
||||||
if (checkEndpointAction.meta.requestStatus === "rejected") {
|
if (checkEndpointAction.meta.requestStatus === "rejected") {
|
||||||
createToast({ body: "The endpoint you entered doesn't resolve. Check the URL again." });
|
createToast({
|
||||||
setErrors({ url: "The entered URL is not reachable." });
|
body: "The endpoint you entered doesn't resolve. Check the URL again.",
|
||||||
return;
|
});
|
||||||
}
|
setErrors({ url: "The entered URL is not reachable." });
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
form = {
|
form = {
|
||||||
...form,
|
...form,
|
||||||
@@ -232,9 +252,9 @@ const CreateMonitor = () => {
|
|||||||
<Field
|
<Field
|
||||||
type={monitor.type === "http" ? "url" : "text"}
|
type={monitor.type === "http" ? "url" : "text"}
|
||||||
id="monitor-url"
|
id="monitor-url"
|
||||||
label="URL to monitor"
|
label={monitorTypeMaps[monitor.type].label || "URL to monitor"}
|
||||||
https={https}
|
https={https}
|
||||||
placeholder="google.com"
|
placeholder={monitorTypeMaps[monitor.type].placeholder || ""}
|
||||||
value={monitor.url}
|
value={monitor.url}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
error={errors["url"]}
|
error={errors["url"]}
|
||||||
@@ -244,7 +264,7 @@ const CreateMonitor = () => {
|
|||||||
id="monitor-name"
|
id="monitor-name"
|
||||||
label="Display name"
|
label="Display name"
|
||||||
isOptional={true}
|
isOptional={true}
|
||||||
placeholder="Google"
|
placeholder={monitorTypeMaps[monitor.type].namePlaceholder || ""}
|
||||||
value={monitor.name}
|
value={monitor.name}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
error={errors["name"]}
|
error={errors["name"]}
|
||||||
@@ -299,6 +319,15 @@ const CreateMonitor = () => {
|
|||||||
checked={monitor.type === "ping"}
|
checked={monitor.type === "ping"}
|
||||||
onChange={(event) => handleChange(event)}
|
onChange={(event) => handleChange(event)}
|
||||||
/>
|
/>
|
||||||
|
<Radio
|
||||||
|
id="monitor-checks-docker"
|
||||||
|
title="Docker container monitoring"
|
||||||
|
desc="Check whether your container is running or not."
|
||||||
|
size="small"
|
||||||
|
value="docker"
|
||||||
|
checked={monitor.type === "docker"}
|
||||||
|
onChange={(event) => handleChange(event)}
|
||||||
|
/>
|
||||||
{errors["type"] ? (
|
{errors["type"] ? (
|
||||||
<Box className="error-container">
|
<Box className="error-container">
|
||||||
<Typography
|
<Typography
|
||||||
@@ -386,19 +415,19 @@ const CreateMonitor = () => {
|
|||||||
direction="row"
|
direction="row"
|
||||||
justifyContent="flex-end"
|
justifyContent="flex-end"
|
||||||
>
|
>
|
||||||
<LoadingButton
|
<LoadingButton
|
||||||
variant="contained"
|
variant="contained"
|
||||||
color="primary"
|
color="primary"
|
||||||
onClick={handleCreateMonitor}
|
onClick={handleCreateMonitor}
|
||||||
disabled={Object.keys(errors).length !== 0 && true}
|
disabled={Object.keys(errors).length !== 0 && true}
|
||||||
loading={isLoading}
|
loading={isLoading}
|
||||||
>
|
>
|
||||||
Create monitor
|
Create monitor
|
||||||
</LoadingButton>
|
</LoadingButton>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default CreateMonitor;
|
export default CreateMonitor;
|
||||||
Reference in new issue
Block a user