-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
311 lines (259 loc) · 9.13 KB
/
Copy pathscript.js
File metadata and controls
311 lines (259 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// API Configuration - loaded from config.js
function getApiConfig() {
return window.appConfig || {
apiUrl: 'http://localhost:5000/api/records',
healthUrl: 'http://localhost:5000/',
baseUrl: 'http://localhost:5000'
};
}
// DOM Elements
const nameInput = document.getElementById('nameInput');
const messageInput = document.getElementById('messageInput');
const noteInput = document.getElementById('noteInput');
const sendBtn = document.getElementById('sendBtn');
const viewDataBtn = document.getElementById('viewDataBtn');
const testConnectionBtn = document.getElementById('testConnectionBtn');
const dataContainer = document.getElementById('dataContainer');
const tableBody = document.getElementById('tableBody');
const notification = document.getElementById('notification');
const connectionStatus = document.getElementById('connectionStatus');
const connectionResult = document.getElementById('connectionResult');
// State
let isDataVisible = false;
// Initialize the application
document.addEventListener('DOMContentLoaded', function() {
setupEventListeners();
});
// Event Listeners
function setupEventListeners() {
sendBtn.addEventListener('click', handleSendData);
viewDataBtn.addEventListener('click', handleViewData);
testConnectionBtn.addEventListener('click', handleTestConnection);
// Allow Enter key to submit form
nameInput.addEventListener('keypress', handleKeyPress);
messageInput.addEventListener('keypress', handleKeyPress);
noteInput.addEventListener('keypress', handleKeyPress);
}
function handleKeyPress(event) {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault();
handleSendData();
}
}
// Send data to backend
async function handleSendData() {
const name = nameInput.value.trim();
const message = messageInput.value.trim();
const note = noteInput.value.trim();
// Validation
if (!name) {
showNotification('Please enter your name', 'error');
nameInput.focus();
return;
}
if (!message) {
showNotification('Please enter your message', 'error');
messageInput.focus();
return;
}
// Disable button during request
setLoadingState(sendBtn, true);
try {
const requestData = {
name: name,
message: message
};
// Add note only if it's not empty
if (note) {
requestData.note = note;
}
const response = await fetch(getApiConfig().apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestData)
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || `HTTP error! status: ${response.status}`);
}
const data = await response.json();
showNotification('Data sent successfully!', 'success');
// Clear form
nameInput.value = '';
messageInput.value = '';
noteInput.value = '';
// If data is currently visible, refresh it
if (isDataVisible) {
await loadData();
}
} catch (error) {
console.error('Error sending data:', error);
showNotification(`Error: ${error.message}`, 'error');
} finally {
setLoadingState(sendBtn, false);
}
}
// View/Hide data
async function handleViewData() {
if (isDataVisible) {
hideData();
} else {
await showData();
}
}
async function showData() {
setLoadingState(viewDataBtn, true);
try {
await loadData();
dataContainer.style.display = 'block';
viewDataBtn.textContent = 'Hide Data';
isDataVisible = true;
// Smooth scroll to data
dataContainer.scrollIntoView({ behavior: 'smooth' });
} catch (error) {
console.error('Error loading data:', error);
showNotification(`Error loading data: ${error.message}`, 'error');
} finally {
setLoadingState(viewDataBtn, false);
}
}
function hideData() {
dataContainer.style.display = 'none';
viewDataBtn.textContent = 'View Data';
isDataVisible = false;
}
// Test connection to backend
async function handleTestConnection() {
setLoadingState(testConnectionBtn, true);
connectionStatus.style.display = 'block';
connectionStatus.className = 'connection-status';
const config = getApiConfig();
connectionResult.textContent = 'Testing connection...';
try {
const startTime = performance.now();
const response = await fetch(config.healthUrl, {
method: 'GET',
headers: {
'Accept': 'application/json'
}
});
const endTime = performance.now();
const responseTime = Math.round(endTime - startTime);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
// Format successful response
const result = {
status: 'SUCCESS',
url: config.healthUrl,
responseTime: `${responseTime}ms`,
httpStatus: response.status,
response: data,
timestamp: new Date().toISOString()
};
connectionResult.textContent = JSON.stringify(result, null, 2);
connectionStatus.className = 'connection-status success';
showNotification('Connection test successful!', 'success');
} catch (error) {
console.error('Connection test failed:', error);
const result = {
status: 'ERROR',
url: config.healthUrl,
error: error.message,
timestamp: new Date().toISOString(),
config: config.getConfig ? config.getConfig() : 'Config not available'
};
connectionResult.textContent = JSON.stringify(result, null, 2);
connectionStatus.className = 'connection-status error';
showNotification(`Connection test failed: ${error.message}`, 'error');
} finally {
setLoadingState(testConnectionBtn, false);
}
}
// Load data from backend
async function loadData() {
try {
const response = await fetch(getApiConfig().apiUrl);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
// Extract records array from response
const records = data.records || data;
renderTable(records);
} catch (error) {
console.error('Error fetching data:', error);
throw error;
}
}
// Render data in table
function renderTable(records) {
// Clear existing rows
tableBody.innerHTML = '';
if (!records || !Array.isArray(records) || records.length === 0) {
const emptyRow = document.createElement('tr');
emptyRow.innerHTML = `
<td colspan="4" class="empty-state">
No records found. Send some data first!
</td>
`;
tableBody.appendChild(emptyRow);
return;
}
// Sort records by created_at (newest first)
records.sort((a, b) => new Date(b.created_at) - new Date(a.created_at));
records.forEach(record => {
const row = document.createElement('tr');
// Format timestamp
const timestamp = new Date(record.created_at).toLocaleString();
row.innerHTML = `
<td>${escapeHtml(record.name)}</td>
<td>${escapeHtml(record.message)}</td>
<td>${record.note ? escapeHtml(record.note) : '<em>No note</em>'}</td>
<td>${timestamp}</td>
`;
tableBody.appendChild(row);
});
}
// Utility Functions
function showNotification(message, type = 'info') {
notification.textContent = message;
notification.className = `notification ${type}`;
notification.style.display = 'block';
// Auto-hide after 5 seconds
setTimeout(() => {
notification.style.display = 'none';
}, 5000);
}
function setLoadingState(button, isLoading) {
if (isLoading) {
button.disabled = true;
button.classList.add('loading');
button.dataset.originalText = button.textContent;
button.textContent = 'Loading...';
} else {
button.disabled = false;
button.classList.remove('loading');
button.textContent = button.dataset.originalText || button.textContent;
}
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Error handling for uncaught errors
window.addEventListener('error', function(event) {
console.error('Global error:', event.error);
showNotification('An unexpected error occurred', 'error');
});
// Handle network errors
window.addEventListener('online', function() {
showNotification('Connection restored', 'success');
});
window.addEventListener('offline', function() {
showNotification('Connection lost. Please check your internet connection.', 'error');
});