|
|
@@ -12,6 +12,14 @@ interface FormData {
|
|
|
currentPassword: string
|
|
|
newPassword: string
|
|
|
confirmPassword: string
|
|
|
+ phone?: string
|
|
|
+ dateOfBirth?: string
|
|
|
+ gender?: string
|
|
|
+ address?: string
|
|
|
+ emergencyContact?: string
|
|
|
+ medicalHistory?: string
|
|
|
+ allergies?: string
|
|
|
+ currentMedications?: string
|
|
|
}
|
|
|
|
|
|
export function useAccountForm() {
|
|
|
@@ -24,7 +32,15 @@ export function useAccountForm() {
|
|
|
email: "",
|
|
|
currentPassword: "",
|
|
|
newPassword: "",
|
|
|
- confirmPassword: ""
|
|
|
+ confirmPassword: "",
|
|
|
+ phone: "",
|
|
|
+ dateOfBirth: "",
|
|
|
+ gender: "",
|
|
|
+ address: "",
|
|
|
+ emergencyContact: "",
|
|
|
+ medicalHistory: "",
|
|
|
+ allergies: "",
|
|
|
+ currentMedications: ""
|
|
|
})
|
|
|
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
@@ -69,7 +85,15 @@ export function useAccountForm() {
|
|
|
name: session.user.name || "",
|
|
|
lastname: session.user.lastname || "",
|
|
|
email: session.user.email || "",
|
|
|
- identificacion: session.user.identificacion || ""
|
|
|
+ identificacion: session.user.identificacion || "",
|
|
|
+ phone: session.user.phone || "",
|
|
|
+ dateOfBirth: session.user.dateOfBirth ? new Date(session.user.dateOfBirth).toISOString().split('T')[0] : "",
|
|
|
+ gender: session.user.gender || "",
|
|
|
+ address: session.user.address || "",
|
|
|
+ emergencyContact: session.user.emergencyContact || "",
|
|
|
+ medicalHistory: session.user.medicalHistory || "",
|
|
|
+ allergies: session.user.allergies || "",
|
|
|
+ currentMedications: session.user.currentMedications || ""
|
|
|
}))
|
|
|
}
|
|
|
}, [session])
|
|
|
@@ -81,7 +105,7 @@ export function useAccountForm() {
|
|
|
}
|
|
|
}, [session?.user?.id, imageLoaded, loadProfileImage])
|
|
|
|
|
|
- const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
+ const handleInputChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>) => {
|
|
|
const { name, value } = e.target
|
|
|
|
|
|
setFormData(prev => ({
|
|
|
@@ -105,10 +129,42 @@ export function useAccountForm() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- const handleImageRemove = () => {
|
|
|
- setProfileImage(null)
|
|
|
- setImageFile(null)
|
|
|
- setImageLoaded(false)
|
|
|
+ const handleImageRemove = async () => {
|
|
|
+ if (!profileImage) return
|
|
|
+
|
|
|
+ setLoadingImage(true)
|
|
|
+ try {
|
|
|
+ const response = await fetch("/api/account/profile-image", {
|
|
|
+ method: 'DELETE',
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ if (response.ok) {
|
|
|
+ setProfileImage(null)
|
|
|
+ setImageFile(null)
|
|
|
+ setImageLoaded(false)
|
|
|
+
|
|
|
+ // Actualizar el contexto global
|
|
|
+ updateProfileImage(null)
|
|
|
+
|
|
|
+ // Actualizar la sesión
|
|
|
+ await update({
|
|
|
+ profileImage: null
|
|
|
+ })
|
|
|
+
|
|
|
+ toast.success("Imagen de perfil eliminada correctamente")
|
|
|
+ } else {
|
|
|
+ const data = await response.json()
|
|
|
+ toast.error(data.error || "Error al eliminar la imagen")
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error("Error eliminando imagen:", error)
|
|
|
+ toast.error("Error al eliminar la imagen de perfil")
|
|
|
+ } finally {
|
|
|
+ setLoadingImage(false)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
|
@@ -142,6 +198,37 @@ export function useAccountForm() {
|
|
|
formDataToSend.append('newPassword', formData.newPassword)
|
|
|
}
|
|
|
|
|
|
+ // Agregar campos médicos y personales
|
|
|
+ if (formData.phone) {
|
|
|
+ formDataToSend.append('phone', formData.phone)
|
|
|
+ }
|
|
|
+
|
|
|
+ // dateOfBirth NO se envía - es provisto por la API UTB y no es editable
|
|
|
+
|
|
|
+ if (formData.gender) {
|
|
|
+ formDataToSend.append('gender', formData.gender)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (formData.address) {
|
|
|
+ formDataToSend.append('address', formData.address)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (formData.emergencyContact) {
|
|
|
+ formDataToSend.append('emergencyContact', formData.emergencyContact)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (formData.medicalHistory) {
|
|
|
+ formDataToSend.append('medicalHistory', formData.medicalHistory)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (formData.allergies) {
|
|
|
+ formDataToSend.append('allergies', formData.allergies)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (formData.currentMedications) {
|
|
|
+ formDataToSend.append('currentMedications', formData.currentMedications)
|
|
|
+ }
|
|
|
+
|
|
|
if (imageFile) {
|
|
|
formDataToSend.append('profileImage', imageFile)
|
|
|
}
|
|
|
@@ -154,7 +241,7 @@ export function useAccountForm() {
|
|
|
const data = await response.json()
|
|
|
|
|
|
if (response.ok) {
|
|
|
- toast.success("Perfil actualizado correctamente. Serás redirigido para iniciar sesión nuevamente.")
|
|
|
+ toast.success("Perfil actualizado correctamente.")
|
|
|
|
|
|
// Limpiar contraseñas
|
|
|
setFormData(prev => ({
|
|
|
@@ -164,12 +251,20 @@ export function useAccountForm() {
|
|
|
confirmPassword: ""
|
|
|
}))
|
|
|
|
|
|
- // Actualizar la sesión de NextAuth con los nuevos datos
|
|
|
+ // Actualizar la sesión de NextAuth con todos los datos nuevos
|
|
|
await update({
|
|
|
name: data.user.name,
|
|
|
lastname: data.user.lastname,
|
|
|
email: data.user.email,
|
|
|
- profileImage: data.user.profileImage
|
|
|
+ profileImage: data.user.profileImage,
|
|
|
+ phone: data.user.phone,
|
|
|
+ dateOfBirth: data.user.dateOfBirth,
|
|
|
+ gender: data.user.gender,
|
|
|
+ address: data.user.address,
|
|
|
+ emergencyContact: data.user.emergencyContact,
|
|
|
+ medicalHistory: data.user.medicalHistory,
|
|
|
+ allergies: data.user.allergies,
|
|
|
+ currentMedications: data.user.currentMedications
|
|
|
})
|
|
|
|
|
|
// Actualizar la imagen de perfil en el contexto global
|
|
|
@@ -184,11 +279,6 @@ export function useAccountForm() {
|
|
|
loadProfileImage()
|
|
|
}, 500)
|
|
|
}
|
|
|
-
|
|
|
- // Redirigir a login después de 2 segundos
|
|
|
- setTimeout(() => {
|
|
|
- router.push("/auth/login")
|
|
|
- }, 2000)
|
|
|
} else {
|
|
|
toast.error(data.error || "Error al actualizar el perfil")
|
|
|
}
|