From 3d3eccdfc09fc4f721f4333e2ed90fe19a1af782 Mon Sep 17 00:00:00 2001 From: brrusselburg <25828824+brrusselburg@users.noreply.github.com> Date: Mon, 28 Oct 2024 13:38:58 -0500 Subject: [PATCH] Address feedback, refactor --- .../components/upcoming-events-list.gjs | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/assets/javascripts/discourse/components/upcoming-events-list.gjs b/assets/javascripts/discourse/components/upcoming-events-list.gjs index cb924fb22..cfb1cb966 100644 --- a/assets/javascripts/discourse/components/upcoming-events-list.gjs +++ b/assets/javascripts/discourse/components/upcoming-events-list.gjs @@ -17,6 +17,19 @@ export const DEFAULT_TIME_FORMAT = "LT"; const DEFAULT_UPCOMING_DAYS = 180; const DEFAULT_COUNT = 8; +function addToResult(date, item, result) { + const year = date.year(); + const month = date.month() + 1; + const day = date.date(); + const monthKey = `${year}-${month}`; + + result[monthKey] = result[monthKey] ?? {}; + console.log("HERE?????"); + result[monthKey][day] = result[monthKey][day] ?? []; + console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + result[monthKey][day].push(item); +} + export default class UpcomingEventsList extends Component { @service appEvents; @service siteSettings; @@ -110,38 +123,30 @@ export default class UpcomingEventsList extends Component { return data.reduce((result, item) => { const startDate = moment(item.starts_at); const endDate = item.ends_at ? moment(item.ends_at) : null; + const today = moment(); - if (endDate && !startDate.isSame(endDate)) { - while (startDate.isSameOrBefore(endDate, "day")) { - const year = startDate.year(); - const month = startDate.month() + 1; - const day = startDate.date(); - const monthKey = `${year}-${month}`; - const today = moment(); + // NO END DATE --OR-- START DATE DAY === END DATE DAY + if (!endDate) { + console.log("SINGLE DAY"); - // For current events, only push upcoming days - if (startDate.isAfter(today)) { - result[monthKey] = result[monthKey] || {}; - result[monthKey][day] = result[monthKey][day] || []; + addToResult(startDate, item, result); + return result; + } - result[monthKey][day].push(item); - } + // START DATE === END DATE --OR-- START DATE << END DATE + while (startDate.isSameOrBefore(endDate, "day")) { + console.log("EXTENDED DAY"); - // Move to the next day - startDate.add(1, "day"); + // For current events, only push upcoming days + if (startDate.isAfter(today)) { + // the problem appears to be here! isAfter() + console.log("RIGHT HERE BEFORE THE FUNC"); + addToResult(startDate, item, result); } - } else { - const date = new Date(item.starts_at); - const year = date.getFullYear(); - const month = date.getMonth() + 1; - const day = date.getDate(); - - const monthKey = `${year}-${month}`; - - result[monthKey] = result[monthKey] ?? {}; - result[monthKey][day] = result[monthKey][day] ?? []; - result[monthKey][day].push(item); + console.log("MOVE TO NEXT DAY"); + // Move to the next day + startDate.add(1, "day"); } return result;