privateuserh's picture
Update script.js
46505d1 verified
raw
history blame
3.81 kB
document.addEventListener('DOMContentLoaded', () => {
// --- CONFIGURATION ---
const workerUrl = 'https://form-handler-worker.aiagents.workers.dev/';
// --- ELEMENT SELECTIONS ---
const submissionForm = document.getElementById('vessel-exchange-form');
const lookupForm = document.getElementById('deal-lookup-form');
const adminForm = document.getElementById('admin-action-form');
// Modals and Buttons
const submissionModal = document.getElementById('submission-modal');
const dealModal = document.getElementById('deal-modal');
const showSubmissionButton = document.getElementById('show-submission-button');
const submissionModalCloseButton = document.getElementById('submission-modal-close-button');
const dealModalCloseButton = document.getElementById('modal-close-button');
// --- EVENT LISTENERS ---
// Show the submission form modal
showSubmissionButton.addEventListener('click', () => {
submissionModal.classList.remove('hidden');
});
// Close the submission form modal
submissionModalCloseButton.addEventListener('click', () => closeModal(submissionModal));
submissionModal.addEventListener('click', (event) => {
if (event.target === submissionModal) closeModal(submissionModal);
});
// Handle New Deal Submissions
if (submissionForm) {
submissionForm.addEventListener('submit', handleFormSubmit);
}
// --- All other event listeners and functions from the previous version remain the same ---
// (lookupForm, adminForm, dealModal closing, etc.)
// --- HANDLER FUNCTIONS ---
async function handleFormSubmit(event) {
event.preventDefault();
const submitButton = document.getElementById('submit-button');
const statusMessage = document.getElementById('status-message');
submitButton.disabled = true;
showStatus(statusMessage, 'Submitting...');
try {
const formData = new FormData(submissionForm);
const response = await fetch(workerUrl, { method: 'POST', body: formData });
const result = await response.json();
if (!response.ok) throw new Error(result.error || `Server responded with status ${response.status}`);
showSuccess(statusMessage, `Success! Your Deal ID is: ${result.deal_id}`);
submissionForm.reset();
// Close the modal after a short delay to allow user to see the success message
setTimeout(() => closeModal(submissionModal), 2000);
} catch (error) {
showError(statusMessage, error.message);
} finally {
submitButton.disabled = false;
}
}
// --- All other handler functions (handleLookupSubmit, handleAdminClick) remain the same ---
// --- MODAL & UI FUNCTIONS ---
function closeModal(modalElement) {
modalElement.classList.add('hidden');
}
function showStatus(element, message) {
element.className = 'status';
element.textContent = message;
element.style.display = 'block';
}
function showSuccess(element, message) {
element.className = 'status success';
element.innerHTML = `<strong>${message}</strong>`; // Use innerHTML to render bold tag
element.style.display = 'block';
}
function showError(element, message) {
element.className = 'status error';
element.innerHTML = `<strong>Error:</strong> ${message}`;
element.style.display = 'block';
}
// --- The openModal function for the deal lookup remains the same ---
function openModal(dealData) {
// ... (existing code to populate deal info modal)
dealModal.classList.remove('hidden');
}
});