broadfield-dev commited on
Commit
5d4d8d0
·
verified ·
1 Parent(s): ae30ab9

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +54 -25
templates/index.html CHANGED
@@ -36,9 +36,9 @@
36
  {% for category, articles in categorized_articles.items() %}
37
  <div class="category-section">
38
  <div class="category-title">{{ category }}</div>
39
- <div class="tiles" id="category-{{ category }}">
40
  {% for article in articles %}
41
- <div class="article-tile" data-published="{{ article.published }}">
42
  {% if article.image != "svg" %}
43
  <img src="{{ article.image }}" alt="Article Image">
44
  {% else %}
@@ -61,40 +61,69 @@
61
 
62
  {% if loading %}
63
  <script>
64
- function checkLoadingStatus() {
65
- fetch('/check_loading')
 
 
66
  .then(response => response.json())
67
  .then(data => {
68
- if (data.status === "complete") {
69
- // Soft refresh: fetch updated articles without full reload
70
- fetch('/')
71
- .then(response => response.text())
72
- .then(html => {
73
- const parser = new DOMParser();
74
- const doc = parser.parseFromString(html, 'text/html');
75
- const newCategories = doc.querySelectorAll('.category-section');
76
- newCategories.forEach(newCat => {
77
- const catId = newCat.querySelector('.tiles').id;
78
- const existingCat = document.getElementById(catId);
79
- if (existingCat) {
80
- existingCat.innerHTML = newCat.querySelector('.tiles').innerHTML;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  }
82
  });
83
- document.querySelector('.loading-message').style.display = 'none';
84
- })
85
- .catch(error => console.error('Error updating articles:', error));
86
- } else {
87
- setTimeout(checkLoadingStatus, 2000); // Check every 2 seconds
 
 
 
 
 
 
88
  }
 
89
  })
90
  .catch(error => {
91
- console.error('Error checking loading status:', error);
92
- setTimeout(checkLoadingStatus, 2000); // Retry on error
93
  });
94
  }
95
 
96
  document.addEventListener('DOMContentLoaded', () => {
97
- checkLoadingStatus();
 
 
 
 
98
  });
99
  </script>
100
  {% endif %}
 
36
  {% for category, articles in categorized_articles.items() %}
37
  <div class="category-section">
38
  <div class="category-title">{{ category }}</div>
39
+ <div class="tiles" id="category-{{ category }}" data-last-update="0">
40
  {% for article in articles %}
41
+ <div class="article-tile" data-published="{{ article.published }}" data-id="{{ loop.index }}">
42
  {% if article.image != "svg" %}
43
  <img src="{{ article.image }}" alt="Article Image">
44
  {% else %}
 
61
 
62
  {% if loading %}
63
  <script>
64
+ let lastUpdate = 0;
65
+
66
+ function updateArticles() {
67
+ fetch('/get_updates')
68
  .then(response => response.json())
69
  .then(data => {
70
+ if (data.articles && data.last_update > lastUpdate) {
71
+ lastUpdate = data.last_update;
72
+ const newArticles = data.articles;
73
+ for (const [category, articles] of Object.entries(newArticles)) {
74
+ const tilesDiv = document.getElementById(`category-${category}`);
75
+ if (tilesDiv) {
76
+ const existingArticles = Array.from(tilesDiv.querySelectorAll('.article-tile'));
77
+ let currentIds = new Set(existingArticles.map(a => a.dataset.id));
78
+ let newHtml = '';
79
+
80
+ // Sort new articles by published date
81
+ articles.sort((a, b) => new Date(b.published) - new Date(a.published));
82
+
83
+ // Add new articles (up to 10 total, keeping most recent)
84
+ articles.forEach((article, index) => {
85
+ if (index < 10 && !currentIds.has(index.toString())) {
86
+ newHtml += `
87
+ <div class="article-tile" data-published="${article.published}" data-id="${index}">
88
+ ${article.image !== "svg" ? `<img src="${article.image}" alt="Article Image">` : `
89
+ <svg width="100%" height="150" xmlns="http://www.w3.org/2000/svg">
90
+ <rect width="100%" height="100%" fill="#e0e0e0"/>
91
+ <text x="50%" y="50%" text-anchor="middle" dy=".3em" fill="#666">No Image</text>
92
+ </svg>
93
+ `}
94
+ <div class="title"><a href="${article.link}" target="_blank">${article.title}</a></div>
95
+ <div class="description">${article.description}</div>
96
+ <div class="published">Published: ${article.published}</div>
97
+ </div>
98
+ `;
99
  }
100
  });
101
+
102
+ // Append new articles, maintaining limit of 10
103
+ if (newHtml) {
104
+ tilesDiv.innerHTML += newHtml;
105
+ const allArticles = Array.from(tilesDiv.querySelectorAll('.article-tile'));
106
+ allArticles.sort((a, b) => new Date(b.dataset.published) - new Date(a.dataset.published));
107
+ tilesDiv.innerHTML = allArticles.slice(0, 10).map(a => a.outerHTML).join('');
108
+ }
109
+ }
110
+ }
111
+ document.querySelector('.loading-message').style.display = 'none';
112
  }
113
+ setTimeout(updateArticles, 2000); // Check every 2 seconds
114
  })
115
  .catch(error => {
116
+ console.error('Error updating articles:', error);
117
+ setTimeout(updateArticles, 2000); // Retry on error
118
  });
119
  }
120
 
121
  document.addEventListener('DOMContentLoaded', () => {
122
+ const tiles = document.querySelectorAll('.tiles');
123
+ tiles.forEach(tile => {
124
+ lastUpdate = Math.max(lastUpdate, parseFloat(tile.dataset.lastUpdate) || 0);
125
+ });
126
+ updateArticles();
127
  });
128
  </script>
129
  {% endif %}