From ea4284a363f352ab728f6c9a4bf8d5f2aba3ae78 Mon Sep 17 00:00:00 2001 From: Caio Cabral Date: Sat, 26 Oct 2024 19:20:53 -0400 Subject: [PATCH] feat: Check for confirm password if password changes and confirm is filled, and they do not match anymore --- .chores.md | 2 ++ Client/src/Pages/Auth/SetNewPassword.jsx | 24 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/.chores.md b/.chores.md index 1b45b2361..b10e56264 100644 --- a/.chores.md +++ b/.chores.md @@ -11,3 +11,5 @@ - [x] Must contain at least one lower character - [x] Pass mismatching password user feedback as the other messages, for consistency - [ ] Get rid of toast message +- [x] Check for confirm password if password changes and confirm is filled, and they do not match anymore +- [ ] Check for confirm password if password changes, and they now match diff --git a/Client/src/Pages/Auth/SetNewPassword.jsx b/Client/src/Pages/Auth/SetNewPassword.jsx index 63ba3af1c..103dff4d5 100644 --- a/Client/src/Pages/Auth/SetNewPassword.jsx +++ b/Client/src/Pages/Auth/SetNewPassword.jsx @@ -79,15 +79,35 @@ const SetNewPassword = () => { }; const handleChange = (event) => { + //update form state const { value, name } = event.target; setForm((prev) => ({ ...prev, [name]: value })); + //validate const validateValue = { [name]: value }; const validateOptions = { abortEarly: false, context: { password: form.password } }; + if (name === "password" && form.confirm.length > 0) { + validateValue.confirm = form.confirm; + validateOptions.context = value; + } const { error } = credentials.validate(validateValue, validateOptions); - const errors = error?.details.map((error) => error.message); - setErrors((prev) => ({ ...prev, [name]: errors })); + //update errors + //fazer o map com path e message + const errors = error?.details.map((error) => ({ + message: error.message, + path: error.path[0], + })); + const errorsByPath = errors + ? errors.reduce((acc, { path, message }) => { + if (!acc[path]) { + acc[path] = []; + } + acc[path].push(message); + return acc; + }, {}) + : { [name]: [] }; + setErrors((prev) => ({ ...prev, ...errorsByPath })); }; const getFeedbackStatus = (field, criteria) => {