11import React , { useEffect } from "react" ;
22import { useForm } from "react-hook-form"
3- import { updateProfile } from "../../services/profileService" ;
43
5- export default function RemoveAccountModal ( { open, setOpen, profileData, onProfileUpdate, onSubmit } ) {
4+ export default function RemoveAccountModal ( { open, setOpen, profileData, onSubmit } ) {
5+
66 const {
77 register,
88 handleSubmit,
9- control,
109 reset,
11- watch,
12- formState : { isSubmitting }
10+ formState : { isSubmitting, errors }
1311 } = useForm ( ) ;
1412
15- // Observar las contraseñas para validar que coincidan
16- const newPassword = watch ( "newPassword" ) ;
17-
1813 useEffect ( ( ) => {
19- if ( profileData ) {
20- reset ( {
21- oldPassword : "" ,
22- newPassword : "" ,
23- confirmPassword : "" ,
24- } ) ;
25- } else {
14+ if ( open ) {
2615 reset ( {
27- oldPassword : "" ,
28- newPassword : "" ,
29- confirmPassword : "" ,
16+ email : "" ,
3017 } ) ;
3118 }
32- } , [ profileData , reset ] ) ;
19+ } , [ open , reset ] ) ;
3320
3421 const handleClose = ( ) => {
3522 reset ( ) ;
3623 setOpen ( false ) ;
3724 } ;
3825
39- const handlePassSubmit = async ( data ) => {
26+ const handleDeleteSubmit = async ( data ) => {
4027 try {
41- if ( data . newPassword !== data . confirmPassword ) {
42- alert ( "Your new passwords don't match" ) ;
28+ // Debug: ver qué emails estamos comparando
29+ console . log ( "Email ingresado:" , data . email ) ;
30+ console . log ( "Email del perfil:" , profileData ?. email ) ;
31+
32+ // Verificar que el email coincida con el del usuario
33+ const inputEmail = data . email ?. trim ( ) . toLowerCase ( ) ;
34+ const profileEmail = profileData ?. email ?. trim ( ) . toLowerCase ( ) ;
35+
36+ if ( inputEmail !== profileEmail ) {
37+ alert ( `The email doesn't match your account email. Expected: ${ profileData ?. email } ` ) ;
4338 return ;
4439 }
4540
41+ // Llamar a onSubmit (igual que en updatePassword)
4642 if ( onSubmit ) {
4743 await onSubmit ( data ) ;
4844 }
4945
50- if ( onProfileUpdate ) {
51- onProfileUpdate ( { password : data . newPassword } ) ;
52- }
53-
5446 handleClose ( ) ;
55-
47+
5648 } catch ( error ) {
57- console . error ( "Error when you tried change the password: " , error ) ;
49+ console . error ( "Error when trying to delete account:" , error ) ;
50+ alert ( "Error deleting account. Please try again." ) ;
5851 }
5952 }
6053
@@ -63,60 +56,46 @@ export default function RemoveAccountModal({ open, setOpen, profileData, onProfi
6356 return (
6457 < div className = "modal modal-open fixed inset-0 flex justify-center items-center z-50" >
6558 < div className = "modal-box max-w-3xl bg-neutral-80 border border-neutral-70 rounded-lg p-6 relative" >
66- < form onSubmit = { handleSubmit ( handlePassSubmit ) } className = "flex flex-col gap-4" >
67- < h2 className = "text-2xl font-bold text-center" > Remove your account</ h2 >
68-
69-
70-
71-
72- { /*Email*/ }
73- < div className = "form-control" >
74- < label className = "block text-sm text-neutral-20 mb-1" >
75- < span className = "label-text font-semibold" > Email</ span >
76- </ label >
77- < input
78- type = "password"
79- { ...register ( "newPassword" , {
80- required : "Your email is requiered"
81- } ) }
82- placeholder = "Enter your email"
83- className = "input input-bordered bg-neutral-90 text-neutral-0 border-neutral-60 w-full placeholder-neutral-40 placeholder:italic"
84- />
59+ < form onSubmit = { handleSubmit ( handleDeleteSubmit ) } className = "flex flex-col gap-4" >
60+ < h2 className = "text-2xl font-bold text-center text-red-500" > Remove your account</ h2 >
61+
62+ < div className = "bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4" >
63+ < strong > Warning:</ strong > This action cannot be undone. Your account and all associated data will be permanently deleted.
8564 </ div >
86-
87- { /* Password */ }
65+
66+ { /* Email */ }
8867 < div className = "form-control" >
8968 < label className = "block text-sm text-neutral-20 mb-1" >
90- < span className = "label-text font-semibold" > Password </ span >
69+ < span className = "label-text font-semibold" > Confirm your email </ span >
9170 </ label >
9271 < input
93- type = "password"
94- { ...register ( "Password" , {
95- required : "Your password is requiered" ,
96- validate : value => value === newPassword
72+ type = "email"
73+ { ...register ( "email" , {
74+ required : "Your email is required"
9775 } ) }
98- placeholder = "Enter your password"
99- className = "input input-bordered bg-neutral-90 text-neutral-0 border-neutral-60 w-full placeholder-neutral-40 placeholder:italic"
76+ placeholder = "Enter your email to confirm"
77+ className = "input input-bordered bg-neutral-90 text-neutral-0 border-neutral-60 w-full placeholder-neutral-40 placeholder:italic"
10078 />
79+ { errors . email && (
80+ < span className = "text-red-500 text-sm mt-1" > { errors . email . message } </ span >
81+ ) }
10182 </ div >
102-
103-
104-
83+
10584 { /* Buttons */ }
10685 < div className = "flex justify-end gap-4 pt-4" >
10786 < button
108- type = "submit"
109- disabled = { isSubmitting }
110- className = "btn bg-red-600 text-neutral-0 hover:bg-red-500 border "
87+ type = "button"
88+ onClick = { handleClose }
89+ className = "btn bg-neutral-90 border border-neutral-70 text-neutral-0 hover:text-primary-40 "
11190 >
112- { isSubmitting ? "Changing..." : "Remove your account" }
91+ Cancel
11392 </ button >
11493 < button
115- type = "button"
116- onClick = { handleClose }
117- className = "btn bg-neutral-90 border border-neutral-70 text-neutral-0 hover:text-primary-40 "
94+ type = "submit"
95+ disabled = { isSubmitting }
96+ className = "btn bg-red-600 text-neutral-0 hover:bg-red-700 border "
11897 >
119- Cancel
98+ { isSubmitting ? "Deleting..." : "Delete Account Permanently" }
12099 </ button >
121100 </ div >
122101 </ form >
0 commit comments