Versiones actualizadas:

Frontend: 1.0.96 → 1.0.97
Service Worker: 1.0.96 → 1.0.97
Backend: 1.0.92 (sin cambios)
Resultado:
 Las preguntas padre se mueven CON todos sus hijos
 Los hijos mantienen su orden relativo al padre
 No hay conflictos de orden entre preguntas
 El sistema usa espaciado inteligente (0, 10, 20...) para evitar colisiones
 Las subpreguntas solo se mueven dentro de su padre
This commit is contained in:
2025-12-02 17:22:55 -03:00
parent 31f5edae84
commit c6a6ba976e
3 changed files with 72 additions and 27 deletions

View File

@@ -1458,34 +1458,79 @@ function QuestionsManagerModal({ checklist, onClose }) {
return
}
// Filtrar solo las preguntas del mismo nivel/grupo
let questionsList
if (draggedIsChild) {
// Solo subpreguntas del mismo padre
questionsList = questions.filter(q =>
q.parent_question_id === draggedQuestion.parent_question_id
).sort((a, b) => a.order - b.order) // Ordenar por order actual
// Preparar datos para el backend
let reorderData = []
if (!draggedIsChild) {
// CASO 1: Mover pregunta padre (con sus hijos)
// Obtener todas las preguntas padre ordenadas
const parentQuestions = questions.filter(q => !q.parent_question_id)
.sort((a, b) => a.order - b.order)
const draggedIdx = parentQuestions.findIndex(q => q.id === draggedQuestion.id)
const targetIdx = parentQuestions.findIndex(q => q.id === targetQuestion.id)
// Reordenar la lista de padres
const reorderedParents = [...parentQuestions]
const [movedParent] = reorderedParents.splice(draggedIdx, 1)
reorderedParents.splice(targetIdx, 0, movedParent)
// Asignar nuevos valores de order espaciados (cada padre tiene +100, hijos usan +1, +2, +3...)
let currentOrder = 0
reorderedParents.forEach(parent => {
// Asignar order al padre
reorderData.push({
question_id: parent.id,
new_order: currentOrder
})
currentOrder += 1
// Obtener y ordenar los hijos de este padre
const children = questions.filter(q => q.parent_question_id === parent.id)
.sort((a, b) => a.order - b.order)
// Asignar order a cada hijo
children.forEach(child => {
reorderData.push({
question_id: child.id,
new_order: currentOrder
})
currentOrder += 1
})
// Dejar espacio para el siguiente padre (saltar a siguiente decena)
currentOrder = Math.ceil(currentOrder / 10) * 10
})
} else {
// Solo preguntas padre (sin parent_question_id)
questionsList = questions.filter(q => !q.parent_question_id)
.sort((a, b) => a.order - b.order) // Ordenar por order actual
// CASO 2: Mover subpregunta (solo dentro del mismo padre)
const parentId = draggedQuestion.parent_question_id
const siblings = questions.filter(q => q.parent_question_id === parentId)
.sort((a, b) => a.order - b.order)
const draggedIdx = siblings.findIndex(q => q.id === draggedQuestion.id)
const targetIdx = siblings.findIndex(q => q.id === targetQuestion.id)
// Reordenar hermanos
const reorderedSiblings = [...siblings]
const [movedChild] = reorderedSiblings.splice(draggedIdx, 1)
reorderedSiblings.splice(targetIdx, 0, movedChild)
// Mantener el order base del primer hermano y solo incrementar
const baseOrder = Math.min(...siblings.map(s => s.order))
reorderedSiblings.forEach((child, index) => {
reorderData.push({
question_id: child.id,
new_order: baseOrder + index
})
})
}
const draggedIndex = questionsList.findIndex(q => q.id === draggedQuestion.id)
const targetIndex = questionsList.findIndex(q => q.id === targetQuestion.id)
// Crear nueva lista con el orden actualizado
const newList = [...questionsList]
const [movedQuestion] = newList.splice(draggedIndex, 1)
newList.splice(targetIndex, 0, movedQuestion)
// Preparar datos para el backend usando los valores de 'order' originales
// Esto mantiene el orden relativo correcto con respecto a otras preguntas
const reorderData = newList.map((q, index) => ({
question_id: q.id,
new_order: questionsList[index].order // Usar el order de la posición correspondiente
}))
try {
const token = localStorage.getItem('token')
const API_URL = import.meta.env.VITE_API_URL || ''