remove old status page
This commit is contained in:
1 parent
d853c50840
commit
d38b51e1ca
1 file changed
+80
-46
@@ -1,21 +1,29 @@
|
|||||||
// Components
|
// Components
|
||||||
import { Stack, Tab, Button } from "@mui/material";
|
import { Stack, Button, Typography } from "@mui/material";
|
||||||
import { TabContext, TabList } from "@mui/lab";
|
import Tabs from "./Components/Tabs";
|
||||||
import Settings from "./Components/Tabs/Settings";
|
import GenericFallback from "../../../Components/GenericFallback";
|
||||||
import Content from "./Components/Tabs/Content";
|
import SkeletonLayout from "./Components/Skeleton";
|
||||||
|
|
||||||
//Utils
|
//Utils
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import { useState, useRef } from "react";
|
import { useState, useRef } from "react";
|
||||||
import { publicPageSettingsValidation } from "../../../Validation/validation";
|
import { statusPageValidation } from "../../../Validation/validation";
|
||||||
import { buildErrors } from "../../../Validation/error";
|
import { buildErrors } from "../../../Validation/error";
|
||||||
|
import { useSelector } from "react-redux";
|
||||||
|
import { useMonitorsFetch } from "./Hooks/useMonitorsFetch";
|
||||||
|
import { useCreateStatusPage } from "./Hooks/useCreateStatusPage";
|
||||||
|
import { createToast } from "../../../Utils/toastUtils";
|
||||||
|
|
||||||
//Constants
|
//Constants
|
||||||
const TAB_LIST = ["General settings", "Contents"];
|
const TAB_LIST = ["General settings", "Contents"];
|
||||||
|
|
||||||
|
const ERROR_TAB_MAPPING = [
|
||||||
|
["companyName", "url", "timezone", "color", "isPublished", "logo"],
|
||||||
|
["monitors", "showUptimePercentage", "showCharts"],
|
||||||
|
];
|
||||||
|
|
||||||
const CreateStatusPage = () => {
|
const CreateStatusPage = () => {
|
||||||
//Redux state
|
//Redux state
|
||||||
|
const { authToken, user } = useSelector((state) => state.auth);
|
||||||
//Local state
|
//Local state
|
||||||
const [tab, setTab] = useState(0);
|
const [tab, setTab] = useState(0);
|
||||||
const [progress, setProgress] = useState({ value: 0, isLoading: false });
|
const [progress, setProgress] = useState({ value: 0, isLoading: false });
|
||||||
@@ -26,21 +34,27 @@ const CreateStatusPage = () => {
|
|||||||
logo: null,
|
logo: null,
|
||||||
timezone: "America/Toronto",
|
timezone: "America/Toronto",
|
||||||
color: "#4169E1",
|
color: "#4169E1",
|
||||||
|
monitors: [],
|
||||||
|
showCharts: true,
|
||||||
|
showUptimePercentage: true,
|
||||||
});
|
});
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
|
const [selectedMonitors, setSelectedMonitors] = useState([]);
|
||||||
|
|
||||||
// Refs
|
// Refs
|
||||||
const intervalRef = useRef(null);
|
const intervalRef = useRef(null);
|
||||||
|
|
||||||
//Utils
|
//Utils
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
const [monitors, isLoading, networkError] = useMonitorsFetch();
|
||||||
|
const [createStatusPage, createSatusIsLoading, createStatusPageNetworkError] =
|
||||||
|
useCreateStatusPage();
|
||||||
|
|
||||||
// Handlers
|
// Handlers
|
||||||
const handleFormChange = (e) => {
|
const handleFormChange = (e) => {
|
||||||
let { type, name, value, checked } = e.target;
|
let { type, name, value, checked } = e.target;
|
||||||
|
|
||||||
// Handle errors
|
// Handle errors
|
||||||
const { error } = publicPageSettingsValidation.validate(
|
const { error } = statusPageValidation.validate(
|
||||||
{ [name]: value },
|
{ [name]: value },
|
||||||
{ abortEarly: false }
|
{ abortEarly: false }
|
||||||
);
|
);
|
||||||
@@ -98,54 +112,74 @@ const CreateStatusPage = () => {
|
|||||||
setProgress({ value: 0, isLoading: false });
|
setProgress({ value: 0, isLoading: false });
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = () => {
|
const handleSubmit = async () => {
|
||||||
let toSubmit = {
|
let toSubmit = {
|
||||||
...form,
|
...form,
|
||||||
logo: { type: form.logo?.type ?? null, size: form.logo?.size ?? null },
|
logo: { type: form.logo?.type ?? null, size: form.logo?.size ?? null },
|
||||||
};
|
};
|
||||||
const { error } = publicPageSettingsValidation.validate(toSubmit, {
|
const { error } = statusPageValidation.validate(toSubmit, {
|
||||||
abortEarly: false,
|
abortEarly: false,
|
||||||
});
|
});
|
||||||
console.log(error);
|
|
||||||
setErrors((prev) => {
|
if (typeof error === "undefined") {
|
||||||
return buildErrors(prev, name, error);
|
const success = await createStatusPage({ form });
|
||||||
|
if (success) {
|
||||||
|
createToast({ body: "Status page created successfully" });
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const newErrors = {};
|
||||||
|
error?.details?.forEach((err) => {
|
||||||
|
newErrors[err.path[0]] = err.message;
|
||||||
});
|
});
|
||||||
|
setErrors((prev) => ({ ...prev, ...newErrors }));
|
||||||
|
|
||||||
|
const errorTabs = Object.keys(newErrors).map((err) => {
|
||||||
|
return ERROR_TAB_MAPPING.findIndex((tab) => tab.includes(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
// If there's an error in the current tab, don't change the tab
|
||||||
|
if (errorTabs.some((errorTab) => errorTab === tab)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Otherwise go to tab with error
|
||||||
|
setTab(errorTabs[0]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (networkError === true) {
|
||||||
|
return (
|
||||||
|
<GenericFallback>
|
||||||
|
<Typography
|
||||||
|
variant="h1"
|
||||||
|
marginY={theme.spacing(4)}
|
||||||
|
color={theme.palette.primary.contrastTextTertiary}
|
||||||
|
>
|
||||||
|
Network error
|
||||||
|
</Typography>
|
||||||
|
<Typography>Please check your connection</Typography>
|
||||||
|
</GenericFallback>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isLoading) return <SkeletonLayout />;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack gap={theme.spacing(10)}>
|
<Stack gap={theme.spacing(10)}>
|
||||||
<TabContext value={TAB_LIST[tab]}>
|
<Tabs
|
||||||
<TabList
|
form={form}
|
||||||
onChange={(_, tab) => {
|
errors={errors}
|
||||||
setTab(TAB_LIST.indexOf(tab));
|
monitors={monitors}
|
||||||
}}
|
selectedMonitors={selectedMonitors}
|
||||||
>
|
setSelectedMonitors={setSelectedMonitors}
|
||||||
{TAB_LIST.map((tab, idx) => (
|
handleFormChange={handleFormChange}
|
||||||
<Tab
|
handleImageChange={handleImageChange}
|
||||||
key={Math.random()}
|
progress={progress}
|
||||||
label={TAB_LIST[idx]}
|
removeLogo={removeLogo}
|
||||||
value={TAB_LIST[idx]}
|
tab={tab}
|
||||||
/>
|
setTab={setTab}
|
||||||
))}
|
TAB_LIST={TAB_LIST}
|
||||||
</TabList>
|
/>
|
||||||
{tab === 0 ? (
|
|
||||||
<Settings
|
|
||||||
tabValue={TAB_LIST[0]}
|
|
||||||
form={form}
|
|
||||||
handleFormChange={handleFormChange}
|
|
||||||
handleImageChange={handleImageChange}
|
|
||||||
progress={progress}
|
|
||||||
removeLogo={removeLogo}
|
|
||||||
errors={errors}
|
|
||||||
/>
|
|
||||||
) : (
|
|
||||||
<Content
|
|
||||||
tabValue={TAB_LIST[1]}
|
|
||||||
form={form}
|
|
||||||
handleFormChange={handleFormChange}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</TabContext>
|
|
||||||
<Stack
|
<Stack
|
||||||
direction="row"
|
direction="row"
|
||||||
justifyContent="flex-end"
|
justifyContent="flex-end"
|
||||||
|
|||||||
Reference in new issue
Block a user