diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index b127f2c..9358e4f 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -2005,14 +2005,14 @@ function InspectionModal({ checklist, user, onClose, onComplete }) { } } - // Step 2: Submit answer and move to next question - const handleAnswerSubmit = async () => { - if (!inspectionId || currentQuestionIndex >= questions.length) return + // Step 2: Auto-save answer when changed (non-blocking) + const saveAnswer = async (questionId) => { + if (!inspectionId) return - const question = questions[currentQuestionIndex] - const answer = answers[question.id] + const question = questions.find(q => q.id === questionId) + const answer = answers[questionId] - setLoading(true) + if (!answer?.value) return // Don't save empty answers try { const token = localStorage.getItem('token') @@ -2038,8 +2038,6 @@ function InspectionModal({ checklist, user, onClose, onComplete }) { is_flagged: status === 'critical' } - console.log('Submitting answer:', answerData) - const response = await fetch(`${API_URL}/api/answers`, { method: 'POST', headers: { @@ -2049,11 +2047,8 @@ function InspectionModal({ checklist, user, onClose, onComplete }) { body: JSON.stringify(answerData) }) - console.log('Answer response status:', response.status) - if (response.ok) { const savedAnswer = await response.json() - console.log('Answer saved:', savedAnswer) // Upload photos if any if (answer.photos.length > 0) { @@ -2069,25 +2064,57 @@ function InspectionModal({ checklist, user, onClose, onComplete }) { } } - // Move to next question or signatures - if (currentQuestionIndex < questions.length - 1) { - setCurrentQuestionIndex(currentQuestionIndex + 1) - } else { - setStep(3) - } - } else { - const errorText = await response.text() - console.error('Error response:', errorText) - alert('Error al guardar respuesta: ' + errorText) + // Mark as saved + setAnswers({ + ...answers, + [questionId]: { ...answers[questionId], saved: true } + }) } } catch (error) { - console.error('Error:', error) - alert('Error al guardar respuesta: ' + error.message) - } finally { - setLoading(false) + console.error('Error saving answer:', error) } } + // Navigate between questions freely + const goToQuestion = (index) => { + setCurrentQuestionIndex(index) + } + + // Validate all questions answered before completing + const validateAllAnswered = () => { + const visibleQuestions = getVisibleQuestions() + const unanswered = visibleQuestions.filter(q => !answers[q.id]?.value) + return unanswered + } + + // Get visible questions based on conditional logic + const getVisibleQuestions = () => { + return questions.filter(q => { + // If no parent, always visible + if (!q.parent_question_id) return true + + // Check parent answer + const parentAnswer = answers[q.parent_question_id] + if (!parentAnswer) return false + + // Show if parent answer matches trigger + return parentAnswer.value === q.show_if_answer + }) + } + + // Move to signatures step + const proceedToSignatures = () => { + const unanswered = validateAllAnswered() + if (unanswered.length > 0) { + alert(`⚠️ Faltan responder ${unanswered.length} pregunta(s). Por favor completa todas las preguntas antes de continuar.`) + // Go to first unanswered + const firstIndex = questions.findIndex(q => q.id === unanswered[0].id) + if (firstIndex >= 0) setCurrentQuestionIndex(firstIndex) + return + } + setStep(3) + } + // Step 3: Submit signatures and complete const handleComplete = async () => { setLoading(true) @@ -2352,14 +2379,24 @@ function InspectionModal({ checklist, user, onClose, onComplete }) { {step === 1 && 'Datos del Vehículo'} - {step === 2 && `Pregunta ${currentQuestionIndex + 1} de ${questions.length}`} + {step === 2 && (() => { + const visible = getVisibleQuestions() + const answered = visible.filter(q => answers[q.id]?.value).length + return `${answered}/${visible.length} preguntas respondidas` + })()} {step === 3 && 'Firmas'}