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 planId: string currentDatos: any }) => { const [appliedFields, setAppliedFields] = useState>([]) const updatePlan = useUpdatePlanFields() 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}

{sug.newValue}
) })}
) }