fix: backend validation password

This commit is contained in:
Caio Cabral committed 2024-10-31 09:30:43 -04:00
1 parent 686217944d
commit fd38a9d7eb
5 files changed
+127 -116

No files matched your search

@@ -1,6 +1,21 @@
import { useState } from "react";
import { useMemo, useState } from "react";
import { credentials } from "../../../Validation/validation";
const getFeedbackStatus = (form, errors, field, criteria) => {
const fieldErrors = errors[field];
const isFieldEmpty = form[field].length === 0;
const hasError = fieldErrors?.includes(criteria) || fieldErrors?.includes("empty");
const isCorrect = !isFieldEmpty && !hasError;
if (isCorrect) {
return "success";
} else if (hasError) {
return "error";
} else {
return "info";
}
};
function useValidatePassword() {
const [errors, setErrors] = useState({});
const [form, setForm] = useState({
@@ -38,29 +53,17 @@ function useValidatePassword() {
setErrors(() => (errorsByPath ? { ...errorsByPath } : {}));
};
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;
if (isCorrect) {
return "success";
} else if (hasError) {
return "error";
} else {
return "info";
}
};
const feedbacks = {
length: getFeedbackStatus("password", "length"),
special: getFeedbackStatus("password", "special"),
number: getFeedbackStatus("password", "number"),
uppercase: getFeedbackStatus("password", "uppercase"),
lowercase: getFeedbackStatus("password", "lowercase"),
confirm: getFeedbackStatus("confirm", "different"),
};
const feedbacks = useMemo(
() => ({
length: getFeedbackStatus(form, errors, "password", "length"),
special: getFeedbackStatus(form, errors, "password", "special"),
number: getFeedbackStatus(form, errors, "password", "number"),
uppercase: getFeedbackStatus(form, errors, "password", "uppercase"),
lowercase: getFeedbackStatus(form, errors, "password", "lowercase"),
confirm: getFeedbackStatus(form, errors, "confirm", "different"),
}),
[form, errors]
);
return { handleChange, feedbacks, form, errors };
}