broadfield-dev commited on
Commit
8aed38d
·
verified ·
1 Parent(s): 64231a2

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +19 -23
templates/index.html CHANGED
@@ -30,7 +30,7 @@
30
  <form method="POST" action="/search" id="searchForm">
31
  <input type="text" name="search" class="search-bar" placeholder="Search news...">
32
  </form>
33
- <button id="backButton" style="display: none; margin-top: 10px;" onclick="window.location.href='/back_to_main'">Back to Main</button>
34
  </div>
35
  {% if loading %}
36
  <div class="loading-message">Fetching new RSS feeds in the background...</div>
@@ -63,10 +63,10 @@
63
  <div class="no-articles">No articles available yet. Loading new feeds...</div>
64
  {% endif %}
65
 
66
- {% if loading %}
67
  <script>
68
  let lastUpdate = 0;
69
  let expandedCategories = new Set();
 
70
  function toggleCategory(category) {
71
  const spinner = document.getElementById(`spinner-${category}`);
72
  const tilesDiv = document.getElementById(`category-${category}`);
@@ -116,6 +116,7 @@
116
  });
117
  }
118
  }
 
119
  function updateArticles() {
120
  fetch('/get_updates')
121
  .then(response => response.json())
@@ -130,9 +131,7 @@
130
  const existingArticles = Array.from(tilesDiv.querySelectorAll('.article-tile'));
131
  let currentIds = new Set(existingArticles.map(a => a.dataset.id));
132
  let newHtml = '';
133
- // Sort new articles by published date
134
  articles.sort((a, b) => new Date(b.published) - new Date(a.published));
135
- // Add new articles to limited view (up to 10, keeping most recent)
136
  articles.slice(0, 10).forEach((article, index) => {
137
  if (!currentIds.has(index.toString())) {
138
  newHtml += `
@@ -150,13 +149,11 @@
150
  `;
151
  }
152
  });
153
- // Update limited view (up to 10)
154
  if (newHtml) {
155
  tilesDiv.innerHTML = (existingArticles.map(a => a.outerHTML).join('') + newHtml);
156
  const allLimited = Array.from(tilesDiv.querySelectorAll('.article-tile'));
157
  tilesDiv.innerHTML = allLimited.sort((a, b) => new Date(b.dataset.published) - new Date(a.dataset.published)).slice(0, 10).map(a => a.outerHTML).join('');
158
  }
159
- // Update expanded view if visible
160
  if (allTilesDiv.style.display === 'grid') {
161
  let allNewHtml = '';
162
  articles.forEach((article, index) => {
@@ -182,20 +179,21 @@
182
  }
183
  document.querySelector('.loading-message').style.display = 'none';
184
  }
185
- setTimeout(updateArticles, 2000); // Check every 2 seconds
186
  })
187
  .catch(error => {
188
  console.error('Error updating articles:', error);
189
- setTimeout(updateArticles, 2000); // Retry on error
190
  });
191
  }
 
192
  document.addEventListener('DOMContentLoaded', () => {
193
  const tiles = document.querySelectorAll('.tiles');
194
  tiles.forEach(tile => {
195
  lastUpdate = Math.max(lastUpdate, parseFloat(tile.dataset.lastUpdate) || 0);
196
  });
197
  updateArticles();
198
- // Handle semantic search
199
  const form = document.getElementById('searchForm');
200
  const backButton = document.getElementById('backButton');
201
  form.addEventListener('submit', (event) => {
@@ -204,11 +202,16 @@
204
  method: 'POST',
205
  body: new FormData(form)
206
  })
207
- .then(response => response.json())
 
 
 
208
  .then(data => {
209
  if (data.has_articles) {
210
- document.querySelector('.search-container').style.display = 'none';
211
  backButton.style.display = 'block';
 
 
 
212
  const categoriesHtml = Object.entries(data.categorized_articles).map(([cat, articles]) => `
213
  <div class="category-section">
214
  <div class="category-title" onclick="toggleCategory('${cat}')">${cat} <span class="loading-spinner" id="spinner-${cat}"></span></div>
@@ -230,17 +233,7 @@
230
  <div class="tiles" id="all-${cat}" style="display: none;"></div>
231
  </div>
232
  `).join('');
233
- document.querySelector('body').innerHTML = `
234
- <h1>News Feed Hub</h1>
235
- <div class="search-container" style="display: none;">
236
- <form method="POST" action="/search" id="searchForm">
237
- <input type="text" name="search" class="search-bar" placeholder="Search news...">
238
- </form>
239
- <button id="backButton" style="display: block; margin-top: 10px;" onclick="window.location.href='/back_to_main'">Back to Main</button>
240
- </div>
241
- ${categoriesHtml}
242
- ${loading ? '<div class="loading-message">Fetching new RSS feeds in the background...</div>' : ''}
243
- `;
244
  } else {
245
  alert('No results found.');
246
  }
@@ -250,8 +243,11 @@
250
  alert('Failed to perform search. Please try again.');
251
  });
252
  });
 
 
 
 
