- Code clean up
This commit is contained in:
1 parent
3a00742d06
commit
11154ad9ac
4 files changed
+83
-51
No files matched your search
@@ -4,50 +4,70 @@ import { Box, Tab, useTheme, Stack, Button } from "@mui/material";
|
|||||||
import TabContext from "@mui/lab/TabContext";
|
import TabContext from "@mui/lab/TabContext";
|
||||||
import TabList from "@mui/lab/TabList";
|
import TabList from "@mui/lab/TabList";
|
||||||
import GeneralSettingsPanel from "../../../Components/TabPanels/Status/GeneralSettingsPanel";
|
import GeneralSettingsPanel from "../../../Components/TabPanels/Status/GeneralSettingsPanel";
|
||||||
import { capitalizeFirstLetter } from "../../../Utils/stringUtils";
|
|
||||||
import ContentPanel from "../../../Components/TabPanels/Status/ContentPanel";
|
import ContentPanel from "../../../Components/TabPanels/Status/ContentPanel";
|
||||||
import { publicPageGeneralSettingsValidation } from "../../../Validation/validation";
|
import { publicPageGeneralSettingsValidation } from "../../../Validation/validation";
|
||||||
import { hasValidationErrors } from "../../../Validation/error";
|
import { hasValidationErrors } from "../../../Validation/error";
|
||||||
import { StatusFormProvider } from "../CreateStatusContext";
|
import { StatusFormProvider } from "../CreateStatusContext";
|
||||||
|
import { formatBytes } from "../../../Utils/fileUtils";
|
||||||
|
import { useSelector } from "react-redux";
|
||||||
|
import { createToast } from "../../../Utils/toastUtils";
|
||||||
|
import { networkService } from "../../../main";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CreateStatus page renders a page with tabs for general settings and contents.
|
* CreateStatus page renders a page with tabs for general settings and contents.
|
||||||
* @param {string} [props.open] - Specifies the initially open tab: 'general settings' or 'content'.
|
* @param {object} [props.initForm] - Specifies the initial form value if the status page exists
|
||||||
* @returns {JSX.Element}
|
* @returns {JSX.Element}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const CreateStatus = ({ open = "general-settings", initForm }) => {
|
const CreateStatus = ({ initForm }) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
let tabList = ["General Settings", "Contents"];
|
const {authToken} = useSelector((state) => state.auth);
|
||||||
|
const {apiBaseUrl} = useSelector((state) => state.settings);
|
||||||
|
const [tabIdx, setTabIdx] = useState(0);
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
const error_tab_maping = [["companyName","url","timezone","color","publish","logo"],["monitors",
|
const error_tab_maping = [["companyName","url","timezone","color","publish","logo"],["monitors",
|
||||||
"showUptimePercentage", "showBarcode"]]
|
"showUptimePercentage", "showBarcode"]]
|
||||||
const [form, setForm] = useState({
|
const tabList = ["General Settings", "Contents"];
|
||||||
companyName: "",
|
const hasInitForm = initForm && Object.keys(initForm).length > 0;
|
||||||
url: "",
|
const [form, setForm] = useState(
|
||||||
timezone: "America/Toronto",
|
hasInitForm
|
||||||
color: "#4169E1",
|
? initForm
|
||||||
//which fields matching below?
|
: {
|
||||||
publish: false,
|
companyName: "",
|
||||||
logo: null,
|
url: "",
|
||||||
monitors: [],
|
timezone: "America/Toronto",
|
||||||
showUptimePercentage: false,
|
color: "#4169E1",
|
||||||
showBarcode: false,
|
//which fields matching below?
|
||||||
});
|
//publish: false,
|
||||||
const tab = open
|
logo: null,
|
||||||
.split("-")
|
monitors: [],
|
||||||
.map((a) => capitalizeFirstLetter(a))
|
// showUptimePercentage: false,
|
||||||
.join(" ");
|
// showBarcode: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const [tabIdx, setTabIdx] = useState(tabList.indexOf(tab));
|
// switch to the respective tab when there is error on it
|
||||||
|
useEffect(() => {
|
||||||
const handleTabChange = (event, newTab) => {
|
let newIdx = -1;
|
||||||
|
Object.keys(errors).map(id=>{
|
||||||
|
error_tab_maping.map((val, idx) => {
|
||||||
|
let anyMatch = val.some(vl=> vl==id )
|
||||||
|
if(anyMatch){
|
||||||
|
newIdx = idx;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
if (newIdx !== -1) setTabIdx(newIdx);
|
||||||
|
}, [errors]);
|
||||||
|
|
||||||
|
const handleTabChange = (_, newTab) => {
|
||||||
setTabIdx(tabList.indexOf(newTab));
|
setTabIdx(tabList.indexOf(newTab));
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = () => {
|
const handleSubmit = async () => {
|
||||||
//validate rest of the form
|
//validate rest of the form
|
||||||
delete form.logo;
|
delete form.logo;
|
||||||
|
|
||||||
if (hasValidationErrors(form, publicPageGeneralSettingsValidation, setErrors)) {
|
if (hasValidationErrors(form, publicPageGeneralSettingsValidation, setErrors)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -57,27 +77,21 @@ const CreateStatus = ({ open = "general-settings", initForm }) => {
|
|||||||
// logoImageValidation
|
// logoImageValidation
|
||||||
// );
|
// );
|
||||||
// if (error) return;
|
// if (error) return;
|
||||||
form.logo = { ...logo, size: formatBytes(logo?.size) };
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
//form.logo = { ...logo, size: formatBytes(logo?.size) };
|
||||||
let newIdx = -1;
|
let config = { authToken: authToken, url: apiBaseUrl, data: form };
|
||||||
Object.keys(errors).map(id=>{
|
try{
|
||||||
console.log("errors id")
|
const res = await networkService.createStatusPage(config)
|
||||||
console.log(id)
|
if (!res.status === 200) {
|
||||||
error_tab_maping.map((val, idx) => {
|
throw new Error("Failed to create status page");
|
||||||
let anyMatch = val.some(vl=> vl==id )
|
}
|
||||||
if(anyMatch){
|
createToast({ body: "Status page created successfully!" });
|
||||||
newIdx = idx;
|
}
|
||||||
}
|
catch(e){
|
||||||
})
|
createToast({ body: e.message || "Error creating status page" });
|
||||||
})
|
}
|
||||||
console.log("newIdx")
|
|
||||||
console.log(newIdx)
|
};
|
||||||
if(newIdx!==-1)
|
|
||||||
setTabIdx(newIdx)
|
|
||||||
setTabIdx(tabList.indexOf(tab));
|
|
||||||
}, [tab,errors]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
@@ -125,7 +139,7 @@ const CreateStatus = ({ open = "general-settings", initForm }) => {
|
|||||||
</TabList>
|
</TabList>
|
||||||
</Box>
|
</Box>
|
||||||
<StatusFormProvider
|
<StatusFormProvider
|
||||||
form={initForm && Object.keys(initForm) > 0 ? initForm : form}
|
form={hasInitForm ? initForm : form}
|
||||||
setForm={setForm}
|
setForm={setForm}
|
||||||
errors={errors}
|
errors={errors}
|
||||||
setErrors={setErrors}
|
setErrors={setErrors}
|
||||||
@@ -150,7 +164,7 @@ const CreateStatus = ({ open = "general-settings", initForm }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
CreateStatus.propTypes = {
|
CreateStatus.propTypes = {
|
||||||
open: PropTypes.oneOf(["general-settings", "contents"]),
|
initForm: PropTypes.object
|
||||||
};
|
};
|
||||||
|
|
||||||
export default CreateStatus;
|
export default CreateStatus;
|
||||||
@@ -3,7 +3,7 @@ import Fallback from "../../Components/Fallback";
|
|||||||
import { Box, Button, Stack } from "@mui/material";
|
import { Box, Button, Stack } from "@mui/material";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import CreateStatus from "./CreateStatus";
|
import CreateStatus from "./CreateStatus";
|
||||||
import { useEffect } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { networkService } from "../../main";
|
import { networkService } from "../../main";
|
||||||
import { useSelector } from "react-redux";
|
import { useSelector } from "react-redux";
|
||||||
|
|
||||||
@@ -12,12 +12,14 @@ const Status = () => {
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const {authToken} = useSelector((state) => state.auth);
|
const {authToken} = useSelector((state) => state.auth);
|
||||||
const {apiBaseUrl} = useSelector((state) => state.settings);
|
const {apiBaseUrl} = useSelector((state) => state.settings);
|
||||||
let initForm = {}
|
const [initForm, setInitForm] = useState({});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const getStatusPage = async () => {
|
const getStatusPage = async () => {
|
||||||
let config = { authToken: authToken, url: apiBaseUrl };
|
let config = { authToken: authToken, url: apiBaseUrl };
|
||||||
initForm = await networkService.getStatusPageByUrl(config);
|
let res = await networkService.getStatusPageByUrl(config);
|
||||||
|
if(res && res.data)
|
||||||
|
setInitForm( res.data.data)
|
||||||
};
|
};
|
||||||
getStatusPage();
|
getStatusPage();
|
||||||
}, []);
|
}, []);
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import CreateNewMaintenanceWindow from "../Pages/Maintenance/CreateMaintenance";
|
|||||||
import PageSpeedDetails from "../Pages/PageSpeed/Details";
|
import PageSpeedDetails from "../Pages/PageSpeed/Details";
|
||||||
import PageSpeedConfigure from "../Pages/PageSpeed/Configure";
|
import PageSpeedConfigure from "../Pages/PageSpeed/Configure";
|
||||||
import withAdminCheck from "../Components/HOC/withAdminCheck";
|
import withAdminCheck from "../Components/HOC/withAdminCheck";
|
||||||
|
import CreateStatus from "../Pages/Status/CreateStatus";
|
||||||
|
|
||||||
const Routes = () => {
|
const Routes = () => {
|
||||||
const AdminCheckedRegister = withAdminCheck(Register);
|
const AdminCheckedRegister = withAdminCheck(Register);
|
||||||
@@ -98,6 +99,11 @@ const Routes = () => {
|
|||||||
path="status"
|
path="status"
|
||||||
element={<Status />}
|
element={<Status />}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<Route
|
||||||
|
path="status/create"
|
||||||
|
element={<CreateStatus/>}
|
||||||
|
/>
|
||||||
<Route
|
<Route
|
||||||
path="integrations"
|
path="integrations"
|
||||||
element={<Integrations />}
|
element={<Integrations />}
|
||||||
|
|||||||
@@ -154,7 +154,17 @@ const publicPageGeneralSettingsValidation = joi.object({
|
|||||||
url: joi.string().trim().messages({ "string.empty": "URL is required." }),
|
url: joi.string().trim().messages({ "string.empty": "URL is required." }),
|
||||||
timezone: joi.string().trim().messages({ "string.empty": "Timezone is required." }),
|
timezone: joi.string().trim().messages({ "string.empty": "Timezone is required." }),
|
||||||
color: joi.string().trim().messages({ "string.empty": "Color is required." }),
|
color: joi.string().trim().messages({ "string.empty": "Color is required." }),
|
||||||
publish: joi.bool()
|
publish: joi.bool(),
|
||||||
|
monitors: joi
|
||||||
|
.array()
|
||||||
|
.items(joi.string().pattern(/^[0-9a-fA-F]{24}$/))
|
||||||
|
.required()
|
||||||
|
.messages({
|
||||||
|
"string.pattern.base": "Must be a valid monitor ID",
|
||||||
|
"array.base": "Monitors must be an array",
|
||||||
|
"array.empty": "At least one monitor is required",
|
||||||
|
"any.required": "Monitors are required",
|
||||||
|
}),
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user