privateuserh commited on
Commit
7c2bec2
·
verified ·
1 Parent(s): c32c896

Update scripts/ui.js

Browse files
Files changed (1) hide show
  1. scripts/ui.js +26 -12
scripts/ui.js CHANGED
@@ -1,22 +1,33 @@
1
- // in scripts/ui.js
 
 
 
2
 
3
  async function loadRecommendations() {
4
  const container = document.getElementById('recommendations-container');
5
  if (!container) return;
6
 
7
  try {
8
- const response = await fetch('https://stream-ai-backend.smplushypermedia.workers.dev/api/recommendations');
 
 
 
 
 
 
9
  const listings = await response.json();
10
 
11
  container.innerHTML = ''; // Clear previous listings
 
 
 
 
 
12
  listings.forEach(item => {
13
  const card = document.createElement('div');
14
- // Add a subtle border if the item is pinned
15
- const pinnedClass = item.is_pinned ? 'border-2 border-indigo-400' : '';
16
-
17
- card.className = `stream-card bg-white rounded-lg overflow-hidden shadow-md hover:shadow-xl transition duration-300 p-4 relative ${pinnedClass}`;
18
 
19
- // The card's content, now including the source_name
20
  card.innerHTML = `
21
  ${item.is_pinned ? '<div class="absolute top-2 right-2 text-xs bg-indigo-500 text-white px-2 py-1 rounded-full animate-pulse"><i class="fas fa-thumbtack mr-1"></i> Pinned</div>' : ''}
22
  <div class="mb-3">
@@ -25,9 +36,9 @@ async function loadRecommendations() {
25
  From: <span class="font-semibold text-indigo-600">${item.source_name}</span>
26
  </div>
27
  </div>
28
- <p class="text-gray-700 text-sm mb-4">${item.description}</p>
29
- <div class="flex justify-between items-center">
30
- <span class="text-xs font-bold uppercase text-gray-400">${item.category.replace('_', ' ')}</span>
31
  <a href="${item.url}" target="_blank" rel="noopener noreferrer" class="bg-indigo-600 hover:bg-indigo-700 text-white px-3 py-1 rounded-full text-xs font-medium transition">
32
  Visit Site
33
  </a>
@@ -36,11 +47,14 @@ async function loadRecommendations() {
36
  container.appendChild(card);
37
  });
38
  } catch (error) {
39
- container.innerHTML = `<p class="text-center text-red-500">Could not load recommendations.</p>`;
 
 
 
40
  }
41
  }
42
 
43
  export function initUI() {
44
- // This will run our function on page load
45
  loadRecommendations();
46
  }
 
1
+ // The complete ui.js file with the correct worker URL.
2
+
3
+ // Define the backend URL as a constant for easy access.
4
+ const BACKEND_URL = 'https://stream-ai-backend.smplushypermedia.workers.dev';
5
 
6
  async function loadRecommendations() {
7
  const container = document.getElementById('recommendations-container');
8
  if (!container) return;
9
 
10
  try {
11
+ // Fetch recommendations from your live backend.
12
+ const response = await fetch(`${BACKEND_URL}/api/recommendations`);
13
+
14
+ if (!response.ok) {
15
+ throw new Error(`Network response was not ok. Status: ${response.status}`);
16
+ }
17
+
18
  const listings = await response.json();
19
 
20
  container.innerHTML = ''; // Clear previous listings
21
+ if (listings.length === 0) {
22
+ container.innerHTML = `<p class="text-center col-span-full text-gray-500">No recommendations available yet. Be the first to add one!</p>`;
23
+ return;
24
+ }
25
+
26
  listings.forEach(item => {
27
  const card = document.createElement('div');
28
+ const pinnedClass = item.is_pinned ? 'border-2 border-indigo-400' : 'shadow-md';
29
+ card.className = `stream-card bg-white rounded-lg overflow-hidden hover:shadow-xl transition duration-300 p-4 relative ${pinnedClass}`;
 
 
30
 
 
31
  card.innerHTML = `
32
  ${item.is_pinned ? '<div class="absolute top-2 right-2 text-xs bg-indigo-500 text-white px-2 py-1 rounded-full animate-pulse"><i class="fas fa-thumbtack mr-1"></i> Pinned</div>' : ''}
33
  <div class="mb-3">
 
36
  From: <span class="font-semibold text-indigo-600">${item.source_name}</span>
37
  </div>
38
  </div>
39
+ <p class="text-gray-700 text-sm mb-4">${item.description || ''}</p>
40
+ <div class="flex justify-between items-center mt-auto">
41
+ <span class="text-xs font-bold uppercase text-gray-400">${(item.category || '').replace('_', ' ')}</span>
42
  <a href="${item.url}" target="_blank" rel="noopener noreferrer" class="bg-indigo-600 hover:bg-indigo-700 text-white px-3 py-1 rounded-full text-xs font-medium transition">
43
  Visit Site
44
  </a>
 
47
  container.appendChild(card);
48
  });
49
  } catch (error) {
50
+ console.error("Failed to load recommendations:", error);
51
+ if (container) {
52
+ container.innerHTML = `<p class="text-center col-span-full text-red-500">Could not load recommendations. Please ensure the backend is running and the URL is correct.</p>`;
53
+ }
54
  }
55
  }
56
 
57
  export function initUI() {
58
+ // This will run our function on page load to populate the recommendations.
59
  loadRecommendations();
60
  }