2024-11-26 16:10:55 -05:00
|
|
|
import PropTypes from "prop-types";
|
2024-12-04 15:10:13 -05:00
|
|
|
import { useEffect, useState } from "react";
|
2024-12-13 15:59:54 -05:00
|
|
|
import { Box, Tab, useTheme, Stack, Button } from "@mui/material";
|
2024-11-26 16:10:55 -05:00
|
|
|
import TabContext from "@mui/lab/TabContext";
|
|
|
|
|
import TabList from "@mui/lab/TabList";
|
|
|
|
|
import GeneralSettingsPanel from "../../../Components/TabPanels/Status/GeneralSettingsPanel";
|
2024-12-16 20:18:05 -05:00
|
|
|
import { capitalizeFirstLetter } from "../../../Utils/stringUtils";
|
2024-12-04 15:10:13 -05:00
|
|
|
import ContentPanel from "../../../Components/TabPanels/Status/ContentPanel";
|
2024-12-16 20:18:05 -05:00
|
|
|
import { publicPageGeneralSettingsValidation } from "../../../Validation/validation";
|
2024-12-13 15:59:54 -05:00
|
|
|
import { hasValidationErrors } from "../../../Validation/error";
|
2024-12-16 20:18:05 -05:00
|
|
|
import { StatusFormProvider } from "../TabContext";
|
2024-11-26 16:10:55 -05:00
|
|
|
/**
|
|
|
|
|
* 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'.
|
|
|
|
|
* @returns {JSX.Element}
|
|
|
|
|
*/
|
|
|
|
|
|
2024-12-16 20:18:05 -05:00
|
|
|
const CreateStatus = ({ open = "general-settings", initForm }) => {
|
2024-11-26 16:10:55 -05:00
|
|
|
const theme = useTheme();
|
2024-12-04 15:10:13 -05:00
|
|
|
let tabList = ["General Settings", "Contents"];
|
2024-12-13 15:59:54 -05:00
|
|
|
const [errors, setErrors] = useState({});
|
|
|
|
|
const [form, setForm] = useState({
|
|
|
|
|
companyName: "",
|
|
|
|
|
url: "",
|
|
|
|
|
timezone: "America/Toronto",
|
|
|
|
|
color: "#4169E1",
|
|
|
|
|
//which fields matching below?
|
|
|
|
|
publish: false,
|
|
|
|
|
logo: null,
|
|
|
|
|
monitors: [],
|
|
|
|
|
showUptimePercentage: false,
|
2024-12-16 20:18:05 -05:00
|
|
|
showBarcode: false,
|
2024-12-13 15:59:54 -05:00
|
|
|
});
|
2024-12-16 20:18:05 -05:00
|
|
|
const tab = open
|
|
|
|
|
.split("-")
|
|
|
|
|
.map((a) => capitalizeFirstLetter(a))
|
|
|
|
|
.join(" ");
|
|
|
|
|
|
|
|
|
|
const [tabIdx, setTabIdx] = useState(tabList.indexOf(tab));
|
|
|
|
|
|
|
|
|
|
const handleTabChange = (event, newTab) => {
|
|
|
|
|
setTabIdx(tabList.indexOf(newTab));
|
|
|
|
|
};
|
2024-12-13 15:59:54 -05:00
|
|
|
|
|
|
|
|
const handleSubmit = () => {
|
|
|
|
|
//validate rest of the form
|
|
|
|
|
delete form.logo;
|
2024-12-16 20:18:05 -05:00
|
|
|
if (hasValidationErrors(form, publicPageGeneralSettingsValidation, setErrors)) {
|
2024-12-13 15:59:54 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// //validate image field, double check if it is required
|
|
|
|
|
// let error = validateField(
|
|
|
|
|
// { type: logo?.type ?? null, size: logo?.size ?? null },
|
|
|
|
|
// logoImageValidation
|
|
|
|
|
// );
|
2024-12-16 20:18:05 -05:00
|
|
|
// if (error) return;
|
2024-12-13 15:59:54 -05:00
|
|
|
form.logo = { ...logo, size: formatBytes(logo?.size) };
|
2024-12-16 20:18:05 -05:00
|
|
|
};
|
2024-12-13 15:59:54 -05:00
|
|
|
|
2024-12-04 15:10:13 -05:00
|
|
|
useEffect(() => {
|
|
|
|
|
setTabIdx(tabList.indexOf(tab));
|
|
|
|
|
}, [tab]);
|
|
|
|
|
|
2024-11-26 16:10:55 -05:00
|
|
|
return (
|
|
|
|
|
<Box
|
|
|
|
|
className="status"
|
|
|
|
|
px={theme.spacing(20)}
|
|
|
|
|
py={theme.spacing(12)}
|
|
|
|
|
border={1}
|
|
|
|
|
borderColor={theme.palette.border.light}
|
|
|
|
|
borderRadius={theme.shape.borderRadius}
|
|
|
|
|
backgroundColor={theme.palette.background.main}
|
|
|
|
|
>
|
2024-12-13 15:59:54 -05:00
|
|
|
<TabContext value={tabList[tabIdx]}>
|
2024-11-26 16:10:55 -05:00
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
borderBottom: 1,
|
|
|
|
|
borderColor: theme.palette.border.light,
|
|
|
|
|
"& .MuiTabs-root": { height: "fit-content", minHeight: "0" },
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<TabList
|
|
|
|
|
onChange={handleTabChange}
|
|
|
|
|
aria-label="status tabs"
|
|
|
|
|
>
|
|
|
|
|
{tabList.map((label, index) => (
|
|
|
|
|
<Tab
|
|
|
|
|
label={label}
|
|
|
|
|
key={index}
|
2024-12-04 13:41:37 -05:00
|
|
|
value={label}
|
2024-11-26 16:10:55 -05:00
|
|
|
sx={{
|
|
|
|
|
fontSize: 13,
|
|
|
|
|
color: theme.palette.text.tertiary,
|
|
|
|
|
textTransform: "none",
|
|
|
|
|
minWidth: "fit-content",
|
|
|
|
|
minHeight: 0,
|
|
|
|
|
paddingLeft: 0,
|
|
|
|
|
paddingY: theme.spacing(4),
|
|
|
|
|
fontWeight: 400,
|
|
|
|
|
marginRight: theme.spacing(8),
|
|
|
|
|
"&:focus": {
|
|
|
|
|
outline: "none",
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</TabList>
|
|
|
|
|
</Box>
|
2024-12-16 20:18:05 -05:00
|
|
|
<StatusFormProvider
|
|
|
|
|
form={initForm && Object.keys(initForm) > 0 ? initForm : form}
|
|
|
|
|
setForm={setForm}
|
|
|
|
|
errors={errors}
|
|
|
|
|
setErrors={setErrors}
|
|
|
|
|
>
|
|
|
|
|
{tabIdx == 0 ? <GeneralSettingsPanel /> : <ContentPanel />}
|
2024-12-13 15:59:54 -05:00
|
|
|
</StatusFormProvider>
|
2024-11-26 16:10:55 -05:00
|
|
|
</TabContext>
|
2024-12-13 15:59:54 -05:00
|
|
|
<Stack
|
|
|
|
|
direction="row"
|
|
|
|
|
justifyContent="flex-end"
|
|
|
|
|
>
|
|
|
|
|
<Button
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="primary"
|
|
|
|
|
onClick={handleSubmit}
|
|
|
|
|
>
|
|
|
|
|
Save
|
|
|
|
|
</Button>
|
|
|
|
|
</Stack>
|
2024-11-26 16:10:55 -05:00
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CreateStatus.propTypes = {
|
2024-12-04 13:41:37 -05:00
|
|
|
open: PropTypes.oneOf(["general-settings", "contents"]),
|
2024-11-26 16:10:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default CreateStatus;
|