diff --git a/frontend/package.json b/frontend/package.json index 882e45f..d22c7e2 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,7 +1,7 @@ { "name": "checklist-frontend", "private": true, - "version": "1.0.96", + "version": "1.0.97", "type": "module", "scripts": { "dev": "vite", diff --git a/frontend/public/service-worker.js b/frontend/public/service-worker.js index 08d0557..2fdde6f 100644 --- a/frontend/public/service-worker.js +++ b/frontend/public/service-worker.js @@ -1,6 +1,6 @@ // Service Worker para PWA con detección de actualizaciones // IMPORTANTE: Actualizar esta versión cada vez que se despliegue una nueva versión -const CACHE_NAME = 'ayutec-v1.0.96'; +const CACHE_NAME = 'ayutec-v1.0.97'; const urlsToCache = [ '/', '/index.html' diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 646e8be..0fef5be 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -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 || ''