Fix: Add comprehensive null checks to prevent script.js errors

- Add guard clauses for reportTable, reportButtons, exportCsvButton, exportPdfButton
- Prevents JavaScript errors when report elements don't exist on page
- Ensures theme toggle and other functionality work on all pages
- Wraps report-specific code in null checks to avoid breaking other features
This commit is contained in:
Quality App System
2026-01-19 21:37:56 +02:00
parent 91b798f323
commit 2b9c478676

View File

@@ -66,6 +66,8 @@ document.addEventListener('DOMContentLoaded', () => {
// Function to populate the table with data // Function to populate the table with data
function populateTable(data) { function populateTable(data) {
if (!reportTable) return; // Guard clause if reportTable doesn't exist
const tableHead = reportTable.querySelector('thead tr'); const tableHead = reportTable.querySelector('thead tr');
const tableBody = reportTable.querySelector('tbody'); const tableBody = reportTable.querySelector('tbody');
@@ -137,6 +139,8 @@ document.addEventListener('DOMContentLoaded', () => {
// Function to export table data as CSV // Function to export table data as CSV
function exportTableToCSV(filename) { function exportTableToCSV(filename) {
if (!reportTable) return; // Guard clause if reportTable doesn't exist
let csv = []; let csv = [];
const rows = reportTable.querySelectorAll('tr'); const rows = reportTable.querySelectorAll('tr');
@@ -165,7 +169,8 @@ document.addEventListener('DOMContentLoaded', () => {
document.body.removeChild(downloadLink); document.body.removeChild(downloadLink);
} }
// Handle report button clicks // Handle report button clicks (only if report elements exist)
if (reportButtons && reportButtons.length > 0) {
reportButtons.forEach((button) => { reportButtons.forEach((button) => {
button.addEventListener('click', () => { button.addEventListener('click', () => {
// Skip buttons that have their own handlers // Skip buttons that have their own handlers
@@ -209,13 +214,14 @@ document.addEventListener('DOMContentLoaded', () => {
}) })
.catch((error) => { .catch((error) => {
console.error('Error fetching report data:', error); console.error('Error fetching report data:', error);
reportTitle.textContent = 'Error loading data.'; if (reportTitle) reportTitle.textContent = 'Error loading data.';
}); });
}); });
}); });
}
// Bind the export functionality to the CSV button // Bind the export functionality to the CSV button
if (exportCsvButton) { if (exportCsvButton && reportTable && reportTitle) {
exportCsvButton.addEventListener('click', () => { exportCsvButton.addEventListener('click', () => {
const rows = reportTable.querySelectorAll('tr'); const rows = reportTable.querySelectorAll('tr');
if (rows.length === 0) { if (rows.length === 0) {