From db5465032e4eb061078b27c36cf814e300c723d4 Mon Sep 17 00:00:00 2001 From: "Roberto.silva" Date: Wed, 4 Feb 2026 16:05:05 -0600 Subject: [PATCH] =?UTF-8?q?Guardado=20autom=C3=A1tico=20fix=20#53?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/planes/$planId/_detalle/index.tsx | 60 +++++++++++++++++++- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/src/routes/planes/$planId/_detalle/index.tsx b/src/routes/planes/$planId/_detalle/index.tsx index d4582e4..1a8e457 100644 --- a/src/routes/planes/$planId/_detalle/index.tsx +++ b/src/routes/planes/$planId/_detalle/index.tsx @@ -112,15 +112,68 @@ function DatosGeneralesPage() { }, [data]) // 3. Manejadores de acciones (Ahora como funciones locales) - const handleEdit = (campo: DatosGeneralesField) => { - setEditingId(campo.id) - setEditValue(campo.value) + const handleEdit = (nuevoCampo: DatosGeneralesField) => { + // 1. SI YA ESTÁBAMOS EDITANDO OTRO CAMPO, GUARDAMOS EL ANTERIOR PRIMERO + if (editingId && editingId !== nuevoCampo.id) { + const campoAnterior = campos.find((c) => c.id === editingId) + if (campoAnterior && editValue !== campoAnterior.value) { + // Solo guardamos si el valor realmente cambió + ejecutarGuardadoSilencioso(campoAnterior, editValue) + } + } + + // 2. ABRIMOS EL NUEVO CAMPO + setEditingId(nuevoCampo.id) + setEditValue(nuevoCampo.value) } const handleCancel = () => { setEditingId(null) setEditValue('') } + // Función auxiliar para procesar los datos (fuera o dentro del componente) + const prepararDatosActualizados = ( + data: any, + campo: DatosGeneralesField, + valor: string, + ) => { + const currentValue = data.datos[campo.clave] + let newValue: any + + if ( + typeof currentValue === 'object' && + currentValue !== null && + 'description' in currentValue + ) { + newValue = { ...currentValue, description: valor } + } else { + newValue = valor + } + + return { + ...data.datos, + [campo.clave]: newValue, + } + } + + const ejecutarGuardadoSilencioso = ( + campo: DatosGeneralesField, + valor: string, + ) => { + if (!data?.datos) return + + const datosActualizados = prepararDatosActualizados(data, campo, valor) + + updatePlan.mutate({ + planId, + patch: { datos: datosActualizados }, + }) + + // Actualizar UI localmente + setCampos((prev) => + prev.map((c) => (c.id === campo.id ? { ...c, value: valor } : c)), + ) + } const handleSave = (campo: DatosGeneralesField) => { if (!data?.datos) return @@ -161,6 +214,7 @@ function DatosGeneralesPage() { prev.map((c) => (c.id === campo.id ? { ...c, value: editValue } : c)), ) + ejecutarGuardadoSilencioso(campo, editValue) setEditingId(null) setEditValue('') }