// scripts/ui.js -- FINAL VERSION const BACKEND_URL = 'https://streamai-backend-v2.smplushypermedia.workers.dev'; // --- Helper Functions --- const getElement = (id) => document.getElementById(id); function showNotification(message, isError = false) { const container = getElement('notification'); if (!container) return; const bgColor = isError ? 'bg-red-500' : 'bg-green-500'; container.innerHTML = `
${message}
${item.description || ''}
No recommendations available yet.
`; return; } listings.forEach(item => container.appendChild(createListingCard(item))); } catch (error) { container.innerHTML = `Could not load recommendations.
`; } } async function handleFormSubmit(event) { event.preventDefault(); const form = event.target; const submitButton = getElement('submit-listing-btn'); submitButton.disabled = true; submitButton.textContent = 'Submitting...'; const listingData = Object.fromEntries(new FormData(form).entries()); try { const response = await fetch(`${BACKEND_URL}/api/add-listing`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(listingData), }); const result = await response.json(); if (!response.ok) throw new Error(result.details || 'Submission failed.'); showNotification('Success! Your recommendation has been added.'); const newCard = createListingCard(listingData); getElement('recommendations-container').prepend(newCard); getElement('form-container').classList.add('hidden'); getElement('form-chevron').classList.remove('rotate-180'); form.reset(); } catch (error) { showNotification(`Error: ${error.message}`, true); } finally { submitButton.disabled = false; submitButton.textContent = 'Submit Listing'; } } // --- Initialization --- export function initUI() { const addListingForm = getElement('add-listing-form'); const toggleBtn = getElement('toggle-form-btn'); const formContainer = getElement('form-container'); const chevron = getElement('form-chevron'); if (toggleBtn && formContainer && chevron) { toggleBtn.addEventListener('click', () => { formContainer.classList.toggle('hidden'); chevron.classList.toggle('rotate-180'); }); } if (addListingForm) { addListingForm.addEventListener('submit', handleFormSubmit); } loadRecommendations(); }