From 50c00293cd1b906290a2798a6052e297b781f94c Mon Sep 17 00:00:00 2001 From: "Roberto.silva" Date: Wed, 18 Feb 2026 15:39:24 -0600 Subject: [PATCH] Persistencia en columnas de plan fix #113 --- .../planes/detalle/Ia/ImprovementCard.tsx | 66 +++++++-- src/data/api/ai.api.ts | 20 ++- src/data/hooks/useAI.ts | 22 ++- src/routes/planes/$planId/_detalle/iaplan.tsx | 134 +++++++++--------- src/routes/planes/$planId/_detalle/index.tsx | 1 + 5 files changed, 157 insertions(+), 86 deletions(-) diff --git a/src/components/planes/detalle/Ia/ImprovementCard.tsx b/src/components/planes/detalle/Ia/ImprovementCard.tsx index a4219e0..67114b2 100644 --- a/src/components/planes/detalle/Ia/ImprovementCard.tsx +++ b/src/components/planes/detalle/Ia/ImprovementCard.tsx @@ -1,46 +1,92 @@ -import { Check } from 'lucide-react' +import { Check, Loader2 } from 'lucide-react' import { useState } from 'react' import { Button } from '@/components/ui/button' +import { useUpdatePlanFields } from '@/data' // Tu hook existente export const ImprovementCard = ({ suggestions, onApply, + planId, // Necesitamos el ID + currentDatos, // Necesitamos los datos actuales para no sobrescribir todo el JSON }: { suggestions: Array - onApply: (key: string, value: string) => void + onApply?: (key: string, value: string) => void + planId: string + currentDatos: any }) => { - // Estado para rastrear qué campos han sido aplicados const [appliedFields, setAppliedFields] = useState>([]) + const updatePlan = useUpdatePlanFields() - const handleApply = (key: string, value: string) => { - onApply(key, value) - setAppliedFields((prev) => [...prev, key]) + const handleApply = (key: string, newValue: string) => { + if (!currentDatos) return + + // 1. Lógica para preparar el valor (idéntica a tu handleSave original) + const currentValue = currentDatos[key] + let finalValue: any + + if ( + typeof currentValue === 'object' && + currentValue !== null && + 'description' in currentValue + ) { + finalValue = { ...currentValue, description: newValue } + } else { + finalValue = newValue + } + + // 2. Construir el nuevo objeto 'datos' manteniendo lo que ya existía + const datosActualizados = { + ...currentDatos, + [key]: finalValue, + } + + // 3. Ejecutar la mutación directamente aquí + updatePlan.mutate( + { + planId: planId as any, + patch: { datos: datosActualizados }, + }, + { + onSuccess: () => { + setAppliedFields((prev) => [...prev, key]) + if (onApply) onApply(key, newValue) + console.log(`Campo ${key} guardado exitosamente`) + }, + }, + ) } return (
{suggestions.map((sug) => { const isApplied = appliedFields.includes(sug.key) + const isUpdating = + updatePlan.isPending && + updatePlan.variables.patch.datos?.[sug.key] !== undefined return (

{sug.label}

)) ) : ( - // LISTA DE CHATS ARCHIVADOS + // --- LISTA DE CHATS ARCHIVADOS ---

Archivados

- {archivedHistory.map((chat) => ( + {archivedChats.map((chat) => (
- {chat.title} + + {chat.title || + `Archivado ${chat.creado_en.split('T')[0]}`} +
))} - {archivedHistory.length === 0 && ( + {archivedChats.length === 0 && (

No hay archivados @@ -558,12 +563,13 @@ function RouteComponent() {

{ - console.log(`Aplicando ${val} al campo ${key}`) - setSelectedFields((prev) => - prev.filter((f) => f.key !== key), + // Esto es opcional, si quieres hacer algo más en la UI del chat + console.log( + 'Evento onApply disparado desde el chat', ) - // Aquí llamarías a tu mutación de actualizar el plan }} />
diff --git a/src/routes/planes/$planId/_detalle/index.tsx b/src/routes/planes/$planId/_detalle/index.tsx index 38e64d2..e9899a5 100644 --- a/src/routes/planes/$planId/_detalle/index.tsx +++ b/src/routes/planes/$planId/_detalle/index.tsx @@ -160,6 +160,7 @@ function DatosGeneralesPage() { if (!data?.datos) return const datosActualizados = prepararDatosActualizados(data, campo, valor) + console.log(datosActualizados) updatePlan.mutate({ planId, -- 2.49.1