Refactored old states
This commit is contained in:
1 parent
40c550e81f
commit
01854dd5e7
6 files changed
+97
-124
No files matched your search
@@ -101,7 +101,9 @@
|
|||||||
/* ///// */
|
/* ///// */
|
||||||
/* LOGIN */
|
/* LOGIN */
|
||||||
/* ///// */
|
/* ///// */
|
||||||
.login-page span:not(.MuiTypography-root):not(.MuiButtonBase-root) {
|
.login-page
|
||||||
|
.MuiStack-root:not(:has(> .MuiButtonBase-root))
|
||||||
|
span:not(.MuiTypography-root) {
|
||||||
color: var(--env-var-color-3);
|
color: var(--env-var-color-3);
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
|
|||||||
@@ -18,7 +18,8 @@
|
|||||||
.create-monitor p.MuiTypography-root {
|
.create-monitor p.MuiTypography-root {
|
||||||
color: var(--env-var-color-2);
|
color: var(--env-var-color-2);
|
||||||
}
|
}
|
||||||
.create-monitor h6.MuiTypography-root {
|
.create-monitor h6.MuiTypography-root,
|
||||||
|
.create-monitor .MuiBox-root .field + p.MuiTypography-root {
|
||||||
font-size: var(--env-var-font-size-small-plus);
|
font-size: var(--env-var-font-size-small-plus);
|
||||||
opacity: 0.8;
|
opacity: 0.8;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import WestRoundedIcon from "@mui/icons-material/WestRounded";
|
|||||||
import Field from "../../../Components/Inputs/Field";
|
import Field from "../../../Components/Inputs/Field";
|
||||||
import Select from "../../../Components/Inputs/Select";
|
import Select from "../../../Components/Inputs/Select";
|
||||||
import Checkbox from "../../../Components/Inputs/Checkbox";
|
import Checkbox from "../../../Components/Inputs/Checkbox";
|
||||||
|
import { createToast } from "../../../Utils/toastUtils";
|
||||||
|
|
||||||
const CreateMonitor = () => {
|
const CreateMonitor = () => {
|
||||||
const MS_PER_MINUTE = 60000;
|
const MS_PER_MINUTE = 60000;
|
||||||
@@ -20,86 +21,61 @@ const CreateMonitor = () => {
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
|
||||||
|
const idMap = {
|
||||||
|
"monitor-url": "url",
|
||||||
|
"monitor-name": "name",
|
||||||
|
"monitor-checks-http": "type",
|
||||||
|
"monitor-checks-ping": "type",
|
||||||
|
"notify-sms": "sms",
|
||||||
|
"notify-email-default": "email",
|
||||||
|
};
|
||||||
|
|
||||||
|
const [monitor, setMonitor] = useState({
|
||||||
|
url: "",
|
||||||
|
name: "",
|
||||||
|
type: "",
|
||||||
|
notifications: [],
|
||||||
|
interval: 1,
|
||||||
|
});
|
||||||
|
const [https, setHttps] = useState(false);
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
|
|
||||||
//General Settings Form
|
const handleChange = (event, name) => {
|
||||||
const [generalSettings, setGeneralSettings] = useState({ url: "", name: "" });
|
const { value, id } = event.target;
|
||||||
//Checks Form
|
if (!name) name = idMap[id];
|
||||||
const [checks, setChecks] = useState({
|
setMonitor((prev) => ({
|
||||||
type: "",
|
|
||||||
// port: ""
|
|
||||||
});
|
|
||||||
//Incidents Form
|
|
||||||
// const [notifications, setNotifications] = useState({
|
|
||||||
// viaSms: false,
|
|
||||||
// viaEmail: false,
|
|
||||||
// viaOther: false,
|
|
||||||
// email: "",
|
|
||||||
// });
|
|
||||||
//Advanced Settings Form
|
|
||||||
const [advancedSettings, setAdvancedSettings] = useState({
|
|
||||||
interval: 1,
|
|
||||||
// retries: "",
|
|
||||||
// codes: "",
|
|
||||||
// redirects: "",
|
|
||||||
});
|
|
||||||
//Proxy Settings Form
|
|
||||||
// const [proxy, setProxy] = useState({
|
|
||||||
// enabled: false,
|
|
||||||
// protocol: "",
|
|
||||||
// address: "",
|
|
||||||
// proxy_port: "",
|
|
||||||
// });
|
|
||||||
|
|
||||||
const handleChange = (event, id, setState, checkbox) => {
|
|
||||||
const { value } = event.target;
|
|
||||||
setState((prev) => ({
|
|
||||||
...prev,
|
...prev,
|
||||||
[id]: checkbox ? true : value,
|
[name]: value,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const validation = monitorValidation.validate(
|
const { error } = monitorValidation.validate(
|
||||||
{ [id]: value },
|
{ [name]: value },
|
||||||
{ abortEarly: false }
|
{ abortEarly: false }
|
||||||
);
|
);
|
||||||
|
|
||||||
setErrors((prev) => {
|
setErrors((prev) => {
|
||||||
const updatedErrors = { ...prev };
|
const updatedErrors = { ...prev };
|
||||||
|
if (error) updatedErrors[name] = error.details[0].message;
|
||||||
if (validation.error) {
|
else delete updatedErrors[name];
|
||||||
updatedErrors[id] = validation.error.details[0].message;
|
|
||||||
} else {
|
|
||||||
delete updatedErrors[id];
|
|
||||||
}
|
|
||||||
return updatedErrors;
|
return updatedErrors;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
// const handleCheck = (id, setState) => {
|
|
||||||
// setState((prev) => ({
|
|
||||||
// ...prev,
|
|
||||||
// [id]: !prev[id],
|
|
||||||
// }));
|
|
||||||
// };
|
|
||||||
|
|
||||||
const handleCreateMonitor = async (event) => {
|
const handleCreateMonitor = async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
//obj to submit
|
//obj to submit
|
||||||
let monitor = {
|
let form = {
|
||||||
url:
|
url:
|
||||||
//preprending protocol for url
|
//preprending protocol for url
|
||||||
checks.type === "http" || checks.type === "https"
|
monitor.type === "http"
|
||||||
? `${checks.type}://` + generalSettings.url
|
? `http${https ? "s" : ""}://` + monitor.url
|
||||||
: generalSettings.url,
|
: monitor.url,
|
||||||
name:
|
name: monitor.name === "" ? monitor.url : monitor.name,
|
||||||
generalSettings.name === ""
|
type: monitor.type,
|
||||||
? generalSettings.url
|
interval: monitor.interval * MS_PER_MINUTE,
|
||||||
: generalSettings.name,
|
|
||||||
//there is no separate monitor type for https since the operations for the two protocols are identical
|
|
||||||
//however the URL does need the correct prepend hence https is still being tracked but overwritten when prepping the monitor obj
|
|
||||||
type: checks.type === "https" ? "http" : checks.type,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const { error } = monitorValidation.validate(monitor, {
|
const { error } = monitorValidation.validate(form, {
|
||||||
abortEarly: false,
|
abortEarly: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -109,29 +85,26 @@ const CreateMonitor = () => {
|
|||||||
newErrors[err.path[0]] = err.message;
|
newErrors[err.path[0]] = err.message;
|
||||||
});
|
});
|
||||||
setErrors(newErrors);
|
setErrors(newErrors);
|
||||||
|
createToast({ body: "Error validation data." });
|
||||||
} else {
|
} else {
|
||||||
monitor = {
|
form = {
|
||||||
...monitor,
|
...form,
|
||||||
description: monitor.name,
|
description: form.name,
|
||||||
userId: user._id,
|
userId: user._id,
|
||||||
// ...advancedSettings
|
|
||||||
interval: advancedSettings.interval * MS_PER_MINUTE,
|
|
||||||
};
|
};
|
||||||
try {
|
const action = await dispatch(
|
||||||
const action = await dispatch(
|
createUptimeMonitor({ authToken, monitor: form })
|
||||||
createUptimeMonitor({ authToken, monitor })
|
);
|
||||||
);
|
if (action.meta.requestStatus === "fulfilled") {
|
||||||
if (action.meta.requestStatus === "fulfilled") {
|
createToast({ body: "Monitor created successfully!" });
|
||||||
navigate("/monitors");
|
navigate("/monitors");
|
||||||
}
|
} else {
|
||||||
} catch (error) {
|
createToast({ body: "Failed to delete monitor." });
|
||||||
alert(error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//select values
|
//select values
|
||||||
// const ports = ["Port 1", "Port 2", "Port 3"];
|
|
||||||
const frequencies = [
|
const frequencies = [
|
||||||
{ _id: 1, name: "1 minute" },
|
{ _id: 1, name: "1 minute" },
|
||||||
{ _id: 2, name: "2 minutes" },
|
{ _id: 2, name: "2 minutes" },
|
||||||
@@ -175,19 +148,13 @@ const CreateMonitor = () => {
|
|||||||
</Box>
|
</Box>
|
||||||
<Stack gap={theme.gap.xl}>
|
<Stack gap={theme.gap.xl}>
|
||||||
<Field
|
<Field
|
||||||
type={
|
type={monitor.type === "http" ? "url" : "text"}
|
||||||
checks.type === "http" || checks.type === "https"
|
|
||||||
? "url"
|
|
||||||
: "text"
|
|
||||||
}
|
|
||||||
id="monitor-url"
|
id="monitor-url"
|
||||||
label="URL to monitor"
|
label="URL to monitor"
|
||||||
https={checks.type === "https"}
|
https={https}
|
||||||
placeholder="google.com"
|
placeholder="google.com"
|
||||||
value={generalSettings.url}
|
value={monitor.url}
|
||||||
onChange={(event) =>
|
onChange={handleChange}
|
||||||
handleChange(event, "url", setGeneralSettings)
|
|
||||||
}
|
|
||||||
error={errors["url"]}
|
error={errors["url"]}
|
||||||
/>
|
/>
|
||||||
<Field
|
<Field
|
||||||
@@ -196,10 +163,8 @@ const CreateMonitor = () => {
|
|||||||
label="Friendly name"
|
label="Friendly name"
|
||||||
isOptional={true}
|
isOptional={true}
|
||||||
placeholder="Google"
|
placeholder="Google"
|
||||||
value={generalSettings.name}
|
value={monitor.name}
|
||||||
onChange={(event) =>
|
onChange={handleChange}
|
||||||
handleChange(event, "name", setGeneralSettings)
|
|
||||||
}
|
|
||||||
error={errors["name"]}
|
error={errors["name"]}
|
||||||
/>
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
@@ -219,33 +184,27 @@ const CreateMonitor = () => {
|
|||||||
desc="Use HTTP(s) to monitor your website or API endpoint."
|
desc="Use HTTP(s) to monitor your website or API endpoint."
|
||||||
size="small"
|
size="small"
|
||||||
value="http"
|
value="http"
|
||||||
checked={checks.type === "http" || checks.type === "https"}
|
checked={monitor.type === "http"}
|
||||||
onChange={(event) => handleChange(event, "type", setChecks)}
|
onChange={(event) => handleChange(event)}
|
||||||
/>
|
/>
|
||||||
{checks.type === "http" || checks.type === "https" ? (
|
{monitor.type === "http" ? (
|
||||||
<ButtonGroup sx={{ ml: "32px" }}>
|
<ButtonGroup sx={{ ml: "32px" }}>
|
||||||
<Button
|
<Button
|
||||||
level="secondary"
|
level="secondary"
|
||||||
label="HTTP"
|
label="HTTP"
|
||||||
onClick={() =>
|
onClick={() => setHttps(false)}
|
||||||
setChecks((prev) => ({ ...prev, type: "http" }))
|
|
||||||
}
|
|
||||||
sx={{
|
sx={{
|
||||||
backgroundColor:
|
backgroundColor:
|
||||||
checks.type === "http" &&
|
!https && theme.palette.otherColors.fillGray,
|
||||||
theme.palette.otherColors.fillGray,
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<Button
|
<Button
|
||||||
level="secondary"
|
level="secondary"
|
||||||
label="HTTPS"
|
label="HTTPS"
|
||||||
onClick={() =>
|
onClick={() => setHttps(true)}
|
||||||
setChecks((prev) => ({ ...prev, type: "https" }))
|
|
||||||
}
|
|
||||||
sx={{
|
sx={{
|
||||||
backgroundColor:
|
backgroundColor:
|
||||||
checks.type === "https" &&
|
https && theme.palette.otherColors.fillGray,
|
||||||
theme.palette.otherColors.fillGray,
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</ButtonGroup>
|
</ButtonGroup>
|
||||||
@@ -259,8 +218,8 @@ const CreateMonitor = () => {
|
|||||||
desc="Check whether your server is available or not."
|
desc="Check whether your server is available or not."
|
||||||
size="small"
|
size="small"
|
||||||
value="ping"
|
value="ping"
|
||||||
checked={checks.type === "ping"}
|
checked={monitor.type === "ping"}
|
||||||
onChange={(event) => handleChange(event, "type", setChecks)}
|
onChange={(event) => handleChange(event)}
|
||||||
/>
|
/>
|
||||||
{/* TODO */}
|
{/* TODO */}
|
||||||
{/* <RadioButton
|
{/* <RadioButton
|
||||||
@@ -299,36 +258,43 @@ const CreateMonitor = () => {
|
|||||||
</Box>
|
</Box>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|
||||||
<Stack className="config-box">
|
<Stack className="config-box">
|
||||||
<Box>
|
<Box>
|
||||||
<Typography component="h2">Incident notifications</Typography>
|
<Typography component="h2">Incident notifications</Typography>
|
||||||
<Typography component="p" mt={theme.gap.small}>
|
<Typography component="p" mt={theme.gap.small}>
|
||||||
When there is an incident, notify users
|
When there is an incident, notify users.
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
<Stack gap={theme.gap.large}>
|
<Stack gap={theme.gap.medium}>
|
||||||
<Typography component="p" mt={theme.gap.small}>
|
<Typography component="p" mt={theme.gap.small}>
|
||||||
When there is a new incident,
|
When there is a new incident,
|
||||||
</Typography>
|
</Typography>
|
||||||
<Checkbox id="notify-sms" label="Notify via SMS (coming soon)" />
|
<Checkbox
|
||||||
|
id="notify-sms"
|
||||||
|
label="Notify via SMS (coming soon)"
|
||||||
|
isDisabled={true}
|
||||||
|
/>
|
||||||
<Checkbox
|
<Checkbox
|
||||||
id="notify-email-default"
|
id="notify-email-default"
|
||||||
label={`Notify via email (to ${user.email})`}
|
label={`Notify via email (to ${user.email})`}
|
||||||
/>
|
/>
|
||||||
<Checkbox
|
<Checkbox
|
||||||
id="notify-email"
|
id="notify-email"
|
||||||
label="Also notify via email to the following addresses:"
|
label="Also notify via email to multiple addresses (coming soon)"
|
||||||
/>
|
isDisabled={true}
|
||||||
<Field
|
|
||||||
id="notify-email-list"
|
|
||||||
type="text"
|
|
||||||
label="You can separate multiple emails with a comma"
|
|
||||||
placeholder="name@gmail.com"
|
|
||||||
/>
|
/>
|
||||||
|
<Box mx={`calc(${theme.gap.ml} * 2)`}>
|
||||||
|
<Field
|
||||||
|
id="notify-email-list"
|
||||||
|
type="text"
|
||||||
|
placeholder="name@gmail.com"
|
||||||
|
/>
|
||||||
|
<Typography mt={theme.gap.small}>
|
||||||
|
You can separate multiple emails with a comma
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|
||||||
<Stack className="config-box">
|
<Stack className="config-box">
|
||||||
<Box>
|
<Box>
|
||||||
<Typography component="h2">Advanced settings</Typography>
|
<Typography component="h2">Advanced settings</Typography>
|
||||||
@@ -337,10 +303,8 @@ const CreateMonitor = () => {
|
|||||||
<Select
|
<Select
|
||||||
id="monitor-interval"
|
id="monitor-interval"
|
||||||
label="Check frequency"
|
label="Check frequency"
|
||||||
value={advancedSettings.interval || 1}
|
value={monitor.interval || 1}
|
||||||
onChange={(event) =>
|
onChange={(event) => handleChange(event, "interval")}
|
||||||
handleChange(event, "interval", setAdvancedSettings)
|
|
||||||
}
|
|
||||||
items={frequencies}
|
items={frequencies}
|
||||||
/>
|
/>
|
||||||
{/* TODO */}
|
{/* TODO */}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@
|
|||||||
min-width: 100px;
|
min-width: 100px;
|
||||||
border: solid 1px;
|
border: solid 1px;
|
||||||
border-radius: var(--env-var-radius-1);
|
border-radius: var(--env-var-radius-1);
|
||||||
padding: var(--env-var-spacing-1-plus) var(--env-var-spacing-2);
|
padding: var(--env-var-spacing-1) 18px;
|
||||||
background-color: var(--env-var-color-8);
|
background-color: var(--env-var-color-8);
|
||||||
}
|
}
|
||||||
.monitor-details .stat-box:not(:first-of-type) {
|
.monitor-details .stat-box:not(:first-of-type) {
|
||||||
|
|||||||
@@ -111,7 +111,12 @@ const Monitors = () => {
|
|||||||
),
|
),
|
||||||
},
|
},
|
||||||
{ id: idx + 2, data: <ResponseTimeChart checks={reversedChecks} /> },
|
{ id: idx + 2, data: <ResponseTimeChart checks={reversedChecks} /> },
|
||||||
{ id: idx + 3, data: monitor.type },
|
{
|
||||||
|
id: idx + 3,
|
||||||
|
data: (
|
||||||
|
<span style={{ textTransform: "uppercase" }}>{monitor.type}</span>
|
||||||
|
),
|
||||||
|
},
|
||||||
{ id: idx + 4, data: "TODO" },
|
{ id: idx + 4, data: "TODO" },
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
padding-bottom: 60px;
|
padding-bottom: 60px;
|
||||||
border: 1px solid var(--env-var-color-16);
|
border: 1px solid var(--env-var-color-16);
|
||||||
border-radius: var(--env-var-radius-1);
|
border-radius: var(--env-var-radius-1);
|
||||||
|
background-color: var(--env-var-color-8);
|
||||||
}
|
}
|
||||||
.settings .config-box .MuiBox-root,
|
.settings .config-box .MuiBox-root,
|
||||||
.settings .config-box .MuiStack-root {
|
.settings .config-box .MuiStack-root {
|
||||||
@@ -39,4 +40,4 @@
|
|||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: var(--env-var-spacing-4);
|
gap: var(--env-var-spacing-4);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user