- Add log and general settins tab validation
- WIP logo uploading in general settings tab
This commit is contained in:
1 parent
8506291586
commit
df36f873ae
3 files changed
+194
-32
No files matched your search
@@ -14,8 +14,9 @@ import { checkImage } from "../../../Utils/fileUtils";
|
|||||||
* @returns {JSX.Element} The rendered component.
|
* @returns {JSX.Element} The rendered component.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const ImageField = ({ id, src, loading, onChange }) => {
|
const ImageField = ({ id, src, loading, onChange, error }) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
const error_border_style = error? {borderColor: theme.palette.error.main}: {}
|
||||||
|
|
||||||
const [isDragging, setIsDragging] = useState(false);
|
const [isDragging, setIsDragging] = useState(false);
|
||||||
const handleDragEnter = () => {
|
const handleDragEnter = () => {
|
||||||
@@ -28,7 +29,7 @@ const ImageField = ({ id, src, loading, onChange }) => {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{!checkImage(src) || loading ? (
|
{!checkImage(src) || loading ? (
|
||||||
<>
|
<>
|
||||||
<Box
|
<Box
|
||||||
className="image-field-wrapper"
|
className="image-field-wrapper"
|
||||||
mt={theme.spacing(8)}
|
mt={theme.spacing(8)}
|
||||||
@@ -46,6 +47,7 @@ const ImageField = ({ id, src, loading, onChange }) => {
|
|||||||
borderColor: theme.palette.primary.main,
|
borderColor: theme.palette.primary.main,
|
||||||
backgroundColor: "hsl(215, 87%, 51%, 0.05)",
|
backgroundColor: "hsl(215, 87%, 51%, 0.05)",
|
||||||
},
|
},
|
||||||
|
...error_border_style
|
||||||
}}
|
}}
|
||||||
onDragEnter={handleDragEnter}
|
onDragEnter={handleDragEnter}
|
||||||
onDragLeave={handleDragLeave}
|
onDragLeave={handleDragLeave}
|
||||||
@@ -62,7 +64,7 @@ const ImageField = ({ id, src, loading, onChange }) => {
|
|||||||
cursor: "pointer",
|
cursor: "pointer",
|
||||||
maxWidth: "500px",
|
maxWidth: "500px",
|
||||||
minHeight: "175px",
|
minHeight: "175px",
|
||||||
zIndex: 1
|
zIndex: 1,
|
||||||
},
|
},
|
||||||
"& fieldset": {
|
"& fieldset": {
|
||||||
padding: 0,
|
padding: 0,
|
||||||
@@ -79,7 +81,7 @@ const ImageField = ({ id, src, loading, onChange }) => {
|
|||||||
top: "50%",
|
top: "50%",
|
||||||
left: "50%",
|
left: "50%",
|
||||||
transform: "translate(-50%, -50%)",
|
transform: "translate(-50%, -50%)",
|
||||||
zIndex:0,
|
zIndex: 0,
|
||||||
width: "100%",
|
width: "100%",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
@@ -114,7 +116,7 @@ const ImageField = ({ id, src, loading, onChange }) => {
|
|||||||
>
|
>
|
||||||
(maximum size: 3MB)
|
(maximum size: 3MB)
|
||||||
</Typography>
|
</Typography>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Box>
|
</Box>
|
||||||
<Typography
|
<Typography
|
||||||
component="p"
|
component="p"
|
||||||
@@ -123,6 +125,19 @@ const ImageField = ({ id, src, loading, onChange }) => {
|
|||||||
>
|
>
|
||||||
Supported formats: JPG, PNG
|
Supported formats: JPG, PNG
|
||||||
</Typography>
|
</Typography>
|
||||||
|
{error && (
|
||||||
|
<Typography
|
||||||
|
component="span"
|
||||||
|
className="input-error"
|
||||||
|
color={theme.palette.error.main}
|
||||||
|
mt={theme.spacing(2)}
|
||||||
|
sx={{
|
||||||
|
opacity: 0.8,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{error}
|
||||||
|
</Typography>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<Stack
|
<Stack
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useState, useRef } from "react";
|
import { useState, useRef } from "react";
|
||||||
import { Box, Stack, Typography } from "@mui/material";
|
import { Button, Box, Stack, Typography } from "@mui/material";
|
||||||
import { ConfigBox } from "../../../Pages/Settings/styled";
|
import { ConfigBox } from "../../../Pages/Settings/styled";
|
||||||
import Checkbox from "../../Inputs/Checkbox";
|
import Checkbox from "../../Inputs/Checkbox";
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
@@ -10,41 +10,115 @@ import ImageField from "../../Inputs/Image";
|
|||||||
import { setMode, setTimezone } from "../../../Features/UI/uiSlice";
|
import { setMode, setTimezone } from "../../../Features/UI/uiSlice";
|
||||||
import timezones from "../../../Utils/timezones.json";
|
import timezones from "../../../Utils/timezones.json";
|
||||||
import Select from "../../Inputs/Select"
|
import Select from "../../Inputs/Select"
|
||||||
|
import { logoImageValidation, publicPageGeneralSettingsValidation } from "../../../Validation/validation"
|
||||||
|
import { buildErrors } from "../../../Validation/error";
|
||||||
|
import { formatBytes } from "../../../Utils/fileUtils";
|
||||||
|
import ProgressUpload from "../../ProgressBars";
|
||||||
|
import ImageIcon from "@mui/icons-material/Image";
|
||||||
|
|
||||||
const GeneralSettingsPanel = () => {
|
const GeneralSettingsPanel = () => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
const [file, setFile] = useState();
|
const [logo, setLogo] = useState();
|
||||||
|
const [progress, setProgress] = useState({ value: 0, isLoading: false });
|
||||||
|
const intervalRef = useRef(null);
|
||||||
// Local state for form data, errors, and file handling
|
// Local state for form data, errors, and file handling
|
||||||
const [localData, setLocalData] = useState({
|
const [localData, setLocalData] = useState({
|
||||||
companyName: "",
|
companyName: "",
|
||||||
url: "",
|
url: "",
|
||||||
timezone: "America/Toronto",
|
timezone: "America/Toronto",
|
||||||
color: "#4169E1",
|
color: "#4169E1",
|
||||||
theme: "light",
|
theme: "light",
|
||||||
//which fields matching below?
|
//which fields matching below?
|
||||||
publish: false,
|
publish: false,
|
||||||
file: null
|
logo: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleChange = (event, appendedId) => {
|
// Clears specific error from errors state
|
||||||
|
const clearError = (err) => {
|
||||||
|
setErrors((prev) => {
|
||||||
|
const updatedErrors = { ...prev };
|
||||||
|
if (updatedErrors[err]) delete updatedErrors[err];
|
||||||
|
return updatedErrors;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
const removeLogo = () => {
|
||||||
|
errors["logo"] && clearError("logo");
|
||||||
|
setLogo({});
|
||||||
|
// interrupt interval if image upload is canceled prior to completing the process
|
||||||
|
clearInterval(intervalRef.current);
|
||||||
|
setProgress({ value: 0, isLoading: false });
|
||||||
|
};
|
||||||
|
|
||||||
|
// Updates profile image displayed on UI
|
||||||
|
const handleUpdateLogo = () => {
|
||||||
|
setProgress({ value: 0, isLoading: false });
|
||||||
|
setLocalData((prev) => ({
|
||||||
|
...prev,
|
||||||
|
logo: logo.src,
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleChange = (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const { value, id } = event.target;
|
const { value, id } = event.target;
|
||||||
let name = appendedId ?? idMap[id] ?? id;
|
|
||||||
setLocalData((prev) => ({
|
setLocalData((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
[name]: value,
|
[id]: value,
|
||||||
}));
|
}));
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
const handleSubmit = () => {};
|
const handleSubmit = () => {};
|
||||||
|
|
||||||
const handleBlur = () => {};
|
const handleBlur = (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
const { value, id } = event.target;
|
||||||
|
const { error } = publicPageGeneralSettingsValidation.validate(
|
||||||
|
{ [id]: value },
|
||||||
|
{
|
||||||
|
abortEarly: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const handlePicture = (event) => {
|
setErrors((prev) => {
|
||||||
|
return buildErrors(prev, id, error);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const validateField = (toValidate, schema, name = "logo") => {
|
||||||
|
const { error } = schema.validate(toValidate, { abortEarly: false });
|
||||||
|
setErrors((prev) => {
|
||||||
|
let prevErrors = { ...prev };
|
||||||
|
if (error) prevErrors[name] = error?.details[0].message;
|
||||||
|
else delete prevErrors[name];
|
||||||
|
return prevErrors;
|
||||||
|
});
|
||||||
|
if (error) return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleLogo = (event) => {
|
||||||
const pic = event.target.files[0];
|
const pic = event.target.files[0];
|
||||||
|
let error = validateField({ type: pic?.type, size: pic?.size }, logoImageValidation);
|
||||||
|
if (error) return;
|
||||||
|
|
||||||
|
setProgress((prev) => ({ ...prev, isLoading: true }));
|
||||||
|
setLogo({
|
||||||
|
src: URL.createObjectURL(pic),
|
||||||
|
name: pic.name,
|
||||||
|
size: formatBytes(pic.size),
|
||||||
|
});
|
||||||
|
|
||||||
|
intervalRef.current = setInterval(() => {
|
||||||
|
const buffer = 12;
|
||||||
|
setProgress((prev) => {
|
||||||
|
if (prev.value + buffer >= 100) {
|
||||||
|
clearInterval(intervalRef.current);
|
||||||
|
return { value: 100, isLoading: false };
|
||||||
|
}
|
||||||
|
return { ...prev, value: prev.value + buffer };
|
||||||
|
});
|
||||||
|
}, 120);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -79,7 +153,7 @@ const GeneralSettingsPanel = () => {
|
|||||||
id="published-to-public"
|
id="published-to-public"
|
||||||
label={`Published and visible to the public`}
|
label={`Published and visible to the public`}
|
||||||
isChecked={localData.publish}
|
isChecked={localData.publish}
|
||||||
value={localData.publish}
|
value={"localData.publish"}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
onBlur={handleBlur}
|
onBlur={handleBlur}
|
||||||
/>
|
/>
|
||||||
@@ -97,19 +171,24 @@ const GeneralSettingsPanel = () => {
|
|||||||
</Box>
|
</Box>
|
||||||
<Stack gap={theme.spacing(6)}>
|
<Stack gap={theme.spacing(6)}>
|
||||||
<Field
|
<Field
|
||||||
id="company-name"
|
id="companyName"
|
||||||
type="text"
|
type="text"
|
||||||
label="Company name"
|
label="Company name"
|
||||||
value={localData.companyName}
|
value={localData.companyName}
|
||||||
|
onChange={handleChange}
|
||||||
|
onBlur={handleBlur}
|
||||||
|
error={errors["companyName"]}
|
||||||
/>
|
/>
|
||||||
<Field
|
<Field
|
||||||
id="suburl"
|
id="url"
|
||||||
type="url"
|
type="url"
|
||||||
label="suburl"
|
label="SubURL"
|
||||||
placeholder={""}
|
placeholder={""}
|
||||||
value={localData.url}
|
value={localData.url}
|
||||||
prefix={"http://uptimegenie.com/"}
|
prefix={"http://uptimegenie.com/"}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
|
onBlur={handleBlur}
|
||||||
|
error={errors["url"]}
|
||||||
/>
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
</ConfigBox>
|
</ConfigBox>
|
||||||
@@ -128,29 +207,71 @@ const GeneralSettingsPanel = () => {
|
|||||||
id="display-timezone"
|
id="display-timezone"
|
||||||
label="Display timezone"
|
label="Display timezone"
|
||||||
value={localData.timezone}
|
value={localData.timezone}
|
||||||
onChange={(e) => {
|
onChange={handleChange}
|
||||||
dispatch(setTimezone({ timezone: e.target.value }));
|
onBlur={handleBlur}
|
||||||
}}
|
|
||||||
items={timezones}
|
items={timezones}
|
||||||
|
error={errors["display-timezone"]}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<ImageField
|
<ImageField
|
||||||
id="update-logo-picture"
|
id="logo"
|
||||||
src={localData.src}
|
src={localData.logo?.src??logo?.src}
|
||||||
loading={true}
|
loading={progress.isLoading && progress.value !== 100}
|
||||||
onChange={handlePicture}
|
onChange={handleLogo}
|
||||||
/>
|
/>
|
||||||
|
{progress.isLoading || progress.value !== 0 || errors["logo"] ? (
|
||||||
|
<ProgressUpload
|
||||||
|
icon={<ImageIcon />}
|
||||||
|
label={logo?.name}
|
||||||
|
size={logo?.size}
|
||||||
|
progress={progress.value}
|
||||||
|
onClick={removeLogo}
|
||||||
|
error={errors["logo"]}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
""
|
||||||
|
)}
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
mt={theme.spacing(10)}
|
||||||
|
gap={theme.spacing(5)}
|
||||||
|
justifyContent="flex-end"
|
||||||
|
>
|
||||||
|
<Button
|
||||||
|
variant="text"
|
||||||
|
color="info"
|
||||||
|
onClick={removeLogo}
|
||||||
|
>
|
||||||
|
Remove
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
color="primary"
|
||||||
|
onClick={handleUpdateLogo}
|
||||||
|
disabled={
|
||||||
|
(Object.keys(errors).length !== 0 && errors?.logo) ||
|
||||||
|
progress.value !== 100
|
||||||
|
? true
|
||||||
|
: false
|
||||||
|
}
|
||||||
|
>
|
||||||
|
Update
|
||||||
|
</Button>
|
||||||
|
</Stack>
|
||||||
<Field
|
<Field
|
||||||
id="color"
|
id="color"
|
||||||
label="Color"
|
label="Color"
|
||||||
value={localData.color}
|
value={localData.color}
|
||||||
|
error={errors["color"]}
|
||||||
|
onChange={handleChange}
|
||||||
|
onBlur={handleBlur}
|
||||||
/>
|
/>
|
||||||
<Select
|
<Select
|
||||||
id="theme"
|
id="theme"
|
||||||
label="Theme"
|
label="Theme"
|
||||||
value={localData.theme}
|
value={localData.theme}
|
||||||
onChange={(e) => {
|
onChange={handleChange}
|
||||||
dispatch(setMode(e.target.value));
|
onBlur={handleBlur}
|
||||||
}}
|
|
||||||
items={[
|
items={[
|
||||||
{ _id: "light", name: "Light" },
|
{ _id: "light", name: "Light" },
|
||||||
{ _id: "dark", name: "Dark" },
|
{ _id: "dark", name: "Dark" },
|
||||||
|
|||||||
@@ -120,6 +120,30 @@ const imageValidation = joi.object({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const logoImageValidation = joi.object({
|
||||||
|
type: joi.string().valid("image/jpeg", "image/png").messages({
|
||||||
|
"any.only": "Invalid file format.",
|
||||||
|
"string.empty": "File type required.",
|
||||||
|
}),
|
||||||
|
size: joi
|
||||||
|
.number()
|
||||||
|
.max(800*800)
|
||||||
|
.messages({
|
||||||
|
"number.base": "File size must be a number.",
|
||||||
|
"number.max": "File size must be less than 640000 pixels.",
|
||||||
|
"number.empty": "File size required.",
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const publicPageGeneralSettingsValidation = joi.object({
|
||||||
|
companyName: joi.string().trim().messages({ "string.empty": "Company name is required." }),
|
||||||
|
url: joi.string().trim().messages({ "string.empty": "URL is required." }),
|
||||||
|
timezone: joi.string().trim().messages({ "string.empty": "Timezone is required." }),
|
||||||
|
color: joi.string().trim().messages({ "string.empty": "Color is required." }),
|
||||||
|
theme: joi.string().trim().messages({ "string.empty": "Theme is required." })
|
||||||
|
}
|
||||||
|
|
||||||
|
)
|
||||||
const settingsValidation = joi.object({
|
const settingsValidation = joi.object({
|
||||||
ttl: joi.number().required().messages({
|
ttl: joi.number().required().messages({
|
||||||
"string.empty": "TTL is required",
|
"string.empty": "TTL is required",
|
||||||
@@ -220,4 +244,6 @@ export {
|
|||||||
maintenanceWindowValidation,
|
maintenanceWindowValidation,
|
||||||
advancedSettingsValidation,
|
advancedSettingsValidation,
|
||||||
infrastructureMonitorValidation,
|
infrastructureMonitorValidation,
|
||||||
|
publicPageGeneralSettingsValidation,
|
||||||
|
logoImageValidation
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user