63 lines
2.4 KiB
JavaScript
63 lines
2.4 KiB
JavaScript
// renderer.js - DEFINITIVE VERSION
|
|
|
|
// We wrap our entire script in a 'DOMContentLoaded' event listener.
|
|
// This guarantees that the code inside will only run AFTER the whole
|
|
// HTML document has been loaded and is ready. This prevents race conditions.
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
console.log('DOM fully loaded. renderer.js is now running.');
|
|
|
|
// Set the default publication date to today
|
|
const publicationDateInput = document.getElementById('publicationDate');
|
|
if (publicationDateInput) {
|
|
publicationDateInput.value = new Date().toISOString().split('T')[0];
|
|
console.log('Publication date set to today.');
|
|
}
|
|
|
|
// Find the form element
|
|
const form = document.getElementById('machine-form');
|
|
|
|
if (form) {
|
|
console.log('Form element #machine-form found. Attaching listener...');
|
|
|
|
// Attach the event listener to the form's 'submit' event
|
|
form.addEventListener('submit', async (event) => {
|
|
event.preventDefault(); // Stop the default page reload
|
|
|
|
// THIS IS THE LOG WE WANT TO SEE WHEN THE BUTTON IS CLICKED
|
|
console.log('Form submit event triggered! Collecting data...');
|
|
|
|
const formData = new FormData(form);
|
|
const data = Object.fromEntries(formData.entries());
|
|
|
|
const radioButtons = form.querySelectorAll('input[type="radio"]');
|
|
const radioState = {};
|
|
const radioNames = [...new Set([...radioButtons].map(rb => rb.name))];
|
|
radioNames.forEach(name => {
|
|
const checkedRadio = form.querySelector(`input[name="${name}"]:checked`);
|
|
radioState[name] = checkedRadio ? checkedRadio.value : 'No';
|
|
});
|
|
|
|
const finalData = { ...data, ...radioState };
|
|
console.log('Collected form data:', finalData);
|
|
|
|
try {
|
|
console.log('Sending data to main process via electronAPI...');
|
|
const result = await window.electronAPI.submitForm(finalData);
|
|
console.log('Response from main process:', result);
|
|
|
|
if (result.success) {
|
|
form.reset();
|
|
document.getElementById('publicationDate').value = new Date().toISOString().split('T')[0];
|
|
// The success dialog is shown from main.js
|
|
}
|
|
} catch (error) {
|
|
console.error('Error during form submission process:', error);
|
|
}
|
|
});
|
|
|
|
} else {
|
|
// If you see this, the ID in index.html is still wrong.
|
|
console.error('CRITICAL ERROR: Form element with ID "machine-form" was not found.');
|
|
}
|
|
}); |