From 04a37501ec2cc8f2e087b85e06596c0153cef32e Mon Sep 17 00:00:00 2001 From: Quality App System Date: Mon, 19 Jan 2026 21:14:44 +0200 Subject: [PATCH] Fix: FG scan form submission hang and add theme toggle functionality - Added isAutoSubmitting flag to prevent duplicate validation during auto-submit - Clear all custom validity states before form submission - Skip duplicate validation in submit handler when auto-submitting - Added theme toggle functionality directly to fg_scan page (since script.js is excluded) - Enhanced logging for debugging form submission flow This fixes the issue where the form would hang and not send data to the database when defect code was auto-submitted. --- py_app/app/templates/fg_scan.html | 59 +++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/py_app/app/templates/fg_scan.html b/py_app/app/templates/fg_scan.html index c0f6457..705e3f7 100644 --- a/py_app/app/templates/fg_scan.html +++ b/py_app/app/templates/fg_scan.html @@ -133,6 +133,40 @@ function showNotification(message, type = 'info') { } document.addEventListener('DOMContentLoaded', function() { + // ========== THEME TOGGLE FUNCTIONALITY ========== + const themeToggleButton = document.getElementById('theme-toggle'); + const body = document.body; + + // Helper function to update the theme toggle button text + function updateThemeToggleButtonText() { + if (themeToggleButton) { + if (body.classList.contains('dark-mode')) { + themeToggleButton.textContent = 'Change to Light Mode'; + } else { + themeToggleButton.textContent = 'Change to Dark Mode'; + } + } + } + + // Check and apply the saved theme from localStorage + const savedTheme = localStorage.getItem('theme'); + if (savedTheme) { + body.classList.toggle('dark-mode', savedTheme === 'dark'); + } + + // Update the button text based on the current theme + updateThemeToggleButtonText(); + + // Toggle the theme on button click + if (themeToggleButton) { + themeToggleButton.addEventListener('click', () => { + const isDarkMode = body.classList.toggle('dark-mode'); + localStorage.setItem('theme', isDarkMode ? 'dark' : 'light'); + updateThemeToggleButtonText(); + }); + } + // ========== END THEME TOGGLE ========== + // Load toggle state FIRST const savedState = localStorage.getItem('scan_to_boxes_enabled'); if (savedState === 'true') { @@ -140,6 +174,9 @@ document.addEventListener('DOMContentLoaded', function() { } console.log('Initial scanToBoxesEnabled:', scanToBoxesEnabled); + // Flag to prevent duplicate validation during auto-submit + let isAutoSubmitting = false; + const operatorCodeInput = document.getElementById('operator_code'); const cpCodeInput = document.getElementById('cp_code'); const oc1CodeInput = document.getElementById('oc1_code'); @@ -636,7 +673,7 @@ document.addEventListener('DOMContentLoaded', function() { this.setCustomValidity(''); } - // Auto-submit when 3 characters are entered and all validations pass + // Auto-submit when 3 characters are entered and all validations pass if (this.value.length === 3) { // Validate operator code before submitting if (!operatorCodeInput.value.startsWith('OP')) { @@ -679,6 +716,13 @@ document.addEventListener('DOMContentLoaded', function() { return; } + // Clear all custom validity states before submitting + operatorCodeInput.setCustomValidity(''); + cpCodeInput.setCustomValidity(''); + oc1CodeInput.setCustomValidity(''); + oc2CodeInput.setCustomValidity(''); + this.setCustomValidity(''); + // Update time field before submitting const timeInput = document.getElementById('time'); const now = new Date(); @@ -703,15 +747,26 @@ document.addEventListener('DOMContentLoaded', function() { console.log('Auto-submit: Scan-to-boxes enabled, calling submitScanWithBoxAssignment'); submitScanWithBoxAssignment(); } else { - console.log('Auto-submit: Normal form submission'); + console.log('Auto-submit: Normal form submission - setting flag and submitting'); + isAutoSubmitting = true; // Submit the form normally + console.log('Calling form.submit() - form:', form); form.submit(); + console.log('form.submit() called successfully'); } } }); // Validate form on submit form.addEventListener('submit', async function(e) { + // Skip validation if this is an auto-submit (already validated) + if (isAutoSubmitting) { + console.log('Auto-submit in progress, skipping duplicate validation'); + isAutoSubmitting = false; // Reset flag + return true; // Allow submission to proceed + } + + console.log('Manual form submission, running validation'); let hasError = false; if (!operatorCodeInput.value.startsWith('OP')) {