-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_page.js
43 lines (37 loc) · 2.03 KB
/
data_page.js
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
document.addEventListener('DOMContentLoaded', function() {
const tagsContainer = document.getElementById('tags-container');
const dataContainer = document.getElementById('data-container');
// Handle tag click to filter videos
tagsContainer.addEventListener('click', function(event) {
if (event.target.classList.contains('tag')) {
// Toggle selected state
document.querySelectorAll('.tag').forEach(tag => tag.classList.remove('selected'));
event.target.classList.add('selected');
// Get the selected tag ID
const selectedTag = event.target.id;
// Fetch filtered data by tag from the server
fetch(`http://localhost:5000/api/get-cached-videos/${selectedTag}`)
.then(response => response.json())
.then(data => {
// Loop through the videos and update the video cards
data.forEach((entry, index) => {
if (index < 4) {
const card = dataContainer.children[index];
// Update video thumbnail, title, and view count
card.querySelector('.video-thumbnail').src = entry.thumbnail_url;
card.querySelector('.video-title').textContent = entry.title;
card.querySelector('.video-views').textContent = `Views: ${entry.view_count}`;
const thumbnailElement = card.querySelector('.video-thumbnail');
thumbnailElement.addEventListener('click', function() {
window.open(`https://www.youtube.com/watch?v=${entry.video_id}`, '_blank');
});
}
});
})
.catch(error => {
console.error('Error fetching data by tag:', error);
dataContainer.innerHTML = '<p>Error loading data.</p>';
});
}
});
});