Spaces:
Running
Running
Update scripts/ui.js
Browse files- scripts/ui.js +6 -65
scripts/ui.js
CHANGED
@@ -1,27 +1,17 @@
|
|
1 |
-
// scripts/ui.js --
|
2 |
|
3 |
-
|
4 |
-
const BACKEND_URL = 'https://streamai-backend-v2.smplushypermedia.workers.dev/api/recommendations';
|
5 |
|
6 |
// --- Helper Functions ---
|
7 |
const getElement = (id) => document.getElementById(id);
|
8 |
|
9 |
-
function showNotification(message, isError = false) {
|
10 |
-
const container = getElement('notification');
|
11 |
-
if (!container) return;
|
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 |
-
container.classList.add('show');
|
15 |
-
setTimeout(() => { container.classList.remove('show'); }, 4000);
|
16 |
-
}
|
17 |
-
|
18 |
// --- Card Rendering ---
|
19 |
function createListingCard(item) {
|
20 |
const card = document.createElement('div');
|
21 |
const pinnedClass = item.is_pinned ? 'border-2 border-indigo-400' : 'shadow-md';
|
22 |
card.className = `stream-card bg-white rounded-lg overflow-hidden hover:shadow-xl transition duration-300 p-4 relative ${pinnedClass}`;
|
23 |
card.innerHTML = `
|
24 |
-
${item.is_pinned ? '<div class="absolute top-2 right-2 text-xs bg-indigo-500 text-white px-2 py-1 rounded-full
|
25 |
<div class="mb-3"><h3 class="font-bold text-base">${item.title}</h3><div class="text-xs text-gray-500 mb-2 mt-1">From: <span class="font-semibold text-indigo-600">${item.source_name}</span></div></div>
|
26 |
<p class="text-gray-700 text-sm mb-4">${item.description || ''}</p>
|
27 |
<div class="flex justify-between items-center mt-auto"><span class="text-xs font-bold uppercase text-gray-400">${(item.category || '').replace('_', ' ')}</span><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">Visit Site</a></div>
|
@@ -48,59 +38,10 @@ async function loadRecommendations() {
|
|
48 |
}
|
49 |
}
|
50 |
|
51 |
-
|
52 |
-
event.preventDefault();
|
53 |
-
const form = event.target;
|
54 |
-
const submitButton = getElement('submit-listing-btn');
|
55 |
-
submitButton.disabled = true;
|
56 |
-
submitButton.textContent = 'Submitting...';
|
57 |
-
|
58 |
-
const listingData = Object.fromEntries(new FormData(form).entries());
|
59 |
-
|
60 |
-
try {
|
61 |
-
const response = await fetch(`${BACKEND_URL}/api/add-listing`, {
|
62 |
-
method: 'POST',
|
63 |
-
headers: { 'Content-Type': 'application/json' },
|
64 |
-
body: JSON.stringify(listingData),
|
65 |
-
});
|
66 |
-
const result = await response.json();
|
67 |
-
if (!response.ok) throw new Error(result.details || 'Submission failed.');
|
68 |
-
|
69 |
-
showNotification('Success! Your recommendation has been added.');
|
70 |
-
|
71 |
-
const newCard = createListingCard(listingData);
|
72 |
-
getElement('recommendations-container').prepend(newCard);
|
73 |
-
|
74 |
-
getElement('form-container').classList.add('hidden');
|
75 |
-
getElement('form-chevron').classList.remove('rotate-180');
|
76 |
-
|
77 |
-
form.reset();
|
78 |
-
|
79 |
-
} catch (error) {
|
80 |
-
showNotification(`Error: ${error.message}`, true);
|
81 |
-
} finally {
|
82 |
-
submitButton.disabled = false;
|
83 |
-
submitButton.textContent = 'Submit Listing';
|
84 |
-
}
|
85 |
-
}
|
86 |
|
87 |
-
// --- Initialization ---
|
88 |
export function initUI() {
|
89 |
-
|
90 |
-
const toggleBtn = getElement('toggle-form-btn');
|
91 |
-
const formContainer = getElement('form-container');
|
92 |
-
const chevron = getElement('form-chevron');
|
93 |
-
|
94 |
-
if (toggleBtn && formContainer && chevron) {
|
95 |
-
toggleBtn.addEventListener('click', () => {
|
96 |
-
formContainer.classList.toggle('hidden');
|
97 |
-
chevron.classList.toggle('rotate-180');
|
98 |
-
});
|
99 |
-
}
|
100 |
-
|
101 |
-
if (addListingForm) {
|
102 |
-
addListingForm.addEventListener('submit', handleFormSubmit);
|
103 |
-
}
|
104 |
-
|
105 |
loadRecommendations();
|
|
|
106 |
}
|
|
|
1 |
+
// scripts/ui.js -- FINAL VERSION
|
2 |
|
3 |
+
const BACKEND_URL = 'https://streamai-backend-v2.smplushypermedia.workers.dev';
|
|
|
4 |
|
5 |
// --- Helper Functions ---
|
6 |
const getElement = (id) => document.getElementById(id);
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
// --- Card Rendering ---
|
9 |
function createListingCard(item) {
|
10 |
const card = document.createElement('div');
|
11 |
const pinnedClass = item.is_pinned ? 'border-2 border-indigo-400' : 'shadow-md';
|
12 |
card.className = `stream-card bg-white rounded-lg overflow-hidden hover:shadow-xl transition duration-300 p-4 relative ${pinnedClass}`;
|
13 |
card.innerHTML = `
|
14 |
+
${item.is_pinned ? '<div class="absolute top-2 right-2 text-xs bg-indigo-500 text-white px-2 py-1 rounded-full"><i class="fas fa-thumbtack mr-1"></i> Pinned</div>' : ''}
|
15 |
<div class="mb-3"><h3 class="font-bold text-base">${item.title}</h3><div class="text-xs text-gray-500 mb-2 mt-1">From: <span class="font-semibold text-indigo-600">${item.source_name}</span></div></div>
|
16 |
<p class="text-gray-700 text-sm mb-4">${item.description || ''}</p>
|
17 |
<div class="flex justify-between items-center mt-auto"><span class="text-xs font-bold uppercase text-gray-400">${(item.category || '').replace('_', ' ')}</span><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">Visit Site</a></div>
|
|
|
38 |
}
|
39 |
}
|
40 |
|
41 |
+
// ... (The rest of your ui.js file, including initUI, handleFormSubmit, showNotification, etc., can remain the same or you can paste the full version from previous steps if you are unsure) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
|
|
43 |
export function initUI() {
|
44 |
+
// This will run our function on page load to populate the recommendations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
loadRecommendations();
|
46 |
+
// ... other event listeners for the form and video button ...
|
47 |
}
|