privateuserh commited on
Commit
8271f95
·
verified ·
1 Parent(s): 2235939

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +167 -38
script.js CHANGED
@@ -1,99 +1,228 @@
 
 
 
 
 
 
 
 
 
 
 
1
  document.addEventListener('DOMContentLoaded', () => {
 
2
  // --- CONFIGURATION ---
3
- // Add these two lines with your other element selectors
4
- const lookupContainer = document.getElementById('lookup-container');
5
- const lookupInput = document.getElementById('lookup-deal-id');
6
- const workerUrl = 'https://form-handler-worker.aiagents.workers.dev/';
7
 
8
  // --- ELEMENT SELECTIONS ---
 
9
  const submissionForm = document.getElementById('vessel-exchange-form');
10
  const lookupForm = document.getElementById('deal-lookup-form');
11
  const adminForm = document.getElementById('admin-action-form');
12
 
13
- // Modals and Buttons
14
- const submissionModal = document.getElementById('submission-modal');
15
- const dealModal = document.getElementById('deal-modal');
16
  const showSubmissionButton = document.getElementById('show-submission-button');
 
 
 
17
  const submissionModalCloseButton = document.getElementById('submission-modal-close-button');
18
  const dealModalCloseButton = document.getElementById('modal-close-button');
19
 
 
 
 
 
 
 
 
 
 
 
 
20
  // --- EVENT LISTENERS ---
21
 
22
- // Show the submission form modal
23
- showSubmissionButton.addEventListener('click', () => {
24
- submissionModal.classList.remove('hidden');
25
- });
 
 
26
 
27
  // Close the submission form modal
28
- submissionModalCloseButton.addEventListener('click', () => closeModal(submissionModal));
29
- submissionModal.addEventListener('click', (event) => {
30
- if (event.target === submissionModal) closeModal(submissionModal);
31
- });
 
 
 
 
 
 
 
 
 
 
32
 
33
  // Handle New Deal Submissions
34
  if (submissionForm) {
35
  submissionForm.addEventListener('submit', handleFormSubmit);
36
  }
37
 
38
- // --- All other event listeners and functions from the previous version remain the same ---
39
- // (lookupForm, adminForm, dealModal closing, etc.)
 
 
 
 
 
 
 
 
 
40
 
41
  // --- HANDLER FUNCTIONS ---
 
 
 
 
42
  async function handleFormSubmit(event) {
43
- event.preventDefault();
44
- const submitButton = document.getElementById('submit-button');
45
- const statusMessage = document.getElementById('status-message');
46
 
 
47
  submitButton.disabled = true;
48
- showStatus(statusMessage, 'Submitting...');
49
 
50
  try {
51
  const formData = new FormData(submissionForm);
52
  const response = await fetch(workerUrl, { method: 'POST', body: formData });
53
- const result = await response.json();
54
- if (!response.ok) throw new Error(result.error || `Server responded with status ${response.status}`);
 
 
 
 
 
 
 
 
 
 
55
 
56
- showSuccess(statusMessage, `Success! Your Deal ID is: ${result.deal_id}`);
 
 
 
 
57
  submissionForm.reset();
58
 
59
- // Close the modal after a short delay to allow user to see the success message
60
- setTimeout(() => closeModal(submissionModal), 2000);
61
 
62
  } catch (error) {
63
- showError(statusMessage, error.message);
64
  } finally {
65
  submitButton.disabled = false;
66
  }
67
  }
68
 
69
- // --- All other handler functions (handleLookupSubmit, handleAdminClick) remain the same ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
- // --- MODAL & UI FUNCTIONS ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  function closeModal(modalElement) {
73
  modalElement.classList.add('hidden');
74
  }
75
-
 
 
 
76
  function showStatus(element, message) {
77
  element.className = 'status';
78
- element.textContent = message;
79
  element.style.display = 'block';
80
  }
81
 
 
 
 
82
  function showSuccess(element, message) {
83
  element.className = 'status success';
84
- element.innerHTML = `<strong>${message}</strong>`; // Use innerHTML to render bold tag
85
  element.style.display = 'block';
86
  }
87
 
 
 
 
88
  function showError(element, message) {
89
  element.className = 'status error';
90
  element.innerHTML = `<strong>Error:</strong> ${message}`;
91
  element.style.display = 'block';
92
  }
93
-
94
- // --- The openModal function for the deal lookup remains the same ---
95
- function openModal(dealData) {
96
- // ... (existing code to populate deal info modal)
97
- dealModal.classList.remove('hidden');
98
- }
99
  });
 
