privateuserh commited on
Commit
5a96820
·
verified ·
1 Parent(s): 058247c

Update scripts/ui.js

Browse files
Files changed (1) hide show
  1. scripts/ui.js +54 -3
scripts/ui.js CHANGED
@@ -5,6 +5,15 @@ const BACKEND_URL = 'https://streamai-backend-v2.smplushypermedia.workers.dev';
5
  // --- Helper Functions ---
6
  const getElement = (id) => document.getElementById(id);
7
 
 
 
 
 
 
 
 
 
 
8
  // --- Card Rendering ---
9
  function createListingCard(item) {
10
  const card = document.createElement('div');
@@ -38,10 +47,52 @@ async function loadRecommendations() {
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
  }
 
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
+ const bgColor = isError ? 'bg-red-500' : 'bg-green-500';
12
+ container.innerHTML = `<div class="flex items-center text-white p-4 rounded-lg shadow-lg ${bgColor}"><i class="fas fa-info-circle mr-3"></i><p>${message}</p></div>`;
13
+ container.classList.add('show');
14
+ setTimeout(() => { container.classList.remove('show'); }, 4000);
15
+ }
16
+
17
  // --- Card Rendering ---
18
  function createListingCard(item) {
19
  const card = document.createElement('div');
 
47
  }
48
  }
49
 
50
+ async function handleFormSubmit(event) {
51
+ event.preventDefault();
52
+ const form = event.target;
53
+ const submitButton = getElement('submit-listing-btn');
54
+ submitButton.disabled = true;
55
+ submitButton.textContent = 'Submitting...';
56
+ const listingData = Object.fromEntries(new FormData(form).entries());
57
+
58
+ try {
59
+ const response = await fetch(`${BACKEND_URL}/api/add-listing`, {
60
+ method: 'POST',
61
+ headers: { 'Content-Type': 'application/json' },
62
+ body: JSON.stringify(listingData),
63
+ });
64
+ const result = await response.json();
65
+ if (!response.ok) throw new Error(result.details || 'Submission failed.');
66
+ showNotification('Success! Your recommendation has been added.');
67
+ const newCard = createListingCard(listingData);
68
+ getElement('recommendations-container').prepend(newCard);
69
+ getElement('form-container').classList.add('hidden');
70
+ getElement('form-chevron').classList.remove('rotate-180');
71
+ form.reset();
72
+ } catch (error) {
73
+ showNotification(`Error: ${error.message}`, true);
74
+ } finally {
75
+ submitButton.disabled = false;
76
+ submitButton.textContent = 'Submit Listing';
77
+ }
78
+ }
79
 
80
+ // --- Initialization ---
81
  export function initUI() {
82
+ const addListingForm = getElement('add-listing-form');
83
+ const toggleBtn = getElement('toggle-form-btn');
84
+ const formContainer = getElement('form-container');
85
+ const chevron = getElement('form-chevron');
86
+
87
+ if (toggleBtn && formContainer && chevron) {
88
+ toggleBtn.addEventListener('click', () => {
89
+ formContainer.classList.toggle('hidden');
90
+ chevron.classList.toggle('rotate-180');
91
+ });
92
+ }
93
+
94
+ if (addListingForm) {
95
+ addListingForm.addEventListener('submit', handleFormSubmit);
96
+ }
97
  loadRecommendations();
 
98
  }