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.
This commit is contained in:
Quality App System
2026-01-19 21:14:44 +02:00
parent ce9563794a
commit 04a37501ec

View File

@@ -133,6 +133,40 @@ function showNotification(message, type = 'info') {
} }
document.addEventListener('DOMContentLoaded', function() { 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 // Load toggle state FIRST
const savedState = localStorage.getItem('scan_to_boxes_enabled'); const savedState = localStorage.getItem('scan_to_boxes_enabled');
if (savedState === 'true') { if (savedState === 'true') {
@@ -140,6 +174,9 @@ document.addEventListener('DOMContentLoaded', function() {
} }
console.log('Initial scanToBoxesEnabled:', scanToBoxesEnabled); console.log('Initial scanToBoxesEnabled:', scanToBoxesEnabled);
// Flag to prevent duplicate validation during auto-submit
let isAutoSubmitting = false;
const operatorCodeInput = document.getElementById('operator_code'); const operatorCodeInput = document.getElementById('operator_code');
const cpCodeInput = document.getElementById('cp_code'); const cpCodeInput = document.getElementById('cp_code');
const oc1CodeInput = document.getElementById('oc1_code'); const oc1CodeInput = document.getElementById('oc1_code');
@@ -679,6 +716,13 @@ document.addEventListener('DOMContentLoaded', function() {
return; return;
} }
// Clear all custom validity states before submitting
operatorCodeInput.setCustomValidity('');
cpCodeInput.setCustomValidity('');
oc1CodeInput.setCustomValidity('');
oc2CodeInput.setCustomValidity('');
this.setCustomValidity('');
// Update time field before submitting // Update time field before submitting
const timeInput = document.getElementById('time'); const timeInput = document.getElementById('time');
const now = new Date(); const now = new Date();
@@ -703,15 +747,26 @@ document.addEventListener('DOMContentLoaded', function() {
console.log('Auto-submit: Scan-to-boxes enabled, calling submitScanWithBoxAssignment'); console.log('Auto-submit: Scan-to-boxes enabled, calling submitScanWithBoxAssignment');
submitScanWithBoxAssignment(); submitScanWithBoxAssignment();
} else { } 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 // Submit the form normally
console.log('Calling form.submit() - form:', form);
form.submit(); form.submit();
console.log('form.submit() called successfully');
} }
} }
}); });
// Validate form on submit // Validate form on submit
form.addEventListener('submit', async function(e) { 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; let hasError = false;
if (!operatorCodeInput.value.startsWith('OP')) { if (!operatorCodeInput.value.startsWith('OP')) {