|
130 | 130 | from { opacity: 0; transform: translateY(-10px); } |
131 | 131 | to { opacity: 1; transform: translateY(0); } |
132 | 132 | } |
| 133 | + |
| 134 | + /* new styles */ |
| 135 | + .recommendation-item { |
| 136 | + margin: 10px 0; |
| 137 | + padding: 12px; |
| 138 | + border-radius: 8px; |
| 139 | + background-color: #f5f5f5; |
| 140 | + transition: background-color 0.2s ease; |
| 141 | + } |
| 142 | + |
| 143 | + .recommendation-link { |
| 144 | + text-decoration: none; |
| 145 | + color: #333; |
| 146 | + display: block; |
| 147 | + } |
| 148 | + |
| 149 | + .recommendation-link:hover { |
| 150 | + color: #2c662d; |
| 151 | + } |
| 152 | + |
| 153 | + .recommendation-item:hover { |
| 154 | + background-color: #ebf6eb; |
| 155 | + } |
| 156 | + |
| 157 | + .recommendation-link strong { |
| 158 | + display: block; |
| 159 | + margin-bottom: 4px; |
| 160 | + color: #4CAF50; |
| 161 | + } |
| 162 | + |
| 163 | + .recommendation-link span { |
| 164 | + display: block; |
| 165 | + font-size: 0.9em; |
| 166 | + line-height: 1.4; |
| 167 | + } |
| 168 | + |
133 | 169 | </style> |
134 | 170 | </head> |
135 | 171 | <body> |
@@ -234,32 +270,68 @@ <h3 id="modal-title"></h3> |
234 | 270 | `; |
235 | 271 | } |
236 | 272 |
|
| 273 | + until fetchRecommendationsData) |
| 274 | + |
237 | 275 | async function fetchRecommendationsData() { |
| 276 | + const urls = [ |
| 277 | + "https://www.fao.org/climate-smart-agriculture/en/", |
| 278 | + "https://www.fao.org/sustainable-agriculture/en/", |
| 279 | + "https://www.fao.org/soils-portal/soil-management/en/", |
| 280 | + "https://www.fao.org/water/en/", |
| 281 | + "https://www.fao.org/digital-agriculture/en/" |
| 282 | + ]; |
| 283 | + |
| 284 | + let recommendations = []; |
238 | 285 | try { |
239 | | - const response = await fetch("/api/recommendations"); |
240 | | - const data = await response.json(); |
241 | | - renderRecommendationCards(data); |
| 286 | + const responses = await Promise.all(urls.map(url => |
| 287 | + fetch(url).then(res => res.text()).catch(() => null) |
| 288 | + )); |
| 289 | + |
| 290 | + recommendations = responses.map((response, index) => { |
| 291 | + if (!response) return ''; // Skip failed fetches |
| 292 | + |
| 293 | + const parser = new DOMParser(); |
| 294 | + const doc = parser.parseFromString(response, 'text/html'); |
| 295 | + |
| 296 | + // Try to extract meta description |
| 297 | + const metaDesc = doc.querySelector('meta[name="description"]')?.content; |
| 298 | + |
| 299 | + // Fallback to first meaningful paragraph |
| 300 | + let summary = metaDesc || Array.from(doc.querySelectorAll('p')).find(p => |
| 301 | + p.textContent.length > 50 && !p.textContent.includes('©') |
| 302 | + )?.textContent || 'Best practices information not available'; |
| 303 | + |
| 304 | + // Clean and truncate summary |
| 305 | + summary = summary.replace(/\s+/g, ' ').trim(); |
| 306 | + summary = summary.split('.')[0] + '.'; |
| 307 | + |
| 308 | + return ` |
| 309 | + <div class="recommendation-item"> |
| 310 | + <a href="${urls[index]}" target="_blank" class="recommendation-link"> |
| 311 | + <strong>${doc.title || `Recommendation ${index + 1}`}:</strong> |
| 312 | + <span>${summary}</span> |
| 313 | + </a> |
| 314 | + </div> |
| 315 | + `; |
| 316 | + }).filter(Boolean); // Remove empty entries |
| 317 | + |
| 318 | + if (recommendations.length === 0) { |
| 319 | + recommendations.push('<p>No recommendations available at this time</p>'); |
| 320 | + } |
242 | 321 | } catch (error) { |
243 | | - showError("Failed to load agricultural recommendations"); |
244 | | - document.getElementById('recommendations-card').innerHTML = ` |
245 | | - <h3 class="card-title">🌱 Expert Recommendations</h3> |
246 | | - <div class="loading">❌ Failed to load recommendations.</div> |
247 | | - `; |
| 322 | + console.error('Recommendations error:', error); |
| 323 | + recommendations = ['<div class="loading">❌ Failed to load recommendations. Please try again later.</div>']; |
248 | 324 | } |
| 325 | + |
| 326 | + renderRecommendationCards(recommendations); |
249 | 327 | } |
250 | 328 |
|
251 | | - async function getRecommendationDetails(index) { |
252 | | - try { |
253 | | - const response = await fetch("/api/details", { |
254 | | - method: 'POST', |
255 | | - headers: {'Content-Type': 'application/json'}, |
256 | | - body: JSON.stringify({ index }) |
257 | | - }); |
258 | | - const details = await response.json(); |
259 | | - displayDetailsModal(details); |
260 | | - } catch (error) { |
261 | | - showError("Could not load recommendation details"); |
262 | | - } |
| 329 | + function renderRecommendationCards(recommendations) { |
| 330 | + const recommendationsCard = document.getElementById('recommendations-card'); |
| 331 | + recommendationsCard.innerHTML = ` |
| 332 | + <h3 class="card-title">🌱 Expert Recommendations</h3> |
| 333 | + ${recommendations.join('')} |
| 334 | + `; |
263 | 335 | } |
264 | 336 |
|
265 | 337 | async function loadDashboardData() { |
@@ -287,12 +359,9 @@ <h3 class="card-title">🌱 Expert Recommendations</h3> |
287 | 359 | function renderRecommendationCards(recommendations) { |
288 | 360 | const recommendationsCard = document.getElementById('recommendations-card'); |
289 | 361 | recommendationsCard.innerHTML = `<h3 class="card-title">🌱 Expert Recommendations</h3>`; |
290 | | - recommendations.forEach((rec, index) => { |
| 362 | + recommendations.forEach(rec => { |
291 | 363 | const recElement = document.createElement('div'); |
292 | | - recElement.innerHTML = ` |
293 | | - <p><strong>${rec.title}</strong></p> |
294 | | - <p>${rec.details}</p> |
295 | | - `; |
| 364 | + recElement.innerHTML = rec; |
296 | 365 | recommendationsCard.appendChild(recElement); |
297 | 366 | }); |
298 | 367 | } |
|
0 commit comments