Update script.js
Browse files
script.js
CHANGED
@@ -1,58 +1,133 @@
|
|
1 |
document.addEventListener('DOMContentLoaded', () => {
|
2 |
// --- CONFIGURATION ---
|
3 |
-
const workerUrl = 'https://
|
4 |
|
5 |
-
// --- ELEMENT
|
6 |
-
const
|
7 |
-
const
|
8 |
-
const
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
// Provide immediate user feedback
|
15 |
-
submitButton.disabled = true;
|
16 |
-
statusMessage.className = 'status'; // Reset classes
|
17 |
-
statusMessage.textContent = 'Submitting...';
|
18 |
-
statusMessage.style.display = 'block';
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
body: formData
|
25 |
-
});
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
const result = JSON.parse(responseText);
|
37 |
-
showSuccess(result.deal_id);
|
38 |
-
form.reset();
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
} catch (error) {
|
41 |
-
showError(error.message);
|
42 |
} finally {
|
43 |
-
// Re-enable the button whether the submission succeeded or failed
|
44 |
submitButton.disabled = false;
|
45 |
}
|
46 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
}
|
53 |
|
54 |
-
function showError(message) {
|
55 |
-
|
56 |
-
|
|
|
57 |
}
|
58 |
});
|
|
|
1 |
document.addEventListener('DOMContentLoaded', () => {
|
2 |
// --- CONFIGURATION ---
|
3 |
+
const workerUrl = 'https://your-worker-url.workers.dev/'; // IMPORTANT: Use your correct worker URL
|
4 |
|
5 |
+
// --- ELEMENT SELECTIONS ---
|
6 |
+
const submissionForm = document.getElementById('vessel-exchange-form');
|
7 |
+
const lookupForm = document.getElementById('deal-lookup-form');
|
8 |
+
const adminForm = document.getElementById('admin-action-form');
|
9 |
+
|
10 |
+
const submissionStatus = document.getElementById('status-message');
|
11 |
+
const adminStatus = document.getElementById('admin-status-message');
|
12 |
|
13 |
+
const modal = document.getElementById('deal-modal');
|
14 |
+
|
15 |
+
// --- EVENT LISTENERS ---
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
// 1. Handle New Deal Submissions
|
18 |
+
if (submissionForm) {
|
19 |
+
submissionForm.addEventListener('submit', handleFormSubmit);
|
20 |
+
}
|
|
|
|
|
21 |
|
22 |
+
// 2. Handle Deal Status Lookups
|
23 |
+
if (lookupForm) {
|
24 |
+
lookupForm.addEventListener('submit', handleLookupSubmit);
|
25 |
+
}
|
26 |
|
27 |
+
// 3. Handle Admin Actions
|
28 |
+
if (adminForm) {
|
29 |
+
document.getElementById('admin-approve-button').addEventListener('click', () => handleAdminClick('approve'));
|
30 |
+
document.getElementById('admin-decline-button').addEventListener('click', () => handleAdminClick('decline'));
|
31 |
+
}
|
32 |
+
|
33 |
+
// 4. Handle Modal Closing
|
34 |
+
if (modal) {
|
35 |
+
document.getElementById('modal-close-button').addEventListener('click', closeModal);
|
36 |
+
modal.addEventListener('click', (event) => {
|
37 |
+
if (event.target === modal) closeModal();
|
38 |
+
});
|
39 |
+
}
|
40 |
|
41 |
+
// --- HANDLER FUNCTIONS ---
|
|
|
|
|
|
|
42 |
|
43 |
+
async function handleFormSubmit(event) {
|
44 |
+
event.preventDefault();
|
45 |
+
const submitButton = document.getElementById('submit-button');
|
46 |
+
submitButton.disabled = true;
|
47 |
+
submissionStatus.className = 'status';
|
48 |
+
submissionStatus.textContent = 'Submitting...';
|
49 |
+
submissionStatus.style.display = 'block';
|
50 |
+
|
51 |
+
try {
|
52 |
+
const formData = new FormData(submissionForm);
|
53 |
+
const response = await fetch(workerUrl, { method: 'POST', body: formData });
|
54 |
+
const result = await response.json();
|
55 |
+
if (!response.ok) throw new Error(result.error || `Server responded with status ${response.status}`);
|
56 |
+
|
57 |
+
showSuccess(submissionStatus, `Success! Your Deal ID is: ${result.deal_id}`);
|
58 |
+
submissionForm.reset();
|
59 |
} catch (error) {
|
60 |
+
showError(submissionStatus, error.message);
|
61 |
} finally {
|
|
|
62 |
submitButton.disabled = false;
|
63 |
}
|
64 |
+
}
|
65 |
+
|
66 |
+
async function handleLookupSubmit(event) {
|
67 |
+
event.preventDefault();
|
68 |
+
const dealId = document.getElementById('lookup-deal-id').value;
|
69 |
+
if (!dealId) return;
|
70 |
+
|
71 |
+
try {
|
72 |
+
const response = await fetch(`${workerUrl}?deal_id=${dealId}`, { method: 'GET' });
|
73 |
+
const result = await response.json();
|
74 |
+
if (!response.ok) throw new Error(result.error || 'Failed to fetch deal.');
|
75 |
+
|
76 |
+
openModal(result);
|
77 |
+
} catch (error) {
|
78 |
+
alert(`Error: ${error.message}`);
|
79 |
+
}
|
80 |
+
}
|
81 |
+
|
82 |
+
async function handleAdminClick(action) {
|
83 |
+
const dealId = document.getElementById('admin-deal-id').value;
|
84 |
+
if (!dealId) {
|
85 |
+
showError(adminStatus, 'Please enter a Deal ID.');
|
86 |
+
return;
|
87 |
+
}
|
88 |
+
|
89 |
+
const formData = new FormData();
|
90 |
+
formData.append('action', action);
|
91 |
+
formData.append('deal_id', dealId);
|
92 |
+
|
93 |
+
try {
|
94 |
+
const response = await fetch(workerUrl, { method: 'POST', body: formData });
|
95 |
+
const result = await response.json();
|
96 |
+
if (!response.ok) throw new Error(result.error || `Server responded with status ${response.status}`);
|
97 |
+
|
98 |
+
showSuccess(adminStatus, result.message);
|
99 |
+
} catch (error) {
|
100 |
+
showError(adminStatus, error.message);
|
101 |
+
}
|
102 |
+
}
|
103 |
+
|
104 |
+
// --- MODAL & UI FUNCTIONS ---
|
105 |
+
|
106 |
+
function openModal(dealData) {
|
107 |
+
document.getElementById('modal-deal-id').textContent = dealData.deal_id;
|
108 |
+
document.getElementById('modal-owner-name').textContent = dealData.owner_name;
|
109 |
+
document.getElementById('modal-token-amount').textContent = dealData.token_amount;
|
110 |
+
|
111 |
+
const statusBadge = document.getElementById('modal-status-badge');
|
112 |
+
statusBadge.textContent = dealData.status;
|
113 |
+
statusBadge.className = `badge ${dealData.status}`; // e.g., "badge accepted"
|
114 |
+
|
115 |
+
modal.classList.remove('hidden');
|
116 |
+
}
|
117 |
+
|
118 |
+
function closeModal() {
|
119 |
+
modal.classList.add('hidden');
|
120 |
+
}
|
121 |
|
122 |
+
function showSuccess(element, message) {
|
123 |
+
element.className = 'status success';
|
124 |
+
element.textContent = message;
|
125 |
+
element.style.display = 'block';
|
126 |
}
|
127 |
|
128 |
+
function showError(element, message) {
|
129 |
+
element.className = 'status error';
|
130 |
+
element.textContent = `Error: ${message}`;
|
131 |
+
element.style.display = 'block';
|
132 |
}
|
133 |
});
|