Merge pull request #807 from bluewave-labs/feat/http-https-for-pagespeed
Refactor create pagespeed, resolves #796
This commit is contained in:
1 file changed
+274
-178
@@ -1,28 +1,136 @@
|
|||||||
import { Box, Stack, Typography } from "@mui/material";
|
|
||||||
import LoadingButton from "@mui/lab/LoadingButton";
|
|
||||||
|
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
import { Box, Button, ButtonGroup, Stack, Typography } from "@mui/material";
|
||||||
|
import { useSelector, useDispatch } from "react-redux";
|
||||||
|
import { monitorValidation } from "../../../Validation/validation";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import { useDispatch, useSelector } from "react-redux";
|
import { createPageSpeed } from "../../../Features/PageSpeedMonitor/pageSpeedMonitorSlice";
|
||||||
import { useNavigate } from "react-router";
|
import { createToast } from "../../../Utils/toastUtils";
|
||||||
|
import { logger } from "../../../Utils/Logger";
|
||||||
|
import { ConfigBox } from "../../Monitors/styled";
|
||||||
|
import Radio from "../../../Components/Inputs/Radio";
|
||||||
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 { monitorValidation } from "../../../Validation/validation";
|
|
||||||
import { createToast } from "../../../Utils/toastUtils";
|
|
||||||
import { createPageSpeed } from "../../../Features/PageSpeedMonitor/pageSpeedMonitorSlice";
|
|
||||||
import Breadcrumbs from "../../../Components/Breadcrumbs";
|
import Breadcrumbs from "../../../Components/Breadcrumbs";
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
import { logger } from "../../../Utils/Logger";
|
|
||||||
const CreatePageSpeed = () => {
|
|
||||||
const theme = useTheme();
|
|
||||||
const navigate = useNavigate();
|
|
||||||
const dispatch = useDispatch();
|
|
||||||
const { isLoading } = useSelector((state) => state.pageSpeedMonitors);
|
|
||||||
|
|
||||||
|
const CreatePageSpeed = () => {
|
||||||
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 dispatch = useDispatch();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const theme = useTheme();
|
||||||
|
|
||||||
|
const idMap = {
|
||||||
|
"monitor-url": "url",
|
||||||
|
"monitor-name": "name",
|
||||||
|
"monitor-checks-http": "type",
|
||||||
|
"monitor-checks-ping": "type",
|
||||||
|
"notify-email-default": "notification-email",
|
||||||
|
};
|
||||||
|
|
||||||
|
const [monitor, setMonitor] = useState({
|
||||||
|
url: "",
|
||||||
|
name: "",
|
||||||
|
type: "pagespeed",
|
||||||
|
notifications: [],
|
||||||
|
interval: 3,
|
||||||
|
});
|
||||||
|
const [https, setHttps] = useState(true);
|
||||||
|
const [errors, setErrors] = useState({});
|
||||||
|
|
||||||
|
const handleChange = (event, name) => {
|
||||||
|
const { value, id } = event.target;
|
||||||
|
if (!name) name = idMap[id];
|
||||||
|
|
||||||
|
if (name.includes("notification-")) {
|
||||||
|
name = name.replace("notification-", "");
|
||||||
|
let hasNotif = monitor.notifications.some(
|
||||||
|
(notification) => notification.type === name
|
||||||
|
);
|
||||||
|
setMonitor((prev) => {
|
||||||
|
const notifs = [...prev.notifications];
|
||||||
|
if (hasNotif) {
|
||||||
|
return {
|
||||||
|
...prev,
|
||||||
|
notifications: notifs.filter((notif) => notif.type !== name),
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
...prev,
|
||||||
|
notifications: [
|
||||||
|
...notifs,
|
||||||
|
name === "email"
|
||||||
|
? { type: name, address: value }
|
||||||
|
: // TODO - phone number
|
||||||
|
{ type: name, phone: value },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setMonitor((prev) => ({
|
||||||
|
...prev,
|
||||||
|
[name]: value,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const { error } = monitorValidation.validate(
|
||||||
|
{ [name]: value },
|
||||||
|
{ abortEarly: false }
|
||||||
|
);
|
||||||
|
|
||||||
|
setErrors((prev) => {
|
||||||
|
const updatedErrors = { ...prev };
|
||||||
|
if (error) updatedErrors[name] = error.details[0].message;
|
||||||
|
else delete updatedErrors[name];
|
||||||
|
return updatedErrors;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleCreateMonitor = async (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
//obj to submit
|
||||||
|
let form = {
|
||||||
|
url: `http${https ? "s" : ""}://` + monitor.url,
|
||||||
|
name: monitor.name === "" ? monitor.url : monitor.name,
|
||||||
|
type: monitor.type,
|
||||||
|
interval: monitor.interval * MS_PER_MINUTE,
|
||||||
|
};
|
||||||
|
|
||||||
|
const { error } = monitorValidation.validate(form, {
|
||||||
|
abortEarly: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
const newErrors = {};
|
||||||
|
error.details.forEach((err) => {
|
||||||
|
newErrors[err.path[0]] = err.message;
|
||||||
|
});
|
||||||
|
setErrors(newErrors);
|
||||||
|
createToast({ body: "Error validation data." });
|
||||||
|
} else {
|
||||||
|
form = {
|
||||||
|
...form,
|
||||||
|
description: form.name,
|
||||||
|
teamId: user.teamId,
|
||||||
|
userId: user._id,
|
||||||
|
notifications: monitor.notifications,
|
||||||
|
};
|
||||||
|
const action = await dispatch(
|
||||||
|
createPageSpeed({ authToken, monitor: form })
|
||||||
|
);
|
||||||
|
if (action.meta.requestStatus === "fulfilled") {
|
||||||
|
createToast({ body: "Monitor created successfully!" });
|
||||||
|
navigate("/pagespeed");
|
||||||
|
} else {
|
||||||
|
createToast({ body: "Failed to create monitor." });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//select values
|
||||||
const frequencies = [
|
const frequencies = [
|
||||||
{ _id: 3, name: "3 minutes" },
|
{ _id: 3, name: "3 minutes" },
|
||||||
{ _id: 5, name: "5 minutes" },
|
{ _id: 5, name: "5 minutes" },
|
||||||
@@ -32,75 +140,15 @@ const CreatePageSpeed = () => {
|
|||||||
{ _id: 1440, name: "1 day" },
|
{ _id: 1440, name: "1 day" },
|
||||||
{ _id: 10080, name: "1 week" },
|
{ _id: 10080, name: "1 week" },
|
||||||
];
|
];
|
||||||
|
|
||||||
const [form, setForm] = useState({
|
|
||||||
name: "",
|
|
||||||
url: "",
|
|
||||||
interval: 3,
|
|
||||||
});
|
|
||||||
const [errors, setErrors] = useState({});
|
|
||||||
|
|
||||||
const handleChange = (event, id) => {
|
|
||||||
const { value } = event.target;
|
|
||||||
setForm((prev) => ({ ...prev, [id]: value }));
|
|
||||||
|
|
||||||
const { error } = monitorValidation.validate(
|
|
||||||
{ [id]: value },
|
|
||||||
{ abortEarly: false }
|
|
||||||
);
|
|
||||||
|
|
||||||
setErrors((prev) => {
|
|
||||||
const newErrors = { ...prev };
|
|
||||||
if (error) newErrors[id] = error.details[0].message;
|
|
||||||
else delete newErrors[id];
|
|
||||||
return newErrors;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleCreate = async (event) => {
|
|
||||||
event.preventDefault();
|
|
||||||
|
|
||||||
let monitor = {
|
|
||||||
url: "http://" + form.url,
|
|
||||||
name: form.name === "" ? form.url : form.name,
|
|
||||||
};
|
|
||||||
|
|
||||||
const { error } = monitorValidation.validate(form, { abortEarly: false });
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
const newErrors = {};
|
|
||||||
error.details.forEach((err) => {
|
|
||||||
newErrors[err.path[0]] = err.message;
|
|
||||||
});
|
|
||||||
setErrors(newErrors);
|
|
||||||
createToast({ body: "Error validating data." });
|
|
||||||
} else {
|
|
||||||
monitor = {
|
|
||||||
...monitor,
|
|
||||||
description: monitor.name,
|
|
||||||
userId: user._id,
|
|
||||||
teamId: user.teamId,
|
|
||||||
interval: form.interval * MS_PER_MINUTE,
|
|
||||||
type: "pagespeed",
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
const action = await dispatch(createPageSpeed({ authToken, monitor }));
|
|
||||||
if (action.meta.requestStatus === "fulfilled") {
|
|
||||||
navigate("/pagespeed");
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
createToast({
|
|
||||||
body:
|
|
||||||
error.details && error.details.length > 0
|
|
||||||
? error.details[0].message
|
|
||||||
: "Unknown error.",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack className="create-page-speed" gap={theme.spacing(6)}>
|
<Box
|
||||||
|
className="create-monitor"
|
||||||
|
sx={{
|
||||||
|
"& h1": {
|
||||||
|
color: theme.palette.text.primary,
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
>
|
||||||
<Breadcrumbs
|
<Breadcrumbs
|
||||||
list={[
|
list={[
|
||||||
{ name: "pagespeed", path: "/pagespeed" },
|
{ name: "pagespeed", path: "/pagespeed" },
|
||||||
@@ -109,18 +157,14 @@ const CreatePageSpeed = () => {
|
|||||||
/>
|
/>
|
||||||
<Stack
|
<Stack
|
||||||
component="form"
|
component="form"
|
||||||
|
className="create-monitor-form"
|
||||||
|
onSubmit={handleCreateMonitor}
|
||||||
noValidate
|
noValidate
|
||||||
spellCheck="false"
|
spellCheck="false"
|
||||||
onSubmit={handleCreate}
|
|
||||||
gap={theme.spacing(12)}
|
gap={theme.spacing(12)}
|
||||||
flex={1}
|
mt={theme.spacing(6)}
|
||||||
>
|
>
|
||||||
<Typography
|
<Typography component="h1">
|
||||||
component="h1"
|
|
||||||
lineHeight={1}
|
|
||||||
fontSize={21}
|
|
||||||
color={theme.palette.text.primary}
|
|
||||||
>
|
|
||||||
<Typography
|
<Typography
|
||||||
component="span"
|
component="span"
|
||||||
fontSize="inherit"
|
fontSize="inherit"
|
||||||
@@ -129,118 +173,170 @@ const CreatePageSpeed = () => {
|
|||||||
Create your{" "}
|
Create your{" "}
|
||||||
</Typography>
|
</Typography>
|
||||||
<Typography component="span" fontSize="inherit" fontWeight="inherit">
|
<Typography component="span" fontSize="inherit" fontWeight="inherit">
|
||||||
pagespeed{" "}
|
Pagespeed monitor
|
||||||
</Typography>
|
|
||||||
<Typography
|
|
||||||
component="span"
|
|
||||||
fontSize="inherit"
|
|
||||||
color={theme.palette.text.secondary}
|
|
||||||
>
|
|
||||||
monitor
|
|
||||||
</Typography>
|
</Typography>
|
||||||
</Typography>
|
</Typography>
|
||||||
<Stack
|
<ConfigBox>
|
||||||
border={1}
|
<Box>
|
||||||
borderColor={theme.palette.border.light}
|
<Typography component="h2">General settings</Typography>
|
||||||
borderRadius={theme.shape.borderRadius}
|
<Typography component="p">
|
||||||
backgroundColor={theme.palette.background.main}
|
Here you can select the URL of the host, together with the type of
|
||||||
p={theme.spacing(20)}
|
monitor.
|
||||||
pl={theme.spacing(15)}
|
</Typography>
|
||||||
gap={theme.spacing(20)}
|
</Box>
|
||||||
sx={{
|
<Stack gap={theme.spacing(15)}>
|
||||||
"& h3, & p": {
|
<Field
|
||||||
color: theme.palette.text.secondary,
|
type={"url"}
|
||||||
},
|
id="monitor-url"
|
||||||
}}
|
label="URL to monitor"
|
||||||
>
|
https={https}
|
||||||
<Stack direction="row">
|
placeholder="google.com"
|
||||||
<Typography component="h3">Monitor display name</Typography>
|
value={monitor.url}
|
||||||
|
onChange={handleChange}
|
||||||
|
error={errors["url"]}
|
||||||
|
/>
|
||||||
<Field
|
<Field
|
||||||
type="text"
|
type="text"
|
||||||
id="monitor-name"
|
id="monitor-name"
|
||||||
placeholder="Example monitor"
|
label="Display name"
|
||||||
value={form.name}
|
isOptional={true}
|
||||||
onChange={(event) => handleChange(event, "name")}
|
placeholder="Google"
|
||||||
error={errors.name}
|
value={monitor.name}
|
||||||
|
onChange={handleChange}
|
||||||
|
error={errors["name"]}
|
||||||
/>
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
<Stack direction="row">
|
</ConfigBox>
|
||||||
<Typography component="h3">URL</Typography>
|
<ConfigBox>
|
||||||
<Field
|
<Box>
|
||||||
type="url"
|
<Typography component="h2">Checks to perform</Typography>
|
||||||
id="monitor-url"
|
<Typography component="p">
|
||||||
placeholder="random.website.com"
|
You can always add or remove checks after adding your site.
|
||||||
value={form.url}
|
|
||||||
onChange={(event) => handleChange(event, "url")}
|
|
||||||
error={errors.url}
|
|
||||||
/>
|
|
||||||
</Stack>
|
|
||||||
<Stack direction="row">
|
|
||||||
<Typography component="h3">Check frequency</Typography>
|
|
||||||
<Select
|
|
||||||
id="monitor-frequency"
|
|
||||||
items={frequencies}
|
|
||||||
value={form.interval}
|
|
||||||
onChange={(event) => handleChange(event, "interval")}
|
|
||||||
/>
|
|
||||||
</Stack>
|
|
||||||
<Stack direction="row">
|
|
||||||
<Typography component="h3">
|
|
||||||
Incidents notifications{" "}
|
|
||||||
<Typography component="span">(coming soon)</Typography>
|
|
||||||
</Typography>
|
</Typography>
|
||||||
<Stack
|
</Box>
|
||||||
className="section-disabled"
|
<Stack gap={theme.spacing(12)}>
|
||||||
backgroundColor={theme.palette.background.fill}
|
<Stack gap={theme.spacing(6)}>
|
||||||
>
|
<Radio
|
||||||
<Typography mb={theme.spacing(4)}>
|
id="monitor-checks-http"
|
||||||
When there is a new incident,
|
title="Website monitoring"
|
||||||
</Typography>
|
desc="Use HTTP(s) to monitor your website or API endpoint."
|
||||||
<Checkbox
|
size="small"
|
||||||
id="notify-sms"
|
value="http"
|
||||||
label="Notify via SMS (coming soon)"
|
checked={monitor.type === "pagespeed"}
|
||||||
isChecked={false}
|
onChange={(event) => handleChange(event)}
|
||||||
isDisabled={true}
|
|
||||||
/>
|
|
||||||
<Checkbox
|
|
||||||
id="notify-email"
|
|
||||||
label="Notify via email (to gorkem.cetin@bluewavelabs.ca)"
|
|
||||||
isChecked={false}
|
|
||||||
/>
|
|
||||||
<Checkbox
|
|
||||||
id="notify-emails"
|
|
||||||
label="Notify via email to following emails"
|
|
||||||
isChecked={false}
|
|
||||||
/>
|
/>
|
||||||
|
<ButtonGroup sx={{ ml: "32px" }}>
|
||||||
|
<Button
|
||||||
|
variant="group"
|
||||||
|
filled={https.toString()}
|
||||||
|
onClick={() => setHttps(true)}
|
||||||
|
>
|
||||||
|
HTTPS
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="group"
|
||||||
|
filled={(!https).toString()}
|
||||||
|
onClick={() => setHttps(false)}
|
||||||
|
>
|
||||||
|
HTTP
|
||||||
|
</Button>
|
||||||
|
</ButtonGroup>
|
||||||
|
</Stack>
|
||||||
|
{errors["type"] ? (
|
||||||
|
<Box className="error-container">
|
||||||
|
<Typography
|
||||||
|
component="p"
|
||||||
|
className="input-error"
|
||||||
|
color={theme.palette.error.text}
|
||||||
|
>
|
||||||
|
{errors["type"]}
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
) : (
|
||||||
|
""
|
||||||
|
)}
|
||||||
|
</Stack>
|
||||||
|
</ConfigBox>
|
||||||
|
<ConfigBox>
|
||||||
|
<Box>
|
||||||
|
<Typography component="h2">Incident notifications</Typography>
|
||||||
|
<Typography component="p">
|
||||||
|
When there is an incident, notify users.
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
<Stack gap={theme.spacing(6)}>
|
||||||
|
<Typography component="p">When there is a new incident,</Typography>
|
||||||
|
<Checkbox
|
||||||
|
id="notify-sms"
|
||||||
|
label="Notify via SMS (coming soon)"
|
||||||
|
isChecked={false}
|
||||||
|
value=""
|
||||||
|
onChange={() => logger.warn("disabled")}
|
||||||
|
isDisabled={true}
|
||||||
|
/>
|
||||||
|
<Checkbox
|
||||||
|
id="notify-email-default"
|
||||||
|
label={`Notify via email (to ${user.email})`}
|
||||||
|
isChecked={monitor.notifications.some(
|
||||||
|
(notification) => notification.type === "email"
|
||||||
|
)}
|
||||||
|
value={user?.email}
|
||||||
|
onChange={(event) => handleChange(event)}
|
||||||
|
/>
|
||||||
|
<Checkbox
|
||||||
|
id="notify-email"
|
||||||
|
label="Also notify via email to multiple addresses (coming soon)"
|
||||||
|
isChecked={false}
|
||||||
|
value=""
|
||||||
|
onChange={() => logger.warn("disabled")}
|
||||||
|
isDisabled={true}
|
||||||
|
/>
|
||||||
|
{monitor.notifications.some(
|
||||||
|
(notification) => notification.type === "emails"
|
||||||
|
) ? (
|
||||||
<Box mx={theme.spacing(16)}>
|
<Box mx={theme.spacing(16)}>
|
||||||
<Field
|
<Field
|
||||||
id="notify-emails-list"
|
id="notify-email-list"
|
||||||
placeholder="notifications@gmail.com"
|
type="text"
|
||||||
|
placeholder="name@gmail.com"
|
||||||
value=""
|
value=""
|
||||||
onChange={() => logger.warn("disabled")}
|
onChange={() => logger.warn("disabled")}
|
||||||
error=""
|
|
||||||
/>
|
/>
|
||||||
<Typography mt={theme.spacing(4)}>
|
<Typography mt={theme.spacing(4)}>
|
||||||
You can separate multiple emails with a comma
|
You can separate multiple emails with a comma
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
</Stack>
|
) : (
|
||||||
|
""
|
||||||
|
)}
|
||||||
</Stack>
|
</Stack>
|
||||||
</Stack>
|
</ConfigBox>
|
||||||
<Stack direction="row" justifyContent="flex-end" mt="auto">
|
<ConfigBox>
|
||||||
<LoadingButton
|
<Box>
|
||||||
loading={isLoading}
|
<Typography component="h2">Advanced settings</Typography>
|
||||||
type="submit"
|
</Box>
|
||||||
|
<Stack gap={theme.spacing(12)}>
|
||||||
|
<Select
|
||||||
|
id="monitor-interval"
|
||||||
|
label="Check frequency"
|
||||||
|
value={monitor.interval || 3}
|
||||||
|
onChange={(event) => handleChange(event, "interval")}
|
||||||
|
items={frequencies}
|
||||||
|
/>
|
||||||
|
</Stack>
|
||||||
|
</ConfigBox>
|
||||||
|
<Stack direction="row" justifyContent="flex-end">
|
||||||
|
<Button
|
||||||
variant="contained"
|
variant="contained"
|
||||||
color="primary"
|
color="primary"
|
||||||
onClick={handleCreate}
|
onClick={handleCreateMonitor}
|
||||||
sx={{ px: theme.spacing(12), mt: theme.spacing(12) }}
|
disabled={Object.keys(errors).length !== 0 && true}
|
||||||
>
|
>
|
||||||
Create
|
Create monitor
|
||||||
</LoadingButton>
|
</Button>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Stack>
|
</Box>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user