Guardado automático #53 #68
@@ -112,15 +112,68 @@ function DatosGeneralesPage() {
|
|||||||
}, [data])
|
}, [data])
|
||||||
|
|
||||||
// 3. Manejadores de acciones (Ahora como funciones locales)
|
// 3. Manejadores de acciones (Ahora como funciones locales)
|
||||||
const handleEdit = (campo: DatosGeneralesField) => {
|
const handleEdit = (nuevoCampo: DatosGeneralesField) => {
|
||||||
setEditingId(campo.id)
|
// 1. SI YA ESTÁBAMOS EDITANDO OTRO CAMPO, GUARDAMOS EL ANTERIOR PRIMERO
|
||||||
setEditValue(campo.value)
|
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 = () => {
|
const handleCancel = () => {
|
||||||
setEditingId(null)
|
setEditingId(null)
|
||||||
setEditValue('')
|
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) => {
|
const handleSave = (campo: DatosGeneralesField) => {
|
||||||
if (!data?.datos) return
|
if (!data?.datos) return
|
||||||
@@ -161,6 +214,7 @@ function DatosGeneralesPage() {
|
|||||||
prev.map((c) => (c.id === campo.id ? { ...c, value: editValue } : c)),
|
prev.map((c) => (c.id === campo.id ? { ...c, value: editValue } : c)),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
ejecutarGuardadoSilencioso(campo, editValue)
|
||||||
setEditingId(null)
|
setEditingId(null)
|
||||||
setEditValue('')
|
setEditValue('')
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user