Difference between revisions of "Template:Hpccalendar"
Jump to navigation
Jump to search
(First try of a course list querying the new REST API of hpc-in-deutschland.de) |
|||
| Line 1: | Line 1: | ||
| − | <div id=' | + | <div id="course-list"></div> |
| + | |||
| + | <script> | ||
| + | async function fetchAndDisplayCourses() { | ||
| + | const url = 'https://api.veranstaltungen.hpc-in-deutschland.de/events'; | ||
| + | |||
| + | try { | ||
| + | const response = await fetch(url); | ||
| + | if (!response.ok) { | ||
| + | throw new Error(`Failed to fetch courses: ${response.status}`); | ||
| + | } | ||
| + | |||
| + | const events = await response.json(); | ||
| + | const today = new Date(); | ||
| + | const twoMonthsLater = new Date(); | ||
| + | twoMonthsLater.setMonth(today.getMonth() + 2); | ||
| + | |||
| + | // Filter upcoming courses within next 2 months | ||
| + | let upcomingCourses = events.filter(event => { | ||
| + | const startDate = new Date(event.startDate || event.date); | ||
| + | return startDate >= today && startDate <= twoMonthsLater; | ||
| + | }); | ||
| + | |||
| + | // Sort by start date ascending | ||
| + | upcomingCourses.sort((a, b) => new Date(a.startDate || a.date) - new Date(b.startDate || b.date)); | ||
| + | |||
| + | let html = '<ul>'; | ||
| + | upcomingCourses.forEach(event => { | ||
| + | const start = new Date(event.startDate || event.date); | ||
| + | const end = event.endDate ? new Date(event.endDate) : null; | ||
| + | |||
| + | const location = event.location || (event.online ? 'Online' : 'No location info'); | ||
| + | |||
| + | html += '<li>'; | ||
| + | |||
| + | // Date before the name | ||
| + | html += `${start.toLocaleDateString()}`; | ||
| + | if (end && end > start) { | ||
| + | html += ` - ${end.toLocaleDateString()}`; | ||
| + | } | ||
| + | |||
| + | html += ': '; | ||
| + | |||
| + | // Name as a link to URL | ||
| + | html += `<a href="${event.url}" target="_blank" rel="noopener noreferrer">${event.title || event.name}</a>`; | ||
| + | |||
| + | html += ` | Location: ${location}`; | ||
| + | html += '</li>'; | ||
| + | }); | ||
| + | |||
| + | html += '</ul>'; | ||
| + | document.getElementById('course-list').innerHTML = html; | ||
| + | |||
| + | } catch (error) { | ||
| + | document.getElementById('course-list').innerText = 'Error loading courses: ' + error.message; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | fetchAndDisplayCourses(); | ||
| + | </script> | ||
Latest revision as of 13:27, 8 September 2025
<script> async function fetchAndDisplayCourses() {
const url = 'https://api.veranstaltungen.hpc-in-deutschland.de/events';
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch courses: ${response.status}`);
}
const events = await response.json(); const today = new Date(); const twoMonthsLater = new Date(); twoMonthsLater.setMonth(today.getMonth() + 2);
// Filter upcoming courses within next 2 months
let upcomingCourses = events.filter(event => {
const startDate = new Date(event.startDate || event.date);
return startDate >= today && startDate <= twoMonthsLater;
});
// Sort by start date ascending upcomingCourses.sort((a, b) => new Date(a.startDate || a.date) - new Date(b.startDate || b.date));
let html = '
- ';
upcomingCourses.forEach(event => {
const start = new Date(event.startDate || event.date);
const end = event.endDate ? new Date(event.endDate) : null;
const location = event.location || (event.online ? 'Online' : 'No location info');
html += '
- '; // Date before the name html += `${start.toLocaleDateString()}`; if (end && end > start) { html += ` - ${end.toLocaleDateString()}`; } html += ': '; // Name as a link to URL html += `<a href="${event.url}" target="_blank" rel="noopener noreferrer">${event.title || event.name}</a>`; html += ` | Location: ${location}`; html += ' '; }); html += '
';
document.getElementById('course-list').innerHTML = html;
} catch (error) {
document.getElementById('course-list').innerText = 'Error loading courses: ' + error.message;
}
}
fetchAndDisplayCourses(); </script>