// /public/js/calendar/upcoming.js
let upcomingTimerInterval = null;
function renderUpcomingEvents() {
const upcomingList = document.getElementById('upcomingEventsList');
if (!upcomingList) return;
const now = new Date();
const today = formatDate(now);
const oneYearAhead = new Date(now);
oneYearAhead.setFullYear(oneYearAhead.getFullYear() + 1);
const futureDate = formatDate(oneYearAhead);
fetch(`/calendar/events?start=${today}&end=${futureDate}`)
.then(response => response.json())
.then(events => {
const upcomingEvents = events
.map(event => ({
...event,
startDate: new Date(event.start_date.replace(' ', 'T'))
}))
.filter(event => event.startDate >= now)
.sort((a, b) => a.startDate - b.startDate)
.slice(0, 10);
if (upcomingEvents.length === 0) {
upcomingList.innerHTML = '
No upcoming events
';
return;
}
const upcomingItems = upcomingEvents.map(event => {
const countdown = getCountdown(event.startDate);
const dateStr = formatEventDateTime(event);
let attendeePills = '';
if (event.attendee_names) {
const attendees = event.attendee_names.split(',').map(name => name.trim());
attendeePills = attendees.map((name, index) => {
const colorClass = `attendee-color-${(index % 8) + 1}`;
return `${escapeHtml(name)}`;
}).join('');
}
return `
${escapeHtml(event.title)}
${dateStr}
${attendeePills ? `
${attendeePills}
` : ''}
${countdown}
`;
}).join('');
upcomingList.innerHTML = upcomingItems;
})
.catch(error => {
console.error('Error loading upcoming events:', error);
upcomingList.innerHTML = 'Error loading events
';
});
}
function startUpcomingCountdownTimer() {
if (upcomingTimerInterval) {
clearInterval(upcomingTimerInterval);
}
renderUpcomingEvents();
upcomingTimerInterval = setInterval(renderUpcomingEvents, 60000);
}
function getCountdown(targetDate) {
const now = new Date();
const diff = targetDate - now;
if (diff < 0) return 'Started';
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
if (days > 0) {
return `In ${days} day${days !== 1 ? 's' : ''}, ${hours} hour${hours !== 1 ? 's' : ''}`;
} else if (hours > 0) {
return `In ${hours} hour${hours !== 1 ? 's' : ''}, ${minutes} minute${minutes !== 1 ? 's' : ''}`;
} else {
return `In ${minutes} minute${minutes !== 1 ? 's' : ''}`;
}
}