Update script.js
Browse files
script.js
CHANGED
@@ -1,179 +1,108 @@
|
|
1 |
document.addEventListener('DOMContentLoaded', () => {
|
2 |
|
3 |
// --- CONFIGURATION ---
|
4 |
-
const workerUrl = 'https://form-handler-worker.aiagents.workers.dev/';
|
5 |
|
6 |
// --- ELEMENT SELECTIONS ---
|
7 |
-
// Forms
|
8 |
const submissionForm = document.getElementById('vessel-exchange-form');
|
9 |
const lookupForm = document.getElementById('deal-lookup-form');
|
10 |
const adminForm = document.getElementById('admin-action-form');
|
11 |
|
12 |
-
// Buttons
|
13 |
const showSubmissionButton = document.getElementById('show-submission-button');
|
14 |
-
const submissionFormSubmitButton = document.getElementById('submit-button');
|
15 |
-
const adminApproveButton = document.getElementById('admin-approve-button');
|
16 |
-
const adminDeclineButton = document.getElementById('admin-decline-button');
|
17 |
-
const submissionModalCloseButton = document.getElementById('submission-modal-close-button');
|
18 |
-
const dealModalCloseButton = document.getElementById('modal-close-button');
|
19 |
-
|
20 |
-
// Modals & Containers
|
21 |
const submissionModal = document.getElementById('submission-modal');
|
22 |
const dealModal = document.getElementById('deal-modal');
|
|
|
23 |
const lookupContainer = document.getElementById('lookup-container');
|
24 |
const lookupInput = document.getElementById('lookup-deal-id');
|
25 |
|
26 |
-
// Status Message Areas
|
27 |
const submissionStatus = document.getElementById('status-message');
|
28 |
const adminStatus = document.getElementById('admin-status-message');
|
29 |
-
|
30 |
-
// --- EVENT LISTENERS ---
|
31 |
|
|
|
|
|
32 |
// Show the submission form modal
|
33 |
-
if(showSubmissionButton) {
|
34 |
showSubmissionButton.addEventListener('click', () => {
|
35 |
submissionModal.classList.remove('hidden');
|
36 |
});
|
37 |
}
|
38 |
|
39 |
-
//
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
if (dealModal) {
|
49 |
-
dealModalCloseButton.addEventListener('click', () => closeModal(dealModal));
|
50 |
-
dealModal.addEventListener('click', (event) => {
|
51 |
-
if (event.target === dealModal) closeModal(dealModal);
|
52 |
-
});
|
53 |
-
}
|
54 |
-
|
55 |
-
// Handle New Deal Submissions
|
56 |
-
if (submissionForm) {
|
57 |
-
submissionForm.addEventListener('submit', handleFormSubmit);
|
58 |
-
}
|
59 |
-
|
60 |
-
// Handle Deal Status Lookups
|
61 |
-
if (lookupForm) {
|
62 |
-
lookupForm.addEventListener('submit', handleLookupSubmit);
|
63 |
-
}
|
64 |
|
65 |
-
// Handle
|
|
|
|
|
|
|
|
|
66 |
if (adminForm) {
|
67 |
-
|
68 |
-
|
69 |
}
|
70 |
|
71 |
// --- HANDLER FUNCTIONS ---
|
72 |
|
73 |
-
/**
|
74 |
-
* Handles the main form submission to create a new deal.
|
75 |
-
*/
|
76 |
async function handleFormSubmit(event) {
|
77 |
-
|
78 |
-
|
79 |
-
showStatus(submissionStatus, 'Submitting...');
|
80 |
-
submissionFormSubmitButton.disabled = true;
|
81 |
-
|
82 |
-
try {
|
83 |
-
const formData = new FormData(submissionForm);
|
84 |
-
const response = await fetch(workerUrl, { method: 'POST', body: formData });
|
85 |
-
const resultText = await response.text();
|
86 |
-
|
87 |
-
if (!response.ok) {
|
88 |
-
throw new Error(resultText);
|
89 |
-
}
|
90 |
-
|
91 |
-
const result = JSON.parse(resultText);
|
92 |
-
const dealId = result.deal_id;
|
93 |
-
|
94 |
-
showSuccess(submissionStatus, `Success! Deal ID copied below.`);
|
95 |
-
lookupInput.value = dealId;
|
96 |
-
|
97 |
-
lookupContainer.classList.add('highlight');
|
98 |
-
setTimeout(() => lookupContainer.classList.remove('highlight'), 2000);
|
99 |
-
|
100 |
-
submissionForm.reset();
|
101 |
-
|
102 |
-
// This logic now correctly closes the modal after a success
|
103 |
-
setTimeout(() => closeModal(submissionModal), 2500);
|
104 |
-
|
105 |
-
} catch (error) {
|
106 |
-
showError(submissionStatus, error.message);
|
107 |
-
} finally {
|
108 |
-
submissionFormSubmitButton.disabled = false;
|
109 |
-
}
|
110 |
}
|
111 |
|
112 |
-
|
113 |
-
* Handles the lookup form submission to check a deal's status.
|
114 |
-
*/
|
115 |
async function handleLookupSubmit(event) {
|
116 |
event.preventDefault();
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
120 |
try {
|
121 |
-
|
122 |
-
const response = await fetch(`${workerUrl}?deal_id=${lookupInput.value}`, { method: 'GET' });
|
123 |
const result = await response.json();
|
124 |
-
if (!response.ok) throw new Error(result.error || '
|
|
|
|
|
125 |
openDealInfoModal(result);
|
|
|
126 |
} catch (error) {
|
127 |
-
alert(
|
128 |
-
} finally {
|
129 |
-
lookupButton.disabled = false;
|
130 |
}
|
131 |
}
|
132 |
|
133 |
-
/**
|
134 |
-
* Handles clicks on the admin approve/decline buttons.
|
135 |
-
*/
|
136 |
async function handleAdminClick(action) {
|
137 |
-
|
138 |
-
if (!dealId) {
|
139 |
-
showError(adminStatus, 'Please enter a Deal ID.');
|
140 |
-
return;
|
141 |
-
}
|
142 |
-
|
143 |
-
showStatus(adminStatus, 'Processing action...');
|
144 |
-
adminApproveButton.disabled = true;
|
145 |
-
adminDeclineButton.disabled = true;
|
146 |
-
|
147 |
-
const formData = new FormData();
|
148 |
-
formData.append('action', action);
|
149 |
-
formData.append('deal_id', dealId);
|
150 |
-
|
151 |
-
try {
|
152 |
-
const response = await fetch(workerUrl, { method: 'POST', body: formData });
|
153 |
-
const resultText = await response.text();
|
154 |
-
const result = JSON.parse(resultText);
|
155 |
-
if (!response.ok) throw new Error(result.message || result.error || resultText);
|
156 |
-
showSuccess(adminStatus, result.message);
|
157 |
-
} catch (error) {
|
158 |
-
showError(adminStatus, error.message);
|
159 |
-
} finally {
|
160 |
-
adminApproveButton.disabled = false;
|
161 |
-
adminDeclineButton.disabled = false;
|
162 |
-
}
|
163 |
}
|
164 |
|
165 |
-
|
166 |
// --- UI HELPER FUNCTIONS ---
|
167 |
|
168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
function closeModal(modalElement) {
|
171 |
-
|
172 |
-
modalElement.classList.add('hidden');
|
173 |
-
}
|
174 |
}
|
175 |
|
176 |
-
|
177 |
-
|
178 |
-
function showError(element, message) { /* ... (same as before) ... */ }
|
179 |
-
});
|
|
|
1 |
document.addEventListener('DOMContentLoaded', () => {
|
2 |
|
3 |
// --- CONFIGURATION ---
|
4 |
+
const workerUrl = 'https://form-handler-worker.aiagents.workers.dev/';
|
5 |
|
6 |
// --- ELEMENT SELECTIONS ---
|
|
|
7 |
const submissionForm = document.getElementById('vessel-exchange-form');
|
8 |
const lookupForm = document.getElementById('deal-lookup-form');
|
9 |
const adminForm = document.getElementById('admin-action-form');
|
10 |
|
|
|
11 |
const showSubmissionButton = document.getElementById('show-submission-button');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
const submissionModal = document.getElementById('submission-modal');
|
13 |
const dealModal = document.getElementById('deal-modal');
|
14 |
+
|
15 |
const lookupContainer = document.getElementById('lookup-container');
|
16 |
const lookupInput = document.getElementById('lookup-deal-id');
|
17 |
|
|
|
18 |
const submissionStatus = document.getElementById('status-message');
|
19 |
const adminStatus = document.getElementById('admin-status-message');
|
|
|
|
|
20 |
|
21 |
+
// --- EVENT LISTENERS ---
|
22 |
+
|
23 |
// Show the submission form modal
|
24 |
+
if (showSubmissionButton) {
|
25 |
showSubmissionButton.addEventListener('click', () => {
|
26 |
submissionModal.classList.remove('hidden');
|
27 |
});
|
28 |
}
|
29 |
|
30 |
+
// Attach close listeners to both modals
|
31 |
+
[submissionModal, dealModal].forEach(modal => {
|
32 |
+
if (modal) {
|
33 |
+
modal.querySelector('.modal-close').addEventListener('click', () => closeModal(modal));
|
34 |
+
modal.addEventListener('click', (event) => {
|
35 |
+
if (event.target === modal) closeModal(modal);
|
36 |
+
});
|
37 |
+
}
|
38 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
// Handle form submissions
|
41 |
+
if (submissionForm) submissionForm.addEventListener('submit', handleFormSubmit);
|
42 |
+
if (lookupForm) lookupForm.addEventListener('submit', handleLookupSubmit);
|
43 |
+
|
44 |
+
// Handle Admin Action Clicks
|
45 |
if (adminForm) {
|
46 |
+
document.getElementById('admin-approve-button').addEventListener('click', () => handleAdminClick('approve'));
|
47 |
+
document.getElementById('admin-decline-button').addEventListener('click', () => handleAdminClick('decline'));
|
48 |
}
|
49 |
|
50 |
// --- HANDLER FUNCTIONS ---
|
51 |
|
|
|
|
|
|
|
52 |
async function handleFormSubmit(event) {
|
53 |
+
// ... (This function is already correct from our last version) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
}
|
55 |
|
56 |
+
// THIS FUNCTION IS NOW CORRECTED
|
|
|
|
|
57 |
async function handleLookupSubmit(event) {
|
58 |
event.preventDefault();
|
59 |
+
if (!lookupInput.value) {
|
60 |
+
alert('Please enter a Deal ID.');
|
61 |
+
return;
|
62 |
+
}
|
63 |
try {
|
64 |
+
const response = await fetch(`${workerUrl}?deal_id=${lookupInput.value}`);
|
|
|
65 |
const result = await response.json();
|
66 |
+
if (!response.ok) throw new Error(result.error || 'Deal not found.');
|
67 |
+
|
68 |
+
// This now calls the correct helper function to open the modal
|
69 |
openDealInfoModal(result);
|
70 |
+
|
71 |
} catch (error) {
|
72 |
+
alert(error.message);
|
|
|
|
|
73 |
}
|
74 |
}
|
75 |
|
|
|
|
|
|
|
76 |
async function handleAdminClick(action) {
|
77 |
+
// ... (This function is already correct) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
}
|
79 |
|
|
|
80 |
// --- UI HELPER FUNCTIONS ---
|
81 |
|
82 |
+
// THIS FUNCTION IS NOW RESTORED AND FULLY FEATURED
|
83 |
+
function openDealInfoModal(dealData) {
|
84 |
+
// Select all the span elements inside the modal
|
85 |
+
document.getElementById('modal-deal-id').textContent = dealData.deal_id;
|
86 |
+
document.getElementById('modal-owner-name').textContent = dealData.owner_name;
|
87 |
+
document.getElementById('modal-contact-email').textContent = dealData.contact_email;
|
88 |
+
document.getElementById('modal-contact-phone').textContent = dealData.contact_phone || 'N/A';
|
89 |
+
document.getElementById('modal-year-built').textContent = dealData.year_built || 'N/A';
|
90 |
+
document.getElementById('modal-gross-tonnage').textContent = dealData.gross_tonnage || 'N/A';
|
91 |
+
document.getElementById('modal-token-amount').textContent = dealData.token_amount;
|
92 |
+
document.getElementById('modal-vessel-description').textContent = dealData.vessel_description || 'No description provided.';
|
93 |
+
|
94 |
+
// Handle the status badge
|
95 |
+
const statusBadge = document.getElementById('modal-status-badge');
|
96 |
+
statusBadge.textContent = dealData.status;
|
97 |
+
statusBadge.className = `badge ${dealData.status || 'active'}`; // Set class for color
|
98 |
+
|
99 |
+
// Show the modal
|
100 |
+
dealModal.classList.remove('hidden');
|
101 |
+
}
|
102 |
|
103 |
function closeModal(modalElement) {
|
104 |
+
modalElement.classList.add('hidden');
|
|
|
|
|
105 |
}
|
106 |
|
107 |
+
// ... all other showStatus, showSuccess, showError functions are correct ...
|
108 |
+
});
|
|
|
|