@@ -7,26 +7,17 @@ export default function ChangePasswordModal({ open, setOpen, profileData, onProf
77 handleSubmit,
88 reset,
99 watch,
10- formState : { isSubmitting }
10+ formState : { isSubmitting, errors }
1111 } = useForm ( ) ;
1212
13- // Observar las contraseñas para validar que coincidan
1413 const newPassword = watch ( "newPassword" ) ;
1514
1615 useEffect ( ( ) => {
17- if ( profileData ) {
18- reset ( {
19- oldPassword : "" ,
20- newPassword : "" ,
21- confirmPassword : "" ,
22- } ) ;
23- } else {
24- reset ( {
25- oldPassword : "" ,
26- newPassword : "" ,
27- confirmPassword : "" ,
28- } ) ;
29- }
16+ reset ( {
17+ oldPassword : "" ,
18+ newPassword : "" ,
19+ confirmPassword : "" ,
20+ } ) ;
3021 } , [ profileData , reset ] ) ;
3122
3223 const handleClose = ( ) => {
@@ -36,11 +27,6 @@ export default function ChangePasswordModal({ open, setOpen, profileData, onProf
3627
3728 const handlePassSubmit = async ( data ) => {
3829 try {
39- if ( data . newPassword !== data . confirmPassword ) {
40- alert ( "Your new passwords don't match" ) ;
41- return ;
42- }
43-
4430 if ( onSubmit ) {
4531 await onSubmit ( data ) ;
4632 }
@@ -50,7 +36,6 @@ export default function ChangePasswordModal({ open, setOpen, profileData, onProf
5036 }
5137
5238 handleClose ( ) ;
53-
5439 } catch ( error ) {
5540 console . error ( "Error when you tried change the password: " , error ) ;
5641 }
@@ -61,66 +46,77 @@ export default function ChangePasswordModal({ open, setOpen, profileData, onProf
6146 return (
6247 < div className = "modal modal-open fixed inset-0 flex justify-center items-center z-50" >
6348 < div className = "modal-box max-w-3xl bg-neutral-80 border border-neutral-70 rounded-lg p-6 relative" >
64- < form onSubmit = { handleSubmit ( handlePassSubmit ) } className = "flex flex-col gap-4" >
49+ < div className = "flex flex-col gap-4" >
6550 < h2 className = "text-2xl font-bold text-center" > Change your password</ h2 >
6651
67-
68- { /* Password Viejo*/ }
52+ { /* Current Password */ }
6953 < div className = "form-control" >
7054 < label className = "block text-sm text-neutral-20 mb-1" >
7155 < span className = "label-text font-semibold" > Current password</ span >
7256 </ label >
7357 < input
7458 type = "password"
7559 { ...register ( "oldPassword" , {
76- required : "Your current password is requiered "
60+ required : "Current password is required "
7761 } ) }
7862 placeholder = "Enter your current password"
79- className = "input input-bordered bg-neutral-90 text-neutral-0 border-neutral-60 w-full placeholder-neutral-40 placeholder:italic"
63+ className = "input input-bordered bg-neutral-90 text-neutral-0 border-neutral-60 w-full placeholder-neutral-40 placeholder:italic"
8064 />
65+ { errors . oldPassword && (
66+ < span className = "text-red-500 text-sm mt-1" > { errors . oldPassword . message } </ span >
67+ ) }
8168 </ div >
8269
83- { /* Password nuevo */ }
70+ { /* New Password */ }
8471 < div className = "form-control" >
8572 < label className = "block text-sm text-neutral-20 mb-1" >
8673 < span className = "label-text font-semibold" > New password</ span >
8774 </ label >
8875 < input
8976 type = "password"
9077 { ...register ( "newPassword" , {
91- required : "Your new password is requiered"
78+ required : "New password is required" ,
79+ minLength : {
80+ value : 6 ,
81+ message : "Password must be at least 6 characters"
82+ }
9283 } ) }
9384 placeholder = "Enter your new password"
94- className = "input input-bordered bg-neutral-90 text-neutral-0 border-neutral-60 w-full placeholder-neutral-40 placeholder:italic"
85+ className = "input input-bordered bg-neutral-90 text-neutral-0 border-neutral-60 w-full placeholder-neutral-40 placeholder:italic"
9586 />
87+ { errors . newPassword && (
88+ < span className = "text-red-500 text-sm mt-1" > { errors . newPassword . message } </ span >
89+ ) }
9690 </ div >
9791
98- { /* Password nuevo again */ }
92+ { /* Confirm Password */ }
9993 < div className = "form-control" >
10094 < label className = "block text-sm text-neutral-20 mb-1" >
10195 < span className = "label-text font-semibold" > Confirm new password</ span >
10296 </ label >
10397 < input
10498 type = "password"
10599 { ...register ( "confirmPassword" , {
106- required : "Confirm your new password" ,
107- validate : value => value === newPassword
100+ required : "Please confirm your password" ,
101+ validate : value => value === newPassword || "Passwords don't match"
108102 } ) }
109103 placeholder = "Confirm your new password"
110- className = "input input-bordered bg-neutral-90 text-neutral-0 border-neutral-60 w-full placeholder-neutral-40 placeholder:italic"
104+ className = "input input-bordered bg-neutral-90 text-neutral-0 border-neutral-60 w-full placeholder-neutral-40 placeholder:italic"
111105 />
106+ { errors . confirmPassword && (
107+ < span className = "text-red-500 text-sm mt-1" > { errors . confirmPassword . message } </ span >
108+ ) }
112109 </ div >
113110
114-
115-
116111 { /* Buttons */ }
117112 < div className = "flex justify-end gap-4 pt-4" >
118113 < button
119114 type = "submit"
120115 disabled = { isSubmitting }
121- className = "btn bg-primary-60 text-neutral-0 hover:bg-primary-50 border border-primary-50"
116+ className = "btn bg-primary-60 text-neutral-0 hover:bg-primary-50 border border-primary-50 disabled:opacity-50"
117+ onClick = { handleSubmit ( handlePassSubmit ) }
122118 >
123- { isSubmitting ? "Changing..." : "Change your password" }
119+ { isSubmitting ? "Changing..." : "Change password" }
124120 </ button >
125121 < button
126122 type = "button"
@@ -130,7 +126,7 @@ export default function ChangePasswordModal({ open, setOpen, profileData, onProf
130126 Cancel
131127 </ button >
132128 </ div >
133- </ form >
129+ </ div >
134130 </ div >
135131 </ div >
136132 ) ;
0 commit comments