Spaces:
Paused
Paused
Update script.js
Browse files
script.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
document.addEventListener('DOMContentLoaded', () => {
|
| 2 |
|
| 3 |
// --- CONFIGURATION ---
|
|
@@ -20,14 +26,12 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 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));
|
|
@@ -37,11 +41,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 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'));
|
|
@@ -49,11 +51,61 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 49 |
|
| 50 |
// --- HANDLER FUNCTIONS ---
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
async function handleFormSubmit(event) {
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
}
|
| 55 |
|
| 56 |
-
/
|
|
|
|
|
|
|
| 57 |
async function handleLookupSubmit(event) {
|
| 58 |
event.preventDefault();
|
| 59 |
if (!lookupInput.value) {
|
|
@@ -65,7 +117,6 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 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) {
|
|
@@ -73,36 +124,84 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
|
|
|
|
|
|
|
|
|
| 76 |
async function handleAdminClick(action) {
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
}
|
| 79 |
|
| 80 |
// --- UI HELPER FUNCTIONS ---
|
| 81 |
|
| 82 |
-
/
|
|
|
|
|
|
|
| 83 |
function openDealInfoModal(dealData) {
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
// Show the modal
|
| 100 |
dealModal.classList.remove('hidden');
|
| 101 |
}
|
| 102 |
|
| 103 |
function closeModal(modalElement) {
|
| 104 |
-
modalElement.classList.add('hidden');
|
| 105 |
}
|
| 106 |
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
});
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Client-Side JavaScript for the International Umbrella Endowment
|
| 3 |
+
*
|
| 4 |
+
* This version includes the fix to auto-populate the Deal ID into the lookup field upon successful submission.
|
| 5 |
+
* Final Version: Monday, June 16, 2025
|
| 6 |
+
*/
|
| 7 |
document.addEventListener('DOMContentLoaded', () => {
|
| 8 |
|
| 9 |
// --- CONFIGURATION ---
|
|
|
|
| 26 |
|
| 27 |
// --- EVENT LISTENERS ---
|
| 28 |
|
|
|
|
| 29 |
if (showSubmissionButton) {
|
| 30 |
showSubmissionButton.addEventListener('click', () => {
|
| 31 |
submissionModal.classList.remove('hidden');
|
| 32 |
});
|
| 33 |
}
|
| 34 |
|
|
|
|
| 35 |
[submissionModal, dealModal].forEach(modal => {
|
| 36 |
if (modal) {
|
| 37 |
modal.querySelector('.modal-close').addEventListener('click', () => closeModal(modal));
|
|
|
|
| 41 |
}
|
| 42 |
});
|
| 43 |
|
|
|
|
| 44 |
if (submissionForm) submissionForm.addEventListener('submit', handleFormSubmit);
|
| 45 |
if (lookupForm) lookupForm.addEventListener('submit', handleLookupSubmit);
|
| 46 |
|
|
|
|
| 47 |
if (adminForm) {
|
| 48 |
document.getElementById('admin-approve-button').addEventListener('click', () => handleAdminClick('approve'));
|
| 49 |
document.getElementById('admin-decline-button').addEventListener('click', () => handleAdminClick('decline'));
|
|
|
|
| 51 |
|
| 52 |
// --- HANDLER FUNCTIONS ---
|
| 53 |
|
| 54 |
+
/**
|
| 55 |
+
* Handles the main form submission to create a new deal.
|
| 56 |
+
* THIS FUNCTION CONTAINS THE FIX.
|
| 57 |
+
*/
|
| 58 |
async function handleFormSubmit(event) {
|
| 59 |
+
event.preventDefault();
|
| 60 |
+
const submitButton = document.getElementById('submit-button');
|
| 61 |
+
|
| 62 |
+
showStatus(submissionStatus, 'Submitting...');
|
| 63 |
+
submitButton.disabled = true;
|
| 64 |
+
|
| 65 |
+
try {
|
| 66 |
+
const formData = new FormData(submissionForm);
|
| 67 |
+
const response = await fetch(workerUrl, { method: 'POST', body: formData });
|
| 68 |
+
const result = await response.json();
|
| 69 |
+
|
| 70 |
+
if (!response.ok) {
|
| 71 |
+
// Use the error message from the worker if available
|
| 72 |
+
throw new Error(result.message || result.error || 'An unknown error occurred.');
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
const dealId = result.deal_id;
|
| 76 |
+
|
| 77 |
+
// --- THIS IS THE CORRECTED LOGIC ---
|
| 78 |
+
// 1. Update the success message.
|
| 79 |
+
showSuccess(submissionStatus, `Success! Deal ID copied below.`);
|
| 80 |
+
|
| 81 |
+
// 2. Automatically populate the lookup field on the main page.
|
| 82 |
+
if(lookupInput) {
|
| 83 |
+
lookupInput.value = dealId;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
// 3. Highlight the lookup container to draw attention to it.
|
| 87 |
+
if(lookupContainer) {
|
| 88 |
+
lookupContainer.classList.add('highlight');
|
| 89 |
+
setTimeout(() => {
|
| 90 |
+
lookupContainer.classList.remove('highlight');
|
| 91 |
+
}, 2000); // Remove highlight after 2 seconds
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
submissionForm.reset();
|
| 95 |
+
|
| 96 |
+
// 4. Close the modal after a delay so the user can see the message.
|
| 97 |
+
setTimeout(() => closeModal(submissionModal), 2500);
|
| 98 |
+
|
| 99 |
+
} catch (error) {
|
| 100 |
+
showError(submissionStatus, error.message);
|
| 101 |
+
} finally {
|
| 102 |
+
submitButton.disabled = false;
|
| 103 |
+
}
|
| 104 |
}
|
| 105 |
|
| 106 |
+
/**
|
| 107 |
+
* Handles the lookup form submission to check a deal's status.
|
| 108 |
+
*/
|
| 109 |
async function handleLookupSubmit(event) {
|
| 110 |
event.preventDefault();
|
| 111 |
if (!lookupInput.value) {
|
|
|
|
| 117 |
const result = await response.json();
|
| 118 |
if (!response.ok) throw new Error(result.error || 'Deal not found.');
|
| 119 |
|
|
|
|
| 120 |
openDealInfoModal(result);
|
| 121 |
|
| 122 |
} catch (error) {
|
|
|
|
| 124 |
}
|
| 125 |
}
|
| 126 |
|
| 127 |
+
/**
|
| 128 |
+
* Handles clicks on the admin approve/decline buttons.
|
| 129 |
+
*/
|
| 130 |
async function handleAdminClick(action) {
|
| 131 |
+
const dealId = document.getElementById('admin-deal-id').value;
|
| 132 |
+
if (!dealId) {
|
| 133 |
+
showError(adminStatus, 'Please enter a Deal ID.');
|
| 134 |
+
return;
|
| 135 |
+
}
|
| 136 |
+
showStatus(adminStatus, 'Processing...');
|
| 137 |
+
|
| 138 |
+
const formData = new FormData();
|
| 139 |
+
formData.append('action', action);
|
| 140 |
+
formData.append('deal_id', dealId);
|
| 141 |
+
|
| 142 |
+
try {
|
| 143 |
+
const response = await fetch(workerUrl, { method: 'POST', body: formData });
|
| 144 |
+
const result = await response.json();
|
| 145 |
+
if (!response.ok) throw new Error(result.message || result.error || 'Action failed.');
|
| 146 |
+
showSuccess(adminStatus, result.message);
|
| 147 |
+
} catch (error) {
|
| 148 |
+
showError(adminStatus, error.message);
|
| 149 |
+
}
|
| 150 |
}
|
| 151 |
|
| 152 |
// --- UI HELPER FUNCTIONS ---
|
| 153 |
|
| 154 |
+
/**
|
| 155 |
+
* Populates and opens the deal information modal.
|
| 156 |
+
*/
|
| 157 |
function openDealInfoModal(dealData) {
|
| 158 |
+
const modalBody = document.getElementById('modal-body');
|
| 159 |
+
// Clear previous results
|
| 160 |
+
modalBody.innerHTML = '';
|
| 161 |
+
|
| 162 |
+
// Create and append deal details
|
| 163 |
+
const details = {
|
| 164 |
+
"Deal ID": dealData.deal_id,
|
| 165 |
+
"Status": `<span class="badge ${dealData.status || 'active'}">${dealData.status}</span>`,
|
| 166 |
+
"Owner": dealData.owner_name,
|
| 167 |
+
"Contact Email": dealData.contact_email,
|
| 168 |
+
"Contact Phone": dealData.contact_phone,
|
| 169 |
+
"Vessel Year Built": dealData.year_built,
|
| 170 |
+
"Gross Tonnage": dealData.gross_tonnage,
|
| 171 |
+
"Tokens Requested": dealData.token_amount,
|
| 172 |
+
"Description": `<p class="description-text">${dealData.vessel_description || 'N/A'}</p>`
|
| 173 |
+
};
|
| 174 |
+
|
| 175 |
+
for (const [key, value] of Object.entries(details)) {
|
| 176 |
+
if (value) { // Only show fields that have data
|
| 177 |
+
const p = document.createElement('p');
|
| 178 |
+
p.innerHTML = `<strong>${key}:</strong> ${value}`;
|
| 179 |
+
modalBody.appendChild(p);
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
|
|
|
|
| 183 |
dealModal.classList.remove('hidden');
|
| 184 |
}
|
| 185 |
|
| 186 |
function closeModal(modalElement) {
|
| 187 |
+
if(modalElement) modalElement.classList.add('hidden');
|
| 188 |
}
|
| 189 |
|
| 190 |
+
function showStatus(element, message) {
|
| 191 |
+
element.className = 'status';
|
| 192 |
+
element.innerHTML = message;
|
| 193 |
+
element.style.display = 'block';
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
function showSuccess(element, message) {
|
| 197 |
+
element.className = 'status success';
|
| 198 |
+
element.innerHTML = `<strong>${message}</strong>`;
|
| 199 |
+
element.style.display = 'block';
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
function showError(element, message) {
|
| 203 |
+
element.className = 'status error';
|
| 204 |
+
element.innerHTML = `<strong>Error:</strong> ${message}`;
|
| 205 |
+
element.style.display = 'block';
|
| 206 |
+
}
|
| 207 |
});
|