fix: erros on console on dialogs

This commit is contained in:
Caio Cabral committed 2024-11-03 06:50:49 -05:00
1 parent 4f647028e7
commit 3709e09ef7
6 files changed
+34 -42

No files matched your search

@@ -12,7 +12,7 @@ const GenericDialog = ({ title, description, open, onClose, theme, children }) =
aria-describedby={ariaDescribedBy} aria-describedby={ariaDescribedBy}
open={open} open={open}
onClose={onClose} onClose={onClose}
onClick={(e)=>e.stopPropagation()} onClick={(e) => e.stopPropagation()}
> >
<Stack <Stack
gap={theme.spacing(2)} gap={theme.spacing(2)}
@@ -62,7 +62,8 @@ GenericDialog.propTypes = {
open: PropTypes.bool.isRequired, open: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired, onClose: PropTypes.func.isRequired,
theme: PropTypes.object.isRequired, theme: PropTypes.object.isRequired,
children: PropTypes.element.isRequired, children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node])
.isRequired,
}; };
export { GenericDialog }; export { GenericDialog };
-1
View File
@@ -51,7 +51,6 @@ Dialog.propTypes = {
title: PropTypes.string.isRequired, title: PropTypes.string.isRequired,
description: PropTypes.string, description: PropTypes.string,
open: PropTypes.bool.isRequired, open: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired,
theme: PropTypes.object.isRequired, theme: PropTypes.object.isRequired,
onCancel: PropTypes.func.isRequired, onCancel: PropTypes.func.isRequired,
confirmationButtonLabel: PropTypes.string.isRequired, confirmationButtonLabel: PropTypes.string.isRequired,
@@ -29,15 +29,6 @@ const PasswordPanel = () => {
"edit-confirm-password": "confirm", "edit-confirm-password": "confirm",
}; };
const errorsMap = {
length: "Must be at least 8 characters long",
uppercase: "Must contain at least one upper character",
lowercase: "Must contain at least one lower character",
number: "Must contain at least one number",
special: "Must contain at least one special character",
different: "Confirm password and password must match",
};
const [localData, setLocalData] = useState({ const [localData, setLocalData] = useState({
password: "", password: "",
newPassword: "", newPassword: "",
@@ -151,7 +142,7 @@ const PasswordPanel = () => {
autoComplete="current-password" autoComplete="current-password"
value={localData.password} value={localData.password}
onChange={handleChange} onChange={handleChange}
error={errorsMap[errors[idToName["edit-current-password"]]]} error={errors[idToName["edit-current-password"]]}
/> />
</Stack> </Stack>
<Stack <Stack
@@ -174,7 +165,7 @@ const PasswordPanel = () => {
autoComplete="new-password" autoComplete="new-password"
value={localData.newPassword} value={localData.newPassword}
onChange={handleChange} onChange={handleChange}
error={errorsMap[errors[idToName["edit-new-password"]]]} error={errors[idToName["edit-new-password"]]}
/> />
</Stack> </Stack>
<Stack <Stack
@@ -197,7 +188,7 @@ const PasswordPanel = () => {
autoComplete="new-password" autoComplete="new-password"
value={localData.confirm} value={localData.confirm}
onChange={handleChange} onChange={handleChange}
error={errorsMap[errors[idToName["edit-confirm-password"]]]} error={errors[idToName["edit-confirm-password"]]}
/> />
</Stack> </Stack>
{Object.keys(errors).length > 0 && ( {Object.keys(errors).length > 0 && (
@@ -218,7 +209,10 @@ const PasswordPanel = () => {
type="submit" type="submit"
loading={isLoading} loading={isLoading}
loadingIndicator="Saving..." loadingIndicator="Saving..."
disabled={Object.keys(errors).length !== 0 && true} disabled={
Object.keys(errors).length !== 0 ||
Object.values(localData).filter((value) => value !== "").length === 0
}
sx={{ sx={{
px: theme.spacing(12), px: theme.spacing(12),
mt: theme.spacing(20), mt: theme.spacing(20),
+1 -9
View File
@@ -232,14 +232,6 @@ const StepTwo = ({ form, errors, onSubmit, onChange, onBack }) => {
const navigate = useNavigate(); const navigate = useNavigate();
const inputRef = useRef(null); const inputRef = useRef(null);
const errorsMap = {
length: "Must be at least 8 characters long",
uppercase: "Must contain at least one upper character",
lowercase: "Must contain at least one lower character",
number: "Must contain at least one number",
special: "Must contain at least one special character",
};
useEffect(() => { useEffect(() => {
if (inputRef.current) { if (inputRef.current) {
inputRef.current.focus(); inputRef.current.focus();
@@ -285,7 +277,7 @@ const StepTwo = ({ form, errors, onSubmit, onChange, onBack }) => {
autoComplete="current-password" autoComplete="current-password"
value={form.password} value={form.password}
onChange={onChange} onChange={onChange}
error={errorsMap[errors.password]} error={errors.password}
ref={inputRef} ref={inputRef}
/> />
<Stack <Stack
@@ -39,15 +39,15 @@ function useValidatePassword() {
const { error } = credentials.validate(validateValue, validateOptions); const { error } = credentials.validate(validateValue, validateOptions);
const errors = error?.details.map((error) => ({ const errors = error?.details.map((error) => ({
path: error.path[0], path: error.path[0],
message: error.message, type: error.type,
})); }));
const errorsByPath = const errorsByPath =
errors && errors &&
errors.reduce((acc, { path, message }) => { errors.reduce((acc, { path, type }) => {
if (!acc[path]) { if (!acc[path]) {
acc[path] = []; acc[path] = [];
} }
acc[path].push(message); acc[path].push(type);
return acc; return acc;
}, {}); }, {});
setErrors(() => (errorsByPath ? { ...errorsByPath } : {})); setErrors(() => (errorsByPath ? { ...errorsByPath } : {}));
@@ -55,7 +55,7 @@ function useValidatePassword() {
const feedbacks = useMemo( const feedbacks = useMemo(
() => ({ () => ({
length: getFeedbackStatus(form, errors, "password", "length"), length: getFeedbackStatus(form, errors, "password", "string.min"),
special: getFeedbackStatus(form, errors, "password", "special"), special: getFeedbackStatus(form, errors, "password", "special"),
number: getFeedbackStatus(form, errors, "password", "number"), number: getFeedbackStatus(form, errors, "password", "number"),
uppercase: getFeedbackStatus(form, errors, "password", "uppercase"), uppercase: getFeedbackStatus(form, errors, "password", "uppercase"),
+19 -13
View File
@@ -16,34 +16,39 @@ const passwordSchema = joi
.string() .string()
.trim() .trim()
.min(8) .min(8)
.messages({
"string.empty": "empty",
"string.min": "length",
})
.custom((value, helpers) => { .custom((value, helpers) => {
if (!/[A-Z]/.test(value)) { if (!/[A-Z]/.test(value)) {
return helpers.message("uppercase"); return helpers.error("uppercase");
} }
return value; return value;
}) })
.custom((value, helpers) => { .custom((value, helpers) => {
if (!/[a-z]/.test(value)) { if (!/[a-z]/.test(value)) {
return helpers.message("lowercase"); return helpers.error("lowercase");
} }
return value; return value;
}) })
.custom((value, helpers) => { .custom((value, helpers) => {
if (!/\d/.test(value)) { if (!/\d/.test(value)) {
return helpers.message("number"); return helpers.error("number");
} }
return value; return value;
}) })
.custom((value, helpers) => { .custom((value, helpers) => {
if (!/[!?@#$%^&*()\-_=+[\]{};:'",.<>~`|\\/]/.test(value)) { if (!/[!?@#$%^&*()\-_=+[\]{};:'",.<>~`|\\/]/.test(value)) {
return helpers.message("special"); return helpers.error("special");
} }
return value; return value;
})
.messages({
"string.empty": "Password is required",
"string.min": "Password must be at least 8 characters long",
uppercase: "Password must contain at least one uppercase letter",
lowercase: "Password must contain at least one lowercase letter",
number: "Password must contain at least one number",
special: "Password must contain at least one special character",
}); });
const credentials = joi.object({ const credentials = joi.object({
firstName: nameSchema, firstName: nameSchema,
lastName: nameSchema, lastName: nameSchema,
@@ -67,15 +72,16 @@ const credentials = joi.object({
confirm: joi confirm: joi
.string() .string()
.trim() .trim()
.messages({
"string.empty": "empty",
})
.custom((value, helpers) => { .custom((value, helpers) => {
const { password } = helpers.prefs.context; const { password } = helpers.prefs.context;
if (value !== password) { if (value !== password) {
return helpers.message("different"); return helpers.error("different");
} }
return value; return value;
})
.messages({
"string.empty": "empty",
different: "Passwords do not match",
}), }),
role: joi.array(), role: joi.array(),
teamId: joi.string().allow("").optional(), teamId: joi.string().allow("").optional(),
@@ -173,5 +179,5 @@ export {
monitorValidation, monitorValidation,
settingsValidation, settingsValidation,
maintenanceWindowValidation, maintenanceWindowValidation,
advancedSettingsValidation advancedSettingsValidation,
}; };