feat: validation on change working for SetNewPassword
This commit is contained in:
1 parent
9b3f73da0e
commit
6905f49d3b
2 files changed
+49
-37
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
import { useState } from "react";
|
import { useId, useState } from "react";
|
||||||
import { useDispatch, useSelector } from "react-redux";
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
@@ -21,6 +21,9 @@ const SetNewPassword = () => {
|
|||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
|
||||||
|
const passwordId = useId();
|
||||||
|
const confirmPasswordId = useId();
|
||||||
|
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
const [form, setForm] = useState({
|
const [form, setForm] = useState({
|
||||||
password: "",
|
password: "",
|
||||||
@@ -36,11 +39,10 @@ const SetNewPassword = () => {
|
|||||||
const passwordForm = { ...form };
|
const passwordForm = { ...form };
|
||||||
const { error } = credentials.validate(passwordForm, {
|
const { error } = credentials.validate(passwordForm, {
|
||||||
abortEarly: false,
|
abortEarly: false,
|
||||||
/* context: { password: form.password }, */
|
context: { password: form.password },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
// validation errors
|
|
||||||
const newErrors = {};
|
const newErrors = {};
|
||||||
error.details.forEach((err) => {
|
error.details.forEach((err) => {
|
||||||
newErrors[err.path[0]] = err.message;
|
newErrors[err.path[0]] = err.message;
|
||||||
@@ -77,46 +79,43 @@ const SetNewPassword = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleChange = (event) => {
|
const handleChange = (event) => {
|
||||||
//TODO Change from id to name
|
|
||||||
const { value, name } = event.target;
|
const { value, name } = event.target;
|
||||||
setForm((prev) => ({
|
setForm((prev) => ({ ...prev, [name]: value }));
|
||||||
...prev,
|
|
||||||
[name]: value,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { error } = credentials.validate(
|
const validateValue = { [name]: value };
|
||||||
{ [name]: value },
|
const validateOptions = { abortEarly: false, context: { password: form.password } };
|
||||||
{
|
const { error } = credentials.validate(validateValue, validateOptions);
|
||||||
abortEarly: false,
|
|
||||||
context: { password: form.password },
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const errors = error.details.map((error) => error.message);
|
|
||||||
|
|
||||||
|
const errors = error?.details.map((error) => error.message);
|
||||||
setErrors((prev) => ({ ...prev, [name]: errors }));
|
setErrors((prev) => ({ ...prev, [name]: errors }));
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log(errors);
|
const getFeedbackStatus = (field, criteria) => {
|
||||||
|
const fieldErrors = errors[field];
|
||||||
|
const isFieldEmpty = form[field].length === 0;
|
||||||
|
const hasError = fieldErrors?.includes(criteria) || fieldErrors?.includes("empty");
|
||||||
|
const isCorrect = !isFieldEmpty && !hasError;
|
||||||
|
|
||||||
/* function getCheckStatus() */
|
if (isCorrect) {
|
||||||
|
return "success";
|
||||||
|
} else if (hasError) {
|
||||||
|
return "error";
|
||||||
|
} else {
|
||||||
|
return "info";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const feedbacks = {
|
const feedbacks = {
|
||||||
length: "info" /* !errors.password
|
length: getFeedbackStatus("password", "length"),
|
||||||
? "info"
|
special: getFeedbackStatus("password", "special"),
|
||||||
: errors.password === "length" || errors.password === "empty"
|
number: getFeedbackStatus("password", "number"),
|
||||||
? "error"
|
uppercase: getFeedbackStatus("password", "uppercase"),
|
||||||
: "success", */,
|
lowercase: getFeedbackStatus("password", "lowercase"),
|
||||||
special: "info" /* !errors
|
confirm: getFeedbackStatus("confirm", "different"),
|
||||||
? "info"
|
|
||||||
: errors.password === "length" || errors.password === "empty"
|
|
||||||
? "error"
|
|
||||||
: "success", */,
|
|
||||||
number: "info",
|
|
||||||
upper: "success",
|
|
||||||
lower: "error",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log({ feedbacks });
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack
|
<Stack
|
||||||
className="set-new-password-page auth"
|
className="set-new-password-page auth"
|
||||||
@@ -203,6 +202,7 @@ const SetNewPassword = () => {
|
|||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
>
|
>
|
||||||
<Field
|
<Field
|
||||||
|
id={passwordId}
|
||||||
type="password"
|
type="password"
|
||||||
name="password"
|
name="password"
|
||||||
label="Password"
|
label="Password"
|
||||||
@@ -210,7 +210,7 @@ const SetNewPassword = () => {
|
|||||||
placeholder="••••••••"
|
placeholder="••••••••"
|
||||||
value={form.password}
|
value={form.password}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
error={errors.password}
|
/* error={errors.password} */
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
<Box
|
<Box
|
||||||
@@ -220,6 +220,7 @@ const SetNewPassword = () => {
|
|||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
>
|
>
|
||||||
<Field
|
<Field
|
||||||
|
id={confirmPasswordId}
|
||||||
type="password"
|
type="password"
|
||||||
name="confirm"
|
name="confirm"
|
||||||
label="Confirm password"
|
label="Confirm password"
|
||||||
@@ -227,7 +228,7 @@ const SetNewPassword = () => {
|
|||||||
placeholder="••••••••"
|
placeholder="••••••••"
|
||||||
value={form.confirm}
|
value={form.confirm}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
error={errors.confirm}
|
/* error={errors.confirm} */
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
<Stack
|
<Stack
|
||||||
@@ -268,7 +269,7 @@ const SetNewPassword = () => {
|
|||||||
upper character
|
upper character
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
variant={feedbacks.upper}
|
variant={feedbacks.uppercase}
|
||||||
/>
|
/>
|
||||||
<Check
|
<Check
|
||||||
text={
|
text={
|
||||||
@@ -277,7 +278,18 @@ const SetNewPassword = () => {
|
|||||||
lower character
|
lower character
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
variant={feedbacks.lower}
|
variant={feedbacks.lowercase}
|
||||||
|
/>
|
||||||
|
<Check
|
||||||
|
text={
|
||||||
|
<>
|
||||||
|
<Typography component="span">
|
||||||
|
Confirm password and password
|
||||||
|
</Typography>{" "}
|
||||||
|
must match
|
||||||
|
</>
|
||||||
|
}
|
||||||
|
variant={feedbacks.confirm}
|
||||||
/>
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ const passwordSchema = joi
|
|||||||
return value;
|
return value;
|
||||||
})
|
})
|
||||||
.custom((value, helpers) => {
|
.custom((value, helpers) => {
|
||||||
if (!/[!@#$%^&*]/.test(value)) {
|
if (!/[!?@#$%^&*()\-_=+[\]{};:'",.<>~`|\\/]/.test(value)) {
|
||||||
return helpers.message("special");
|
return helpers.message("special");
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
|
|||||||
Reference in new issue
Block a user