1
+ /**
2
+ * Client-Side JavaScript for the International Umbrella Endowment
3
+ *
4
+ * Handles all user interactions:
5
+ * - Opening the submission modal via a floating button.
6
+ * - Submitting a new deal and auto-populating the lookup field.
7
+ * - Looking up a deal's status and displaying it in a modal.
8
+ * - Admin actions for approving/declining deals.
9
+ *
10
+ * Final Version: Monday, June 16, 2025
11
+ */
12
  document.addEventListener('DOMContentLoaded', () => {
13
+
14
  // --- CONFIGURATION ---
15
+ const workerUrl = 'https://form-handler-worker.aiagents.workers.dev/'; // Ensure this is your correct worker URL
 
 
 
16
 
17
  // --- ELEMENT SELECTIONS ---
18
+ // Forms
19
  const submissionForm = document.getElementById('vessel-exchange-form');
20
  const lookupForm = document.getElementById('deal-lookup-form');
21
  const adminForm = document.getElementById('admin-action-form');
22
 
23
+ // Buttons
 
 
24
  const showSubmissionButton = document.getElementById('show-submission-button');
25
+ const submitButton = document.getElementById('submit-button');
26
+ const adminApproveButton = document.getElementById('admin-approve-button');
27
+ const adminDeclineButton = document.getElementById('admin-decline-button');
28
  const submissionModalCloseButton = document.getElementById('submission-modal-close-button');
29
  const dealModalCloseButton = document.getElementById('modal-close-button');
30
 
31
+ // Modals & Containers
32
+ const submissionModal = document.getElementById('submission-modal');
33
+ const dealModal = document.getElementById('deal-modal');
34
+ const lookupContainer = document.getElementById('lookup-container');
35
+ const lookupInput = document.getElementById('lookup-deal-id');
36
+
37
+ // Status Message Areas
38
+ const submissionStatus = document.getElementById('status-message');
39
+ const adminStatus = document.getElementById('admin-status-message');
40
+
41
+
42
  // --- EVENT LISTENERS ---
43
 
44
+ // Floating button to show the submission form modal
45
+ if (showSubmissionButton) {
46
+ showSubmissionButton.addEventListener('click', () => {
47
+ submissionModal.classList.remove('hidden');
48
+ });
49
+ }
50
 
51
  // Close the submission form modal
52
+ if (submissionModal) {
53
+ submissionModalCloseButton.addEventListener('click', () => closeModal(submissionModal));
54
+ submissionModal.addEventListener('click', (event) => {
55
+ if (event.target === submissionModal) closeModal(submissionModal);
56
+ });
57
+ }
58
+
59
+ // Close the deal info modal
60
+ if (dealModal) {
61
+ dealModalCloseButton.addEventListener('click', () => closeModal(dealModal));
62
+ dealModal.addEventListener('click', (event) => {
63
+ if (event.target === dealModal) closeModal(dealModal);
64
+ });
65
+ }
66
 
67
  // Handle New Deal Submissions
68
  if (submissionForm) {
69
  submissionForm.addEventListener('submit', handleFormSubmit);
70
  }
71
 
72
+ // Handle Deal Status Lookups
73
+ if (lookupForm) {
74
+ lookupForm.addEventListener('submit', handleLookupSubmit);
75
+ }
76
+
77
+ // Handle Admin Actions
78
+ if (adminForm) {
79
+ adminApproveButton.addEventListener('click', () => handleAdminClick('approve'));
80
+ adminDeclineButton.addEventListener('click', () => handleAdminClick('decline'));
81
+ }
82
+
83
 
84
  // --- HANDLER FUNCTIONS ---
85
+
86
+ /**
87
+ * Handles the main form submission to create a new deal.
88
+ */
89
  async function handleFormSubmit(event) {
90
+ event.preventDefault(); // Stop page reload
 
 
91
 
92
+ showStatus(submissionStatus, 'Submitting...');
93
  submitButton.disabled = true;
 
94
 
95
  try {
96
  const formData = new FormData(submissionForm);
97
  const response = await fetch(workerUrl, { method: 'POST', body: formData });
98
+ const resultText = await response.text();
99
+
100
+ if (!response.ok) {
101
+ throw new Error(resultText); // Use the error text from the worker
102
+ }
103
+
104
+ const result = JSON.parse(resultText);
105
+ const dealId = result.deal_id;
106
+
107
+ // --- Auto-populate and Highlight Logic ---
108
+ showSuccess(submissionStatus, `Success! Deal ID copied below.`);
109
+ lookupInput.value = dealId; // Auto-populate the lookup field
110
 
111
+ lookupContainer.classList.add('highlight');
112
+ setTimeout(() => {
113
+ lookupContainer.classList.remove('highlight');
114
+ }, 2000);
115
+
116
  submissionForm.reset();
117
 
118
+ // Close the modal after a delay
119
+ setTimeout(() => closeModal(submissionModal), 2500);
120
 
121
  } catch (error) {
122
+ showError(submissionStatus, error.message);
123
  } finally {
124
  submitButton.disabled = false;
125
  }
126
  }
127
 
128
+ /**
129
+ * Handles the lookup form submission to check a deal's status.
130
+ */
131
+ async function handleLookupSubmit(event) {
132
+ event.preventDefault();
133
+ if (!lookupInput.value) return;
134
+
135
+ try {
136
+ const response = await fetch(`${workerUrl}?deal_id=${lookupInput.value}`, { method: 'GET' });
137
+ const result = await response.json();
138
+ if (!response.ok) throw new Error(result.error || 'Failed to fetch deal.');
139
+
140
+ openDealInfoModal(result);
141
+ } catch (error) {
142
+ alert(`Error: ${error.message}`);
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Handles clicks on the admin approve/decline buttons.
148
+ */
149
+ async function handleAdminClick(action) {
150
+ const dealId = document.getElementById('admin-deal-id').value;
151
+ if (!dealId) {
152
+ showError(adminStatus, 'Please enter a Deal ID.');
153
+ return;
154
+ }
155
+
156
+ showStatus(adminStatus, 'Processing action...');
157
+ const formData = new FormData();
158
+ formData.append('action', action);
159
+ formData.append('deal_id', dealId);
160
+
161
+ try {
162
+ const response = await fetch(workerUrl, { method: 'POST', body: formData });
163
+ const result = await response.json();
164
+ if (!response.ok) throw new Error(result.error || `Server responded with status ${response.status}`);
165
+
166
+ showSuccess(adminStatus, result.message);
167
+ } catch (error) {
168
+ showError(adminStatus, error.message);
169
+ }
170
+ }
171
+
172
+
173
+ // --- UI HELPER FUNCTIONS ---
174
 
175
+ /**
176
+ * Populates and opens the deal information modal.
177
+ */
178
+ function openDealInfoModal(dealData) {
179
+ document.getElementById('modal-deal-id').textContent = dealData.deal_id;
180
+ document.getElementById('modal-owner-name').textContent = dealData.owner_name;
181
+ document.getElementById('modal-contact-email').textContent = dealData.contact_email;
182
+ document.getElementById('modal-contact-phone').textContent = dealData.contact_phone || 'N/A';
183
+ document.getElementById('modal-year-built').textContent = dealData.year_built || 'N/A';
184
+ document.getElementById('modal-gross-tonnage').textContent = dealData.gross_tonnage || 'N/A';
185
+ document.getElementById('modal-token-amount').textContent = dealData.token_amount;
186
+ document.getElementById('modal-vessel-description').textContent = dealData.vessel_description || 'No description provided.';
187
+
188
+ const statusBadge = document.getElementById('modal-status-badge');
189
+ statusBadge.textContent = dealData.status;
190
+ statusBadge.className = `badge ${dealData.status}`;
191
+
192
+ dealModal.classList.remove('hidden');
193
+ }
194
+
195
+ /**
196
+ * Hides a modal element.
197
+ */
198
  function closeModal(modalElement) {
199
  modalElement.classList.add('hidden');
200
  }
201
+
202
+ /**
203
+ * Displays a generic status message.
204
+ */
205
  function showStatus(element, message) {
206
  element.className = 'status';
207
+ element.innerHTML = message;
208
  element.style.display = 'block';
209
  }
210
 
211
+ /**
212
+ * Displays a success message.
213
+ */
214
  function showSuccess(element, message) {
215
  element.className = 'status success';
216
+ element.innerHTML = `<strong>${message}</strong>`;
217
  element.style.display = 'block';
218
  }
219
 
220
+ /**
221
+ * Displays an error message.
222
+ */
223
  function showError(element, message) {
224
  element.className = 'status error';
225
  element.innerHTML = `<strong>Error:</strong> ${message}`;
226
  element.style.display = 'block';
227
  }
 
 
 
 
 
 
228
  });