implement cconfig using create component
This commit is contained in:
1 parent
dc84bd8848
commit
ab16ab0d70
3 files changed
+132
-31
No files matched your search
@@ -0,0 +1,33 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { networkService } from "../../../../main";
|
||||||
|
import { useSelector } from "react-redux";
|
||||||
|
import { createToast } from "../../../../Utils/toastUtils";
|
||||||
|
|
||||||
|
const useCreateDistributedUptimeMonitor = ({ isCreate, monitorId }) => {
|
||||||
|
const { authToken, user } = useSelector((state) => state.auth);
|
||||||
|
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [networkError, setNetworkError] = useState(false);
|
||||||
|
const createDistributedUptimeMonitor = async ({ form }) => {
|
||||||
|
setIsLoading(true);
|
||||||
|
try {
|
||||||
|
if (isCreate) {
|
||||||
|
await networkService.createMonitor({ authToken, monitor: form });
|
||||||
|
} else {
|
||||||
|
await networkService.updateMonitor({ authToken, monitor: form, monitorId });
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
setNetworkError(true);
|
||||||
|
createToast({ body: error?.response?.data?.msg ?? error.message });
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return [createDistributedUptimeMonitor, isLoading, networkError];
|
||||||
|
};
|
||||||
|
|
||||||
|
export { useCreateDistributedUptimeMonitor };
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { networkService } from "../../../../main";
|
||||||
|
import { createToast } from "../../../../Utils/toastUtils";
|
||||||
|
|
||||||
|
export const useMonitorFetch = ({ authToken, monitorId, isCreate }) => {
|
||||||
|
const [networkError, setNetworkError] = useState(false);
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [monitor, setMonitor] = useState(undefined);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchMonitors = async () => {
|
||||||
|
try {
|
||||||
|
if (isCreate) return;
|
||||||
|
const res = await networkService.getUptimeDetailsById({
|
||||||
|
authToken: authToken,
|
||||||
|
monitorId: monitorId,
|
||||||
|
normalize: true,
|
||||||
|
});
|
||||||
|
setMonitor(res?.data?.data ?? {});
|
||||||
|
} catch (error) {
|
||||||
|
setNetworkError(true);
|
||||||
|
createToast({ body: error.message });
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
fetchMonitors();
|
||||||
|
}, [authToken, monitorId, isCreate]);
|
||||||
|
return [monitor, isLoading, networkError];
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useMonitorFetch;
|
||||||
@@ -13,18 +13,15 @@ import { createToast } from "../../../Utils/toastUtils";
|
|||||||
|
|
||||||
// Utility
|
// Utility
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import { useState } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector } from "react-redux";
|
||||||
import { monitorValidation } from "../../../Validation/validation";
|
import { monitorValidation } from "../../../Validation/validation";
|
||||||
import { createUptimeMonitor } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
import { useParams } from "react-router-dom";
|
||||||
import { useLocation } from "react-router-dom";
|
import { useCreateDistributedUptimeMonitor } from "./Hooks/useCreateDistributedUptimeMonitor";
|
||||||
|
import { useMonitorFetch } from "./Hooks/useMonitorFetch";
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
const BREADCRUMBS = [
|
|
||||||
{ name: `distributed uptime`, path: "/distributed-uptime" },
|
|
||||||
{ name: "create", path: `/distributed-uptime/create` },
|
|
||||||
];
|
|
||||||
const MS_PER_MINUTE = 60000;
|
const MS_PER_MINUTE = 60000;
|
||||||
const SELECT_VALUES = [
|
const SELECT_VALUES = [
|
||||||
{ _id: 1, name: "1 minute" },
|
{ _id: 1, name: "1 minute" },
|
||||||
@@ -34,18 +31,30 @@ const SELECT_VALUES = [
|
|||||||
{ _id: 5, name: "5 minutes" },
|
{ _id: 5, name: "5 minutes" },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const parseUrl = (url) => {
|
||||||
|
try {
|
||||||
|
return new URL(url);
|
||||||
|
} catch (error) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const CreateDistributedUptime = () => {
|
const CreateDistributedUptime = () => {
|
||||||
const location = useLocation();
|
const { monitorId } = useParams();
|
||||||
const isCreate = location.pathname.startsWith("/distributed-uptime/create");
|
const isCreate = typeof monitorId === "undefined";
|
||||||
|
|
||||||
|
const BREADCRUMBS = [
|
||||||
|
{ name: `distributed uptime`, path: "/distributed-uptime" },
|
||||||
|
{ name: isCreate ? "create" : "configure", path: `` },
|
||||||
|
];
|
||||||
|
|
||||||
// Redux state
|
// Redux state
|
||||||
const { user, authToken } = useSelector((state) => state.auth);
|
const { user, authToken } = useSelector((state) => state.auth);
|
||||||
const isLoading = useSelector((state) => state.uptimeMonitors.isLoading);
|
|
||||||
|
|
||||||
// Local state
|
// Local state
|
||||||
const [https, setHttps] = useState(true);
|
const [https, setHttps] = useState(true);
|
||||||
const [notifications, setNotifications] = useState([]);
|
const [notifications, setNotifications] = useState([]);
|
||||||
const [monitor, setMonitor] = useState({
|
const [form, setForm] = useState({
|
||||||
type: "distributed_http",
|
type: "distributed_http",
|
||||||
name: "",
|
name: "",
|
||||||
url: "",
|
url: "",
|
||||||
@@ -55,12 +64,37 @@ const CreateDistributedUptime = () => {
|
|||||||
|
|
||||||
//utils
|
//utils
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const dispatch = useDispatch();
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const [createDistributedUptimeMonitor, isLoading, networkError] =
|
||||||
|
useCreateDistributedUptimeMonitor({ isCreate, monitorId });
|
||||||
|
|
||||||
|
const [monitor, monitorIsLoading, monitorNetworkError] = useMonitorFetch({
|
||||||
|
authToken,
|
||||||
|
monitorId,
|
||||||
|
isCreate,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Effect to set monitor to fetched monitor
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof monitor !== "undefined") {
|
||||||
|
const parsedUrl = parseUrl(monitor?.url);
|
||||||
|
const protocol = parsedUrl?.protocol?.replace(":", "") || "";
|
||||||
|
setHttps(protocol === "https");
|
||||||
|
|
||||||
|
const newForm = {
|
||||||
|
name: monitor.name,
|
||||||
|
interval: monitor.interval / MS_PER_MINUTE,
|
||||||
|
url: parsedUrl.host,
|
||||||
|
type: monitor.type,
|
||||||
|
};
|
||||||
|
|
||||||
|
setForm(newForm);
|
||||||
|
}
|
||||||
|
}, [monitor]);
|
||||||
|
|
||||||
// Handlers
|
// Handlers
|
||||||
const handleCreateMonitor = async (event) => {
|
const handleCreateMonitor = async () => {
|
||||||
const monitorToSubmit = { ...monitor };
|
const monitorToSubmit = { ...form };
|
||||||
|
|
||||||
// Prepend protocol to url
|
// Prepend protocol to url
|
||||||
monitorToSubmit.url = `http${https ? "s" : ""}://` + monitorToSubmit.url;
|
monitorToSubmit.url = `http${https ? "s" : ""}://` + monitorToSubmit.url;
|
||||||
@@ -68,7 +102,6 @@ const CreateDistributedUptime = () => {
|
|||||||
const { error } = monitorValidation.validate(monitorToSubmit, {
|
const { error } = monitorValidation.validate(monitorToSubmit, {
|
||||||
abortEarly: false,
|
abortEarly: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
const newErrors = {};
|
const newErrors = {};
|
||||||
error.details.forEach((err) => {
|
error.details.forEach((err) => {
|
||||||
@@ -80,16 +113,14 @@ const CreateDistributedUptime = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Append needed fields
|
// Append needed fields
|
||||||
monitorToSubmit.description = monitor.name;
|
monitorToSubmit.description = form.name;
|
||||||
monitorToSubmit.interval = monitor.interval * MS_PER_MINUTE;
|
monitorToSubmit.interval = form.interval * MS_PER_MINUTE;
|
||||||
monitorToSubmit.teamId = user.teamId;
|
monitorToSubmit.teamId = user.teamId;
|
||||||
monitorToSubmit.userId = user._id;
|
monitorToSubmit.userId = user._id;
|
||||||
monitorToSubmit.notifications = notifications;
|
monitorToSubmit.notifications = notifications;
|
||||||
|
|
||||||
const action = await dispatch(
|
const success = await createDistributedUptimeMonitor({ form: monitorToSubmit });
|
||||||
createUptimeMonitor({ authToken, monitor: monitorToSubmit })
|
if (success) {
|
||||||
);
|
|
||||||
if (action.meta.requestStatus === "fulfilled") {
|
|
||||||
createToast({ body: "Monitor created successfully!" });
|
createToast({ body: "Monitor created successfully!" });
|
||||||
navigate("/distributed-uptime");
|
navigate("/distributed-uptime");
|
||||||
} else {
|
} else {
|
||||||
@@ -98,9 +129,10 @@ const CreateDistributedUptime = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleChange = (event) => {
|
const handleChange = (event) => {
|
||||||
const { name, value } = event.target;
|
let { name, value } = event.target;
|
||||||
setMonitor({
|
|
||||||
...monitor,
|
setForm({
|
||||||
|
...form,
|
||||||
[name]: value,
|
[name]: value,
|
||||||
});
|
});
|
||||||
const { error } = monitorValidation.validate(
|
const { error } = monitorValidation.validate(
|
||||||
@@ -178,7 +210,8 @@ const CreateDistributedUptime = () => {
|
|||||||
label="URL to monitor"
|
label="URL to monitor"
|
||||||
https={https}
|
https={https}
|
||||||
placeholder={"www.google.com"}
|
placeholder={"www.google.com"}
|
||||||
value={monitor.url}
|
disabled={!isCreate}
|
||||||
|
value={form.url}
|
||||||
name="url"
|
name="url"
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
error={errors["url"] ? true : false}
|
error={errors["url"] ? true : false}
|
||||||
@@ -190,7 +223,7 @@ const CreateDistributedUptime = () => {
|
|||||||
label="Display name"
|
label="Display name"
|
||||||
isOptional={true}
|
isOptional={true}
|
||||||
placeholder={"Google"}
|
placeholder={"Google"}
|
||||||
value={monitor.name}
|
value={form.name}
|
||||||
name="name"
|
name="name"
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
error={errors["name"] ? true : false}
|
error={errors["name"] ? true : false}
|
||||||
@@ -217,8 +250,11 @@ const CreateDistributedUptime = () => {
|
|||||||
checked={true}
|
checked={true}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
/>
|
/>
|
||||||
{monitor.type === "http" || monitor.type === "distributed_http" ? (
|
{form.type === "http" || form.type === "distributed_http" ? (
|
||||||
<ButtonGroup sx={{ ml: theme.spacing(16) }}>
|
<ButtonGroup
|
||||||
|
disabled={!isCreate}
|
||||||
|
sx={{ ml: theme.spacing(16) }}
|
||||||
|
>
|
||||||
<Button
|
<Button
|
||||||
variant="group"
|
variant="group"
|
||||||
filled={https.toString()}
|
filled={https.toString()}
|
||||||
@@ -282,7 +318,7 @@ const CreateDistributedUptime = () => {
|
|||||||
id="monitor-interval"
|
id="monitor-interval"
|
||||||
label="Check frequency"
|
label="Check frequency"
|
||||||
name="interval"
|
name="interval"
|
||||||
value={monitor.interval || 1}
|
value={form.interval}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
items={SELECT_VALUES}
|
items={SELECT_VALUES}
|
||||||
/>
|
/>
|
||||||
@@ -299,7 +335,7 @@ const CreateDistributedUptime = () => {
|
|||||||
disabled={!Object.values(errors).every((value) => value === undefined)}
|
disabled={!Object.values(errors).every((value) => value === undefined)}
|
||||||
loading={isLoading}
|
loading={isLoading}
|
||||||
>
|
>
|
||||||
Create monitor
|
{isCreate ? "Create monitor" : "Configure monitor"}
|
||||||
</LoadingButton>
|
</LoadingButton>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|||||||
Reference in new issue
Block a user