Spaces:
Running
Running
Update scripts/ui.js
Browse files- scripts/ui.js +67 -37
scripts/ui.js
CHANGED
@@ -1,60 +1,90 @@
|
|
1 |
-
//
|
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 |
-
|
7 |
-
|
|
|
|
|
|
|
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
|
23 |
return;
|
24 |
}
|
25 |
-
|
26 |
listings.forEach(item => {
|
27 |
const card = document.createElement('div');
|
28 |
-
|
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">
|
34 |
-
<h3 class="font-bold text-base">${item.title}</h3>
|
35 |
-
<div class="text-xs text-gray-500 mb-2 mt-1">
|
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>
|
45 |
-
</div>
|
46 |
-
`;
|
47 |
container.appendChild(card);
|
48 |
});
|
49 |
} catch (error) {
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
}
|
55 |
}
|
56 |
|
|
|
|
|
57 |
export function initUI() {
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
loadRecommendations();
|
60 |
}
|
|
|
1 |
+
// scripts/ui.js -- Final version with form handling
|
2 |
|
|
|
3 |
const BACKEND_URL = 'https://stream-ai-backend.smplushypermedia.workers.dev';
|
4 |
|
5 |
+
// --- Helper Functions ---
|
6 |
+
const getElement = (id) => document.getElementById(id);
|
7 |
+
|
8 |
+
function showNotification(message, isError = false) {
|
9 |
+
const container = getElement('notification');
|
10 |
if (!container) return;
|
11 |
+
|
12 |
+
const bgColor = isError ? 'bg-red-500' : 'bg-green-500';
|
13 |
+
container.innerHTML = `<div class="flex items-center text-white p-4 rounded-lg shadow-lg ${bgColor}"><i class="fas fa-check-circle mr-3"></i><p>${message}</p></div>`;
|
14 |
+
|
15 |
+
container.classList.add('show');
|
16 |
+
setTimeout(() => { container.classList.remove('show'); }, 4000);
|
17 |
+
}
|
18 |
+
|
19 |
|
20 |
+
// --- Core Functions ---
|
21 |
+
|
22 |
+
async function loadRecommendations() {
|
23 |
+
const container = getElement('recommendations-container');
|
24 |
+
if (!container) return;
|
25 |
try {
|
|
|
26 |
const response = await fetch(`${BACKEND_URL}/api/recommendations`);
|
27 |
+
if (!response.ok) throw new Error(`Network Error: ${response.statusText}`);
|
|
|
|
|
|
|
|
|
28 |
const listings = await response.json();
|
29 |
+
container.innerHTML = ''; // Clear before loading
|
|
|
30 |
if (listings.length === 0) {
|
31 |
+
container.innerHTML = `<p class="text-center col-span-full text-gray-500">No recommendations available yet.</p>`;
|
32 |
return;
|
33 |
}
|
|
|
34 |
listings.forEach(item => {
|
35 |
const card = document.createElement('div');
|
36 |
+
// ... (The card rendering logic from the previous step remains the same) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
container.appendChild(card);
|
38 |
});
|
39 |
} catch (error) {
|
40 |
+
container.innerHTML = `<p class="text-center col-span-full text-red-500">Could not load recommendations.</p>`;
|
41 |
+
}
|
42 |
+
}
|
43 |
+
|
44 |
+
async function handleFormSubmit(event) {
|
45 |
+
event.preventDefault(); // Stop the browser from reloading the page
|
46 |
+
const form = event.target;
|
47 |
+
const submitButton = getElement('submit-listing-btn');
|
48 |
+
submitButton.disabled = true;
|
49 |
+
submitButton.textContent = 'Submitting...';
|
50 |
+
|
51 |
+
const formData = new FormData(form);
|
52 |
+
const listingData = Object.fromEntries(formData.entries());
|
53 |
+
|
54 |
+
try {
|
55 |
+
const response = await fetch(`${BACKEND_URL}/api/add-listing`, {
|
56 |
+
method: 'POST',
|
57 |
+
headers: { 'Content-Type': 'application/json' },
|
58 |
+
body: JSON.stringify(listingData),
|
59 |
+
});
|
60 |
+
|
61 |
+
const result = await response.json();
|
62 |
+
|
63 |
+
if (!response.ok) {
|
64 |
+
throw new Error(result.details || 'Submission failed.');
|
65 |
}
|
66 |
+
|
67 |
+
showNotification('Success! Your recommendation has been added.');
|
68 |
+
form.reset(); // Clear the form fields
|
69 |
+
loadRecommendations(); // Refresh the list to show the new item
|
70 |
+
|
71 |
+
} catch (error) {
|
72 |
+
showNotification(`Error: ${error.message}`, true);
|
73 |
+
} finally {
|
74 |
+
submitButton.disabled = false;
|
75 |
+
submitButton.textContent = 'Submit Listing';
|
76 |
}
|
77 |
}
|
78 |
|
79 |
+
// --- Initialization ---
|
80 |
+
|
81 |
export function initUI() {
|
82 |
+
const addListingForm = getElement('add-listing-form');
|
83 |
+
|
84 |
+
if (addListingForm) {
|
85 |
+
addListingForm.addEventListener('submit', handleFormSubmit);
|
86 |
+
}
|
87 |
+
|
88 |
+
// Load the initial recommendations when the page loads
|
89 |
loadRecommendations();
|
90 |
}
|