From 35be0705defd5cc9c0d2a09c6043e56226ee97bf Mon Sep 17 00:00:00 2001 From: Dennis Kigen Date: Wed, 20 Nov 2024 15:50:41 +0300 Subject: [PATCH] (fix) Fix change password modal This PR fixes the change password modal as follows: - Adds default values to the change password form schema - this is to prevent the form from being empty when the user clicks on the change password button. - Adds min validation to the old password, new password and password confirmation fields to ensure that the user has entered a value. - Adds the appropriate dependencies to the submit handler useCallback hook. - Surfaces the error message returned from the change password API. - Fixes a translation key/value pair. --- .../change-password.component.tsx | 81 ++++++++++++------- .../change-password/change-password.modal.tsx | 79 +++++++++++------- .../change-password/change-password.test.tsx | 31 +++---- .../apps/esm-login-app/translations/en.json | 1 + 4 files changed, 116 insertions(+), 76 deletions(-) diff --git a/packages/apps/esm-login-app/src/change-password/change-password.component.tsx b/packages/apps/esm-login-app/src/change-password/change-password.component.tsx index 64f3864a1..c63376f0d 100644 --- a/packages/apps/esm-login-app/src/change-password/change-password.component.tsx +++ b/packages/apps/esm-login-app/src/change-password/change-password.component.tsx @@ -13,17 +13,29 @@ const ChangePassword: React.FC = () => { const { t } = useTranslation(); const [isChangingPassword, setIsChangingPassword] = useState(false); - const oldPasswordValidation = z.string({ - required_error: t('oldPasswordRequired', 'Old password is required'), - }); + const oldPasswordValidation = z + .string({ + required_error: t('oldPasswordRequired', 'Old password is required'), + }) + .min(1, { + message: t('oldPasswordRequired', 'Old password is required'), + }); - const newPasswordValidation = z.string({ - required_error: t('newPasswordRequired', 'New password is required'), - }); + const newPasswordValidation = z + .string({ + required_error: t('newPasswordRequired', 'New password is required'), + }) + .min(1, { + message: t('newPasswordRequired', 'New password is required'), + }); - const passwordConfirmationValidation = z.string({ - required_error: t('passwordConfirmationRequired', 'Password confirmation is required'), - }); + const passwordConfirmationValidation = z + .string({ + required_error: t('passwordConfirmationRequired', 'Password confirmation is required'), + }) + .min(1, { + message: t('passwordConfirmationRequired', 'Password confirmation is required'), + }); const changePasswordFormSchema = z .object({ @@ -42,31 +54,40 @@ const ChangePassword: React.FC = () => { formState: { errors }, } = useForm({ resolver: zodResolver(changePasswordFormSchema), + defaultValues: { + oldPassword: '', + newPassword: '', + passwordConfirmation: '', + }, }); - const onSubmit: SubmitHandler> = useCallback((data) => { - setIsChangingPassword(true); + const onSubmit: SubmitHandler> = useCallback( + (data) => { + setIsChangingPassword(true); - const { oldPassword, newPassword } = data; + const { oldPassword, newPassword } = data; - changeUserPassword(oldPassword, newPassword) - .then(() => { - showSnackbar({ - title: t('passwordChangedSuccessfully', 'Password changed successfully'), - kind: 'success', + changeUserPassword(oldPassword, newPassword) + .then(() => { + showSnackbar({ + title: t('passwordChangedSuccessfully', 'Password changed successfully'), + kind: 'success', + }); + }) + .catch((error) => { + const errorMessage = error?.responseBody?.message ?? error?.message; + showSnackbar({ + kind: 'error', + subtitle: errorMessage, + title: t('errorChangingPassword', 'Error changing password'), + }); + }) + .finally(() => { + setIsChangingPassword(false); }); - }) - .catch((error) => { - showSnackbar({ - kind: 'error', - subtitle: error?.message, - title: t('errorChangingPassword', 'Error changing password'), - }); - }) - .finally(() => { - setIsChangingPassword(false); - }); - }, []); + }, + [t], + ); const onError = useCallback(() => setIsChangingPassword(false), []); @@ -121,7 +142,7 @@ const ChangePassword: React.FC = () => { />