From d8eb7b72248d56f879e389fdf387e0e32a288ece Mon Sep 17 00:00:00 2001 From: Pinx0 Date: Sat, 9 Nov 2024 21:13:59 +0100 Subject: [PATCH] fix: update form data on user change (from null to a value) --- .../HelpRequests/HelpRequestForm.tsx | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/components/HelpRequests/HelpRequestForm.tsx b/src/components/HelpRequests/HelpRequestForm.tsx index 0fadd05..aedb6da 100644 --- a/src/components/HelpRequests/HelpRequestForm.tsx +++ b/src/components/HelpRequests/HelpRequestForm.tsx @@ -1,6 +1,6 @@ 'use client'; -import { ChangeEvent, FormEvent, useCallback, useState } from 'react'; +import { ChangeEvent, FormEvent, useCallback, useEffect, useState } from 'react'; import { isValidPhone } from '@/helpers/utils'; import { tiposAyudaArray } from '@/helpers/constants'; import { PhoneInput } from '@/components/input/PhoneInput'; @@ -59,6 +59,24 @@ export default function HelpRequestForm({ town_id: request?.town_id || 0, }); + useEffect(() => { + setFormData({ + nombre: request?.name || user?.user_metadata?.full_name || user?.user_metadata?.nombre || '', + telefono: request?.contact_info || user?.user_metadata?.telefono || '', + ubicacion: request?.location || '', + coordinates: { lat: 0, lng: 0 }, + tiposAyuda: request?.help_type || [], + numeroPersonas: request?.number_of_people || 1, + descripcion: request?.description || '', + urgencia: request?.urgency === 'alta' ? 'alta' : request?.urgency === 'media' ? 'media' : 'baja', + situacionEspecial: request?.additional_info?.special_situations || '', + consentimiento: request?.additional_info?.consent || false, + pueblo: '', + status: request?.status || 'active', + town_id: request?.town_id || 0, + }); + }, [user, request]); + const handleTipoAyudaChange = (tipo: HelpRequestHelpType) => { setFormData((prev) => ({ ...prev,