Refactored old validation from profile and password forms, fixed alert text

This commit is contained in:
Daniel Cojocea committed 2024-07-15 20:51:18 -04:00
1 parent 2a77c3fc12
commit d39a716ab6
6 files changed
+37 -70

No files matched your search

+4 -2
View File
@@ -2,6 +2,8 @@
margin: 0; margin: 0;
width: fit-content; width: fit-content;
} }
.alert, .alert button{ .alert,
.alert button,
.alert .MuiTypography-root {
font-size: var(--env-var-font-size-medium); font-size: var(--env-var-font-size-medium);
} }
+12 -4
View File
@@ -1,6 +1,6 @@
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import { Box, IconButton, Stack } from "@mui/material"; import { Box, IconButton, Stack, Typography } from "@mui/material";
import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined"; import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined";
import ErrorOutlineOutlinedIcon from "@mui/icons-material/ErrorOutlineOutlined"; import ErrorOutlineOutlinedIcon from "@mui/icons-material/ErrorOutlineOutlined";
import WarningAmberOutlinedIcon from "@mui/icons-material/WarningAmberOutlined"; import WarningAmberOutlinedIcon from "@mui/icons-material/WarningAmberOutlined";
@@ -50,9 +50,17 @@ const Alert = ({ variant, title, body, isToast, hasIcon = true, onClick }) => {
}} }}
> >
{hasIcon && <Box sx={{ color: color }}>{icon}</Box>} {hasIcon && <Box sx={{ color: color }}>{icon}</Box>}
<Stack direction="column" gap="2px" sx={{ flex: 1, color: color }}> <Stack direction="column" gap="2px" sx={{ flex: 1 }}>
{title && <Box sx={{ fontWeight: "700" }}>{title}</Box>} {title && (
{body && <Box sx={{ fontWeight: "400" }}>{body}</Box>} <Typography sx={{ fontWeight: "700", color: `${color} !important` }}>
{title}
</Typography>
)}
{body && (
<Typography sx={{ fontWeight: "400", color: `${color} !important` }}>
{body}
</Typography>
)}
{hasIcon && isToast && ( {hasIcon && isToast && (
<Button <Button
level="tertiary" level="tertiary"
@@ -4,7 +4,7 @@ import { useTheme } from "@emotion/react";
import { Box, Divider, Stack, Typography } from "@mui/material"; import { Box, Divider, Stack, Typography } from "@mui/material";
import ButtonSpinner from "../../ButtonSpinner"; import ButtonSpinner from "../../ButtonSpinner";
import Field from "../../Inputs/Field"; import Field from "../../Inputs/Field";
import { editPasswordValidation } from "../../../Validation/validation"; import { credentials } from "../../../Validation/validation";
import Alert from "../../Alert"; import Alert from "../../Alert";
import { update } from "../../../Features/Auth/authSlice"; import { update } from "../../../Features/Auth/authSlice";
import { useDispatch, useSelector } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
@@ -44,9 +44,9 @@ const PasswordPanel = () => {
[name]: value, [name]: value,
})); }));
const validation = editPasswordValidation.validate( const validation = credentials.validate(
{ [name]: value }, { [name]: value },
{ abortEarly: false, context: { newPassword: localData.newPassword } } { abortEarly: false, context: { password: localData.newPassword } }
); );
setErrors((prev) => { setErrors((prev) => {
@@ -64,9 +64,9 @@ const PasswordPanel = () => {
const handleSubmit = async (event) => { const handleSubmit = async (event) => {
event.preventDefault(); event.preventDefault();
const { error } = editPasswordValidation.validate(localData, { const { error } = credentials.validate(localData, {
abortEarly: false, abortEarly: false,
context: { newPassword: localData.newPassword }, context: { password: localData.newPassword },
}); });
if (error) { if (error) {
@@ -7,10 +7,7 @@ import Button from "../../Button";
import Avatar from "../../Avatar"; import Avatar from "../../Avatar";
import Field from "../../Inputs/Field"; import Field from "../../Inputs/Field";
import ImageField from "../../Inputs/Image"; import ImageField from "../../Inputs/Image";
import { import { credentials, imageValidation } from "../../../Validation/validation";
editProfileValidation,
imageValidation,
} from "../../../Validation/validation";
import { useDispatch, useSelector } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
import { import {
clearAuthState, clearAuthState,
@@ -66,7 +63,7 @@ const ProfilePanel = () => {
[name]: value, [name]: value,
})); }));
validateField({ [name]: value }, editProfileValidation, name); validateField({ [name]: value }, credentials, name);
}; };
// Handles image file // Handles image file
@@ -165,8 +162,8 @@ const ProfilePanel = () => {
localData.file === undefined localData.file === undefined
) { ) {
createToast({ createToast({
variant: "warning", variant: "info",
body: "Unable to update profile: No changes detected.", body: "Unable to update profile — no changes detected.",
hasIcon: false, hasIcon: false,
}); });
setErrors({ unchanged: "unable to update profile" }); setErrors({ unchanged: "unable to update profile" });
+12 -12
View File
@@ -40,18 +40,18 @@ const Register = () => {
}); });
const [errors, setErrors] = useState({}); const [errors, setErrors] = useState({});
// useEffect(() => { useEffect(() => {
// axiosInstance axiosInstance
// .get("/auth/users/admin") .get("/auth/users/admin")
// .then((response) => { .then((response) => {
// if (response.data.data === true) { if (response.data.data === true) {
// navigate("/login"); navigate("/login");
// } }
// }) })
// .catch((error) => { .catch((error) => {
// console.log(error); console.log(error);
// }); });
// }, [form, navigate]); }, [form, navigate]);
const handleSubmit = async (e) => { const handleSubmit = async (e) => {
e.preventDefault(); e.preventDefault();
-40
View File
@@ -139,44 +139,6 @@ const newPasswordValidation = joi.object({
}), }),
}); });
const editProfileValidation = joi.object({
firstname: joi.string().trim().pattern(new RegExp("^[A-Za-z]+$")).messages({
"string.empty": "*First name is required.",
"string.pattern.base": "*First name must contain only letters.",
}),
lastname: joi.string().trim().pattern(new RegExp("^[A-Za-z]+$")).messages({
"string.empty": "*Last name is required.",
"string.pattern.base": "*Last name must contain only letters.",
}),
email: joi
.string()
.trim()
.email({ tlds: { allow: false } })
.messages({
"string.empty": "*Email is required.",
"string.email": "*Invalid email address.",
}),
});
const editPasswordValidation = joi.object({
// TBD - validation for current password ?
password: passwordSchema,
newPassword: passwordSchema,
confirm: joi
.string()
.trim()
.messages({
"string.empty": "Password confirmation is required",
})
.custom((value, helpers) => {
const { newPassword } = helpers.prefs.context;
if (value !== newPassword) {
return helpers.message("*Passwords do not match.");
}
return value;
}),
});
const createMonitorValidation = joi.object({ const createMonitorValidation = joi.object({
url: joi url: joi
.string() .string()
@@ -218,6 +180,4 @@ export {
loginValidation, loginValidation,
recoveryValidation, recoveryValidation,
newPasswordValidation, newPasswordValidation,
editPasswordValidation,
editProfileValidation,
}; };