Integrated joi validation
This commit is contained in:
1 parent
b85f1f6396
commit
75f28eaf45
5 files changed
+174
-126
No files matched your search
@@ -2,22 +2,12 @@ import TabPanel from "@mui/lab/TabPanel";
|
|||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import AnnouncementsDualButtonWithIcon from "../../Announcements/AnnouncementsDualButtonWithIcon/AnnouncementsDualButtonWithIcon";
|
import AnnouncementsDualButtonWithIcon from "../../Announcements/AnnouncementsDualButtonWithIcon/AnnouncementsDualButtonWithIcon";
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import {
|
import { Box, Divider, Stack, Typography } from "@mui/material";
|
||||||
Box,
|
|
||||||
Divider,
|
|
||||||
FormControl,
|
|
||||||
IconButton,
|
|
||||||
InputAdornment,
|
|
||||||
OutlinedInput,
|
|
||||||
Stack,
|
|
||||||
Typography,
|
|
||||||
} from "@mui/material";
|
|
||||||
import VisibilityOff from "@mui/icons-material/VisibilityOff";
|
|
||||||
import Visibility from "@mui/icons-material/Visibility";
|
|
||||||
import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined";
|
import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined";
|
||||||
import WarningAmberOutlinedIcon from "@mui/icons-material/WarningAmberOutlined";
|
import WarningAmberOutlinedIcon from "@mui/icons-material/WarningAmberOutlined";
|
||||||
import ButtonSpinner from "../../ButtonSpinner";
|
import ButtonSpinner from "../../ButtonSpinner";
|
||||||
import PasswordTextField from "../../TextFields/Password/PasswordTextField";
|
import PasswordTextField from "../../TextFields/Password/PasswordTextField";
|
||||||
|
import { editPasswordValidation } from "../../../Validation/validation";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PasswordPanel component manages the form for editing password.
|
* PasswordPanel component manages the form for editing password.
|
||||||
@@ -33,86 +23,76 @@ const PasswordPanel = () => {
|
|||||||
const [showPassword, setShowPassword] = useState(false);
|
const [showPassword, setShowPassword] = useState(false);
|
||||||
|
|
||||||
//for testing, will tweak when I implement redux slice
|
//for testing, will tweak when I implement redux slice
|
||||||
|
const idToName = {
|
||||||
|
"edit-current-password": "currentPassword",
|
||||||
|
"edit-new-password": "password",
|
||||||
|
"edit-confirm-password": "confirm",
|
||||||
|
};
|
||||||
const [localData, setLocalData] = useState({
|
const [localData, setLocalData] = useState({
|
||||||
"edit-current-password": {
|
currentPassword: "",
|
||||||
value: "",
|
password: "",
|
||||||
//TBD
|
confirm: "",
|
||||||
type: "",
|
|
||||||
},
|
|
||||||
"edit-new-password": {
|
|
||||||
value: "",
|
|
||||||
type: "password",
|
|
||||||
},
|
|
||||||
"edit-confirm-password": {
|
|
||||||
value: "",
|
|
||||||
type: "confirm",
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
|
|
||||||
const handleChange = (event) => {
|
const handleChange = (event) => {
|
||||||
const { value, id } = event.target;
|
const { value, id } = event.target;
|
||||||
|
const name = idToName[id];
|
||||||
setLocalData((prev) => ({
|
setLocalData((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
[id]: {
|
[name]: value,
|
||||||
...prev[id],
|
|
||||||
value: value,
|
|
||||||
},
|
|
||||||
}));
|
}));
|
||||||
validateField(id, value);
|
|
||||||
};
|
|
||||||
|
|
||||||
const validateField = (id, value) => {
|
const validation = editPasswordValidation.validate(
|
||||||
let error = "";
|
{ [name]: value },
|
||||||
switch (localData[id].type) {
|
{ abortEarly: false, context: { password: localData.password } }
|
||||||
case "password":
|
);
|
||||||
error =
|
|
||||||
value.trim() === ""
|
|
||||||
? "*This field is required."
|
|
||||||
: value.length < 8
|
|
||||||
? "*Password must be at least 8 characters long."
|
|
||||||
: !/[A-Z]/.test(value)
|
|
||||||
? "*Password must contain at least one uppercase letter."
|
|
||||||
: !/\d/.test(value)
|
|
||||||
? "*Password must contain at least one number."
|
|
||||||
: !/[!@#$%^&*]/.test(value)
|
|
||||||
? "*Password must contain at least one symbol."
|
|
||||||
: "";
|
|
||||||
break;
|
|
||||||
case "confirm":
|
|
||||||
error =
|
|
||||||
value.trim() === ""
|
|
||||||
? "*This field is required."
|
|
||||||
: value !== localData["edit-new-password"].value
|
|
||||||
? "*Passwords do not match."
|
|
||||||
: "";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
setErrors((prev) => {
|
setErrors((prev) => {
|
||||||
const updatedErrors = { ...prev };
|
const updatedErrors = { ...prev };
|
||||||
if (error === "") {
|
|
||||||
delete updatedErrors[id];
|
if (validation.error) {
|
||||||
|
updatedErrors[name] = validation.error.details[0].message;
|
||||||
} else {
|
} else {
|
||||||
updatedErrors[id] = error;
|
delete updatedErrors[name];
|
||||||
}
|
}
|
||||||
return updatedErrors;
|
return updatedErrors;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
//TODO - implement save password function
|
const handleSubmit = (event) => {
|
||||||
const handleSavePassword = () => {
|
event.preventDefault();
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
|
||||||
|
const { error } = editPasswordValidation.validate(localData, {
|
||||||
|
abortEarly: false,
|
||||||
|
context: { password: localData.password },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
const newErrors = {};
|
||||||
|
error.details.forEach((err) => {
|
||||||
|
newErrors[err.path[0]] = err.message;
|
||||||
|
});
|
||||||
|
setErrors(newErrors);
|
||||||
|
setIsLoading(false);
|
||||||
|
} else {
|
||||||
|
//TODO - submit logic
|
||||||
|
}
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
setIsOpen(false);
|
|
||||||
}, 2000);
|
}, 2000);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TabPanel value="1">
|
<TabPanel value="1">
|
||||||
<form className="edit-password-form" noValidate spellCheck="false">
|
<form
|
||||||
|
onSubmit={handleSubmit}
|
||||||
|
className="edit-password-form"
|
||||||
|
noValidate
|
||||||
|
spellCheck="false"
|
||||||
|
>
|
||||||
<div className="edit-password-form__wrapper">
|
<div className="edit-password-form__wrapper">
|
||||||
<AnnouncementsDualButtonWithIcon
|
<AnnouncementsDualButtonWithIcon
|
||||||
icon={
|
icon={
|
||||||
@@ -138,7 +118,16 @@ const PasswordPanel = () => {
|
|||||||
autoComplete="current-password"
|
autoComplete="current-password"
|
||||||
visibility={showPassword}
|
visibility={showPassword}
|
||||||
setVisibility={setShowPassword}
|
setVisibility={setShowPassword}
|
||||||
|
onChange={handleChange}
|
||||||
|
error={errors[idToName["edit-current-password"]] ? true : false}
|
||||||
/>
|
/>
|
||||||
|
{errors[idToName["edit-current-password"]] ? (
|
||||||
|
<Typography variant="h5" component="p" className="input-error">
|
||||||
|
{errors[idToName["edit-current-password"]]}
|
||||||
|
</Typography>
|
||||||
|
) : (
|
||||||
|
""
|
||||||
|
)}
|
||||||
</Stack>
|
</Stack>
|
||||||
</div>
|
</div>
|
||||||
<div className="edit-password-form__wrapper">
|
<div className="edit-password-form__wrapper">
|
||||||
@@ -156,11 +145,11 @@ const PasswordPanel = () => {
|
|||||||
visibility={showPassword}
|
visibility={showPassword}
|
||||||
setVisibility={setShowPassword}
|
setVisibility={setShowPassword}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
error={errors["edit-new-password"] ? true : false}
|
error={errors[idToName["edit-new-password"]] ? true : false}
|
||||||
/>
|
/>
|
||||||
{errors["edit-new-password"] ? (
|
{errors[idToName["edit-new-password"]] ? (
|
||||||
<Typography variant="h5" component="p" className="input-error">
|
<Typography variant="h5" component="p" className="input-error">
|
||||||
{errors["edit-new-password"]}
|
{errors[idToName["edit-new-password"]]}
|
||||||
</Typography>
|
</Typography>
|
||||||
) : (
|
) : (
|
||||||
""
|
""
|
||||||
@@ -182,11 +171,11 @@ const PasswordPanel = () => {
|
|||||||
visibility={showPassword}
|
visibility={showPassword}
|
||||||
setVisibility={setShowPassword}
|
setVisibility={setShowPassword}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
error={errors["edit-confirm-password"] ? true : false}
|
error={errors[idToName["edit-confirm-password"]] ? true : false}
|
||||||
/>
|
/>
|
||||||
{errors["edit-confirm-password"] ? (
|
{errors[idToName["edit-confirm-password"]] ? (
|
||||||
<Typography variant="h5" component="p" className="input-error">
|
<Typography variant="h5" component="p" className="input-error">
|
||||||
{errors["edit-confirm-password"]}
|
{errors[idToName["edit-confirm-password"]]}
|
||||||
</Typography>
|
</Typography>
|
||||||
) : (
|
) : (
|
||||||
""
|
""
|
||||||
@@ -208,7 +197,7 @@ const PasswordPanel = () => {
|
|||||||
<ButtonSpinner
|
<ButtonSpinner
|
||||||
level="primary"
|
level="primary"
|
||||||
label="Save"
|
label="Save"
|
||||||
onClick={handleSavePassword}
|
onClick={handleSubmit}
|
||||||
isLoading={isLoading}
|
isLoading={isLoading}
|
||||||
loadingText="Saving..."
|
loadingText="Saving..."
|
||||||
disabled={Object.keys(errors).length !== 0 && true}
|
disabled={Object.keys(errors).length !== 0 && true}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import Button from "../../Button";
|
|||||||
import EmailTextField from "../../TextFields/Email/EmailTextField";
|
import EmailTextField from "../../TextFields/Email/EmailTextField";
|
||||||
import StringTextField from "../../TextFields/Text/TextField";
|
import StringTextField from "../../TextFields/Text/TextField";
|
||||||
import Avatar from "../../Avatar";
|
import Avatar from "../../Avatar";
|
||||||
|
import { editProfileValidation } from "../../../Validation/validation";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ProfilePanel component displays a form for editing user profile information
|
* ProfilePanel component displays a form for editing user profile information
|
||||||
@@ -23,63 +24,38 @@ const ProfilePanel = () => {
|
|||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
|
||||||
//for testing, will tweak when I implement redux slice
|
//for testing, will tweak when I implement redux slice
|
||||||
|
const idToName = {
|
||||||
|
"edit-first-name": "firstName",
|
||||||
|
"edit-last-name": "lastName",
|
||||||
|
"edit-email": "email",
|
||||||
|
};
|
||||||
const [localData, setLocalData] = useState({
|
const [localData, setLocalData] = useState({
|
||||||
"edit-first-name": {
|
firstName: "",
|
||||||
value: "",
|
lastName: "",
|
||||||
type: "name",
|
email: "",
|
||||||
},
|
|
||||||
"edit-last-name": {
|
|
||||||
value: "",
|
|
||||||
type: "name",
|
|
||||||
},
|
|
||||||
"edit-email": {
|
|
||||||
value: "",
|
|
||||||
type: "email",
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
|
|
||||||
const handleChange = (event) => {
|
const handleChange = (event) => {
|
||||||
const { value, id } = event.target;
|
const { value, id } = event.target;
|
||||||
|
const name = idToName[id];
|
||||||
setLocalData((prev) => ({
|
setLocalData((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
[id]: {
|
[name]: value,
|
||||||
...prev[id],
|
|
||||||
value: value,
|
|
||||||
},
|
|
||||||
}));
|
}));
|
||||||
validateField(id, value);
|
|
||||||
};
|
|
||||||
|
|
||||||
const validateField = (id, value) => {
|
const validation = editProfileValidation.validate(
|
||||||
let error = "";
|
{ [name]: value },
|
||||||
switch (localData[id].type) {
|
{ abortEarly: false }
|
||||||
case "name":
|
);
|
||||||
error =
|
|
||||||
value.trim() === ""
|
|
||||||
? "*This field is required."
|
|
||||||
: /\d/.test(value)
|
|
||||||
? "*Name is invalid."
|
|
||||||
: "";
|
|
||||||
break;
|
|
||||||
case "email":
|
|
||||||
error =
|
|
||||||
value.trim() === ""
|
|
||||||
? "*This field is required."
|
|
||||||
: !/^\S+@\S+\.\S+$/.test(value)
|
|
||||||
? "*Email is invalid."
|
|
||||||
: "";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
setErrors((prev) => {
|
setErrors((prev) => {
|
||||||
const updatedErrors = { ...prev };
|
const updatedErrors = { ...prev };
|
||||||
if (error === "") {
|
|
||||||
delete updatedErrors[id];
|
if (validation.error) {
|
||||||
|
updatedErrors[name] = validation.error.details[0].message;
|
||||||
} else {
|
} else {
|
||||||
updatedErrors[id] = error;
|
delete updatedErrors[name];
|
||||||
}
|
}
|
||||||
return updatedErrors;
|
return updatedErrors;
|
||||||
});
|
});
|
||||||
@@ -128,11 +104,11 @@ const ProfilePanel = () => {
|
|||||||
placeholder="Enter your first name"
|
placeholder="Enter your first name"
|
||||||
autoComplete="given-name"
|
autoComplete="given-name"
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
error={errors["edit-first-name"] ? true : false}
|
error={errors[idToName["edit-first-name"]] ? true : false}
|
||||||
/>
|
/>
|
||||||
{errors["edit-first-name"] ? (
|
{errors[idToName["edit-first-name"]] ? (
|
||||||
<Typography variant="h5" component="p" className="input-error">
|
<Typography variant="h5" component="p" className="input-error">
|
||||||
{errors["edit-first-name"]}
|
{errors[idToName["edit-first-name"]]}
|
||||||
</Typography>
|
</Typography>
|
||||||
) : (
|
) : (
|
||||||
""
|
""
|
||||||
@@ -152,11 +128,11 @@ const ProfilePanel = () => {
|
|||||||
placeholder="Enter your last name"
|
placeholder="Enter your last name"
|
||||||
autoComplete="family-name"
|
autoComplete="family-name"
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
error={errors["edit-last-name"] ? true : false}
|
error={errors[idToName["edit-last-name"]] ? true : false}
|
||||||
/>
|
/>
|
||||||
{errors["edit-last-name"] ? (
|
{errors[idToName["edit-last-name"]] ? (
|
||||||
<Typography variant="h5" component="p" className="input-error">
|
<Typography variant="h5" component="p" className="input-error">
|
||||||
{errors["edit-last-name"]}
|
{errors[idToName["edit-last-name"]]}
|
||||||
</Typography>
|
</Typography>
|
||||||
) : (
|
) : (
|
||||||
""
|
""
|
||||||
@@ -179,11 +155,11 @@ const ProfilePanel = () => {
|
|||||||
placeholder="Enter your email"
|
placeholder="Enter your email"
|
||||||
autoComplete="email"
|
autoComplete="email"
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
error={errors["edit-email"] ? true : false}
|
error={errors[idToName["edit-email"]] ? true : false}
|
||||||
/>
|
/>
|
||||||
{errors["edit-email"] ? (
|
{errors[idToName["edit-email"]] ? (
|
||||||
<Typography variant="h5" component="p" className="input-error">
|
<Typography variant="h5" component="p" className="input-error">
|
||||||
{errors["edit-email"]}
|
{errors[idToName["edit-email"]]}
|
||||||
</Typography>
|
</Typography>
|
||||||
) : (
|
) : (
|
||||||
""
|
""
|
||||||
@@ -206,7 +182,10 @@ const ProfilePanel = () => {
|
|||||||
firstName="Jackie"
|
firstName="Jackie"
|
||||||
lastName="Dawn"
|
lastName="Dawn"
|
||||||
sx={{
|
sx={{
|
||||||
width: "64px", height: "64px", border: "none", mr: "8px"
|
width: "64px",
|
||||||
|
height: "64px",
|
||||||
|
border: "none",
|
||||||
|
mr: "8px",
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<ButtonSpinner
|
<ButtonSpinner
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ const PasswordTextField = ({
|
|||||||
<IconButton
|
<IconButton
|
||||||
aria-label="toggle password visibility"
|
aria-label="toggle password visibility"
|
||||||
onClick={() => setVisibility((show) => !show)}
|
onClick={() => setVisibility((show) => !show)}
|
||||||
|
tabIndex={-1}
|
||||||
sx={{
|
sx={{
|
||||||
width: "30px",
|
width: "30px",
|
||||||
height: "30px",
|
height: "30px",
|
||||||
|
|||||||
@@ -171,9 +171,15 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.settings .MuiStack-root:has(p.MuiTypography-root.input-error){
|
||||||
|
position: relative;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
}
|
||||||
.settings p.MuiTypography-root.input-error{
|
.settings p.MuiTypography-root.input-error{
|
||||||
color: var(--env-var-color-24);
|
color: var(--env-var-color-24);
|
||||||
opacity: 0.8;
|
opacity: 0.8;
|
||||||
|
position: absolute;
|
||||||
|
top: calc(100% + 5px);
|
||||||
}
|
}
|
||||||
.settings .email-text-field,
|
.settings .email-text-field,
|
||||||
.settings .password-text-field {
|
.settings .password-text-field {
|
||||||
|
|||||||
@@ -63,9 +63,82 @@ 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 ?
|
||||||
|
currentPassword : joi
|
||||||
|
.string().trim()
|
||||||
|
.messages({
|
||||||
|
"string.empty": "*Current password is required.",
|
||||||
|
}),
|
||||||
|
password: joi
|
||||||
|
.string()
|
||||||
|
.trim()
|
||||||
|
.min(8)
|
||||||
|
.messages({
|
||||||
|
"string.empty": "*Password is required.",
|
||||||
|
"string.min": "*Password must be at least 8 characters long.",
|
||||||
|
})
|
||||||
|
.custom((value, helpers) => {
|
||||||
|
if (!/[A-Z]/.test(value)) {
|
||||||
|
return helpers.message(
|
||||||
|
"*Password must contain at least one uppercase letter."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!/[a-z]/.test(value)) {
|
||||||
|
return helpers.message(
|
||||||
|
"*Password must contain at least one lowercase letter."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!/\d/.test(value)) {
|
||||||
|
return helpers.message("*Password must contain at least one number.");
|
||||||
|
}
|
||||||
|
if (!/[!@#$%^&*]/.test(value)) {
|
||||||
|
return helpers.message(
|
||||||
|
"*Password must contain at least one special character."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}),
|
||||||
|
confirm: joi
|
||||||
|
.string()
|
||||||
|
.trim()
|
||||||
|
.messages({
|
||||||
|
"string.empty": "*Password confirmation is required.",
|
||||||
|
})
|
||||||
|
.custom((value, helpers) => {
|
||||||
|
const { password } = helpers.prefs.context;
|
||||||
|
if (value !== password) {
|
||||||
|
return helpers.message("*Passwords do not match.");
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
export {
|
export {
|
||||||
registerValidation,
|
registerValidation,
|
||||||
loginValidation,
|
loginValidation,
|
||||||
recoveryValidation,
|
recoveryValidation,
|
||||||
newPasswordValidation,
|
newPasswordValidation,
|
||||||
|
editPasswordValidation,
|
||||||
|
editProfileValidation,
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user