Skip to content

Commit f49fabd

Browse files
committed
Count2
1 parent 7dad57f commit f49fabd

File tree

1 file changed

+78
-82
lines changed

1 file changed

+78
-82
lines changed

static/scripts.js

Lines changed: 78 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// 165-356
1+
// 165-349
22

33
document.addEventListener('DOMContentLoaded', function() {
44
// Initialize the chess board
@@ -162,27 +162,25 @@ function initContactForm() {
162162
}
163163

164164

165+
165166
// Visitor counter functionality
166167
function initVisitorCounter() {
167168
const visitorCountElement = document.getElementById('visitor-count');
168169
const visitorOrdinalElement = document.getElementById('visitor-ordinal');
169170
const miniChessPieces = document.querySelectorAll('.mini-chess-piece');
170171

171-
// Your API Gateway Endpoint URL
172-
const API_URL = 'https://4bfefcldxk.execute-api.us-east-1.amazonaws.com/Prod/visitor'; // **MAKE SURE THIS IS YOUR ACTUAL API GATEWAY URL**
173-
174172
if (!visitorCountElement || !visitorOrdinalElement) return;
175173

176174
// Set initial loading state
177175
visitorCountElement.textContent = '...';
178176
visitorOrdinalElement.textContent = '';
179177

180-
// Function to update the visitor count display with fancy animation (Your existing function)
178+
// Function to update the visitor count display with fancy animation
181179
function updateVisitorDisplay(count) {
182-
if (typeof count !== 'number' && count !== 0) { // Improved check for valid count
180+
// Keep your existing updateVisitorDisplay logic here
181+
if (!count && count !== 0) {
183182
visitorCountElement.textContent = 'Error';
184183
visitorOrdinalElement.textContent = '';
185-
console.error('Invalid count received:', count);
186184
return;
187185
}
188186

@@ -203,7 +201,8 @@ function initVisitorCounter() {
203201
// Set the ordinal text immediately
204202
visitorOrdinalElement.textContent = count + ordinal;
205203

206-
// Then start the animations (Your existing anime.js animations)
204+
// Then start the animations (if you still want them)
205+
// Keep your anime.timeline code here if desired
207206
anime.timeline({
208207
easing: 'easeOutExpo',
209208
})
@@ -244,116 +243,113 @@ function initVisitorCounter() {
244243
offset: '-=400'
245244
});
246245

247-
// Update the mini chess pieces with different floating animations
248-
if (miniChessPieces) { // Add check if elements exist
249-
miniChessPieces.forEach((piece, index) => {
250-
const delay = index * 300;
251-
const duration = 5000 + (index * 1000);
246+
// Update the mini chess pieces animations (if desired)
247+
miniChessPieces.forEach((piece, index) => {
248+
const delay = index * 300;
249+
const duration = 5000 + (index * 1000);
252250

253-
anime({
254-
targets: piece,
255-
opacity: [0, 0.7],
256-
translateY: function() {
257-
return anime.random(-30, 30);
258-
},
259-
translateX: function() {
260-
return anime.random(-20, 20);
261-
},
262-
scale: [0.5, 1],
263-
rotate: [0, anime.random(-25, 25)],
264-
delay: delay,
265-
duration: 1200,
266-
easing: 'easeOutElastic(1, .6)',
267-
complete: function() {
268-
// After appearing, start floating animation
269-
anime({
270-
targets: piece,
271-
translateY: ['-10px', '10px'],
272-
translateX: ['-5px', '5px'],
273-
rotate: ['-5deg', '5deg'],
274-
duration: duration,
275-
direction: 'alternate',
276-
loop: true,
277-
easing: 'easeInOutSine'
278-
});
279-
}
280-
});
251+
anime({
252+
targets: piece,
253+
opacity: [0, 0.7],
254+
translateY: function() {
255+
return anime.random(-30, 30);
256+
},
257+
translateX: function() {
258+
return anime.random(-20, 20);
259+
},
260+
scale: [0.5, 1],
261+
rotate: [0, anime.random(-25, 25)],
262+
delay: delay,
263+
duration: 1200,
264+
easing: 'easeOutElastic(1, .6)',
265+
complete: function() {
266+
// After appearing, start floating animation
267+
anime({
268+
targets: piece,
269+
translateY: ['-10px', '10px'],
270+
translateX: ['-5px', '5px'],
271+
rotate: ['-5deg', '5deg'],
272+
duration: duration,
273+
direction: 'alternate',
274+
loop: true,
275+
easing: 'easeInOutSine'
276+
});
277+
}
281278
});
282-
}
283-
279+
});
284280

285-
// Animate the badge
286-
anime({
281+
// Animate the badge (if desired)
282+
anime({
287283
targets: '.counter-badge',
288284
translateY: [20, 0],
289285
opacity: [0, 1],
290286
scale: [0.9, 1],
291287
duration: 800,
292288
easing: 'easeOutQuad'
289+
// ... (rest of animation code)
293290
});
291+
}
294292

295-
} // End of updateVisitorDisplay
293+
// --- Add the Fetch API calls here ---
296294

297-
// --- ADD FETCH CALLS HERE ---
295+
const API_URL = 'YOUR_API_GATEWAY_INVOKE_URL'; // <--- REPLACE WITH YOUR API URL
298296

299-
// 1. Make a POST request to potentially increment the count
297+
// 1. Send POST request to trigger backend logic (record IP and potentially increment count)
300298
fetch(API_URL, {
301299
method: 'POST',
302300
headers: {
303301
'Content-Type': 'application/json',
302+
// Add other headers if required by your API Gateway config, though not needed for proxy integration usually
304303
},
304+
// For a simple visitor counter, you don't need to send a body with POST
305305
})
306306
.then(response => {
307-
if (!response.ok) {
308-
console.error('Error incrementing visitor count (POST failed):', response.status, response.statusText);
309-
// You might want to log the response body for more details in development
310-
// response.text().then(text => console.error('POST response body:', text));
311-
}
312-
// We don't necessarily need to process the POST response body for frontend display
307+
// Log the response status and body for debugging
308+
console.log('POST request status:', response.status);
309+
return response.json(); // Parse the JSON response from Lambda
313310
})
314-
.catch(error => {
315-
console.error('Error during POST fetch for visitor counter:', error);
316-
});
317-
318-
// 2. Make a GET request to fetch the current count
319-
fetch(API_URL, {
320-
method: 'GET',
321-
headers: {
322-
'Content-Type': 'application/json',
323-
},
324-
// Add cache control headers to potentially prevent browser caching if needed
325-
// cache: 'no-cache', // This can help during development to ensure fresh data
311+
.then(postData => {
312+
console.log('POST request result:', postData.message);
313+
// The POST response just confirms the backend action, it doesn't contain the final count
314+
315+
// 2. After the POST, immediately send a GET request to fetch the latest count
316+
return fetch(API_URL, {
317+
method: 'GET',
318+
headers: {
319+
// GET requests typically don't need a body
320+
'Content-Type': 'application/json', // Still good practice
321+
},
322+
});
326323
})
327324
.then(response => {
325+
// Log the GET response status for debugging
326+
console.log('GET request status:', response.status);
328327
if (!response.ok) {
329-
// Handle HTTP errors (e.g., 404, 500)
330-
console.error('Error fetching visitor count (GET failed):', response.status, response.statusText);
331-
visitorCountElement.textContent = 'Error';
332-
visitorOrdinalElement.textContent = '';
333-
// You might want to log the response body for more details in development
334-
// response.text().then(text => console.error('GET response body:', text));
335-
return Promise.reject('API error fetching count'); // Stop processing
328+
throw new Error(`GET request failed with status ${response.status}`);
336329
}
337-
return response.json(); // Parse the JSON response
330+
return response.json(); // Parse the JSON response from Lambda
338331
})
339-
.then(data => {
340-
// Check if the expected data structure exists and has a number count
341-
if (data && typeof data.visitorCount === 'number') {
342-
// Pass the fetched count to the display function
343-
updateVisitorDisplay(data.visitorCount);
332+
.then(getData => {
333+
console.log('GET request result:', getData);
334+
// Call the display function with the fetched count
335+
if (getData && typeof getData.visitorCount === 'number') {
336+
updateVisitorDisplay(getData.visitorCount);
344337
} else {
345-
console.error('Invalid response data format for visitor count:', data);
338+
console.error("GET response did not contain a valid visitorCount:", getData);
346339
visitorCountElement.textContent = 'Error';
347340
visitorOrdinalElement.textContent = '';
348341
}
349342
})
350343
.catch(error => {
351-
console.error('Error during GET fetch for visitor counter:', error);
344+
console.error('Error fetching or updating visitor count:', error);
345+
// Display an error message if the API calls fail
352346
visitorCountElement.textContent = 'Error';
353347
visitorOrdinalElement.textContent = '';
354348
});
349+
}
355350

356-
} // End of initVisitorCounter function
351+
// Keep your other init functions below this...
352+
// initChessBoard(), initMobileMenu(), etc.
357353

358354

359355

0 commit comments

Comments
 (0)