253
  });
254
  </script>
255
- {% endif %}
256
  </body>
257
  </html>
 
30
  <form method="POST" action="/search" id="searchForm">
31
  <input type="text" name="search" class="search-bar" placeholder="Search news...">
32
  </form>
33
+ <button id="backButton" style="display: none; margin-top: 10px;">Back to Main</button>
34
  </div>
35
  {% if loading %}
36
  <div class="loading-message">Fetching new RSS feeds in the background...</div>
 
63
  <div class="no-articles">No articles available yet. Loading new feeds...</div>
64
  {% endif %}
65
 
 
66
  <script>
67
  let lastUpdate = 0;
68
  let expandedCategories = new Set();
69
+
70
  function toggleCategory(category) {
71
  const spinner = document.getElementById(`spinner-${category}`);
72
  const tilesDiv = document.getElementById(`category-${category}`);
 
116
  });
117
  }
118
  }
119
+
120
  function updateArticles() {
121
  fetch('/get_updates')
122
  .then(response => response.json())
 
131
  const existingArticles = Array.from(tilesDiv.querySelectorAll('.article-tile'));
132
  let currentIds = new Set(existingArticles.map(a => a.dataset.id));
133
  let newHtml = '';
 
134
  articles.sort((a, b) => new Date(b.published) - new Date(a.published));
 
135
  articles.slice(0, 10).forEach((article, index) => {
136
  if (!currentIds.has(index.toString())) {
137
  newHtml += `
 
149
  `;
150
  }
151
  });
 
152
  if (newHtml) {
153
  tilesDiv.innerHTML = (existingArticles.map(a => a.outerHTML).join('') + newHtml);
154
  const allLimited = Array.from(tilesDiv.querySelectorAll('.article-tile'));
155
  tilesDiv.innerHTML = allLimited.sort((a, b) => new Date(b.dataset.published) - new Date(a.dataset.published)).slice(0, 10).map(a => a.outerHTML).join('');
156
  }
 
157
  if (allTilesDiv.style.display === 'grid') {
158
  let allNewHtml = '';
159
  articles.forEach((article, index) => {
 
179
  }
180
  document.querySelector('.loading-message').style.display = 'none';
181
  }
182
+ setTimeout(updateArticles, 2000);
183
  })
184
  .catch(error => {
185
  console.error('Error updating articles:', error);
186
+ setTimeout(updateArticles, 2000);
187
  });
188
  }
189
+
190
  document.addEventListener('DOMContentLoaded', () => {
191
  const tiles = document.querySelectorAll('.tiles');
192
  tiles.forEach(tile => {
193
  lastUpdate = Math.max(lastUpdate, parseFloat(tile.dataset.lastUpdate) || 0);
194
  });
195
  updateArticles();
196
+
197
  const form = document.getElementById('searchForm');
198
  const backButton = document.getElementById('backButton');
199
  form.addEventListener('submit', (event) => {
 
202
  method: 'POST',
203
  body: new FormData(form)
204
  })
205
+ .then(response => {
206
+ if (!response.ok) throw new Error('Network response was not ok');
207
+ return response.json();
208
+ })
209
  .then(data => {
210
  if (data.has_articles) {
 
211
  backButton.style.display = 'block';
212
+ const existingContent = document.querySelectorAll('.category-section');
213
+ existingContent.forEach(section => section.remove());
214
+
215
  const categoriesHtml = Object.entries(data.categorized_articles).map(([cat, articles]) => `
216
  <div class="category-section">
217
  <div class="category-title" onclick="toggleCategory('${cat}')">${cat} <span class="loading-spinner" id="spinner-${cat}"></span></div>
 
233
  <div class="tiles" id="all-${cat}" style="display: none;"></div>
234
  </div>
235
  `).join('');
236
+ document.querySelector('.search-container').insertAdjacentHTML('afterend', categoriesHtml);
 
 
 
 
 
 
 
 
 
 
237
  } else {
238
  alert('No results found.');
239
  }
 
243
  alert('Failed to perform search. Please try again.');
244
  });
245
  });
246
+
247
+ backButton.addEventListener('click', () => {
248
+ window.location.href = '/';
249
+ });
250
  });
251
  </script>
 
252
  </body>
253
  </html>