From bc637c345724ecc3ff327d8b49f18ed00eba3f8f Mon Sep 17 00:00:00 2001 From: tree Date: Sat, 18 May 2024 17:11:05 +0200 Subject: [PATCH] Show past events as past events --- src/components/EventsPage.astro | 15 +++++++++++++-- src/lib/date.js | 8 ++++++++ src/pages/index.astro | 8 +++----- 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 src/lib/date.js diff --git a/src/components/EventsPage.astro b/src/components/EventsPage.astro index 4126e8f..c1e1f08 100644 --- a/src/components/EventsPage.astro +++ b/src/components/EventsPage.astro @@ -5,6 +5,7 @@ import core from '../core.json'; import EventItem from '../components/EventItem.astro'; import { isFuture } from 'date-fns'; import { types, countryNames } from '../lib/events.js'; +import { isFutureDate } from '../lib/date'; const { type: selectedType } = Astro.props; const { country: selectedCountry } = Astro.props; @@ -43,12 +44,22 @@ function eventsFilter (year, future=true) { } const pastYears = [...new Set(events.map(e => e.date.match(/^(\d{4})/)[1]))]; -const upcoming = events.filter(x => x.date.match(/^2024/)) + +let upcomingEvents = []; +let pastEvents = []; +for (const e of events) { + if (isFutureDate(e.date)) { + upcomingEvents.push(e) + } else { + pastEvents.push(e) + } +} +const upcoming = events.filter(x => isFutureDate(x.date)) const past = {} let pastTotal = 0 for (const year of pastYears.reverse()) { - past[year] = events.filter(eventsFilter(year, false)).reverse() + past[year] = pastEvents.filter(eventsFilter(year, false)).reverse() pastTotal += past[year].length } diff --git a/src/lib/date.js b/src/lib/date.js new file mode 100644 index 0000000..36abe22 --- /dev/null +++ b/src/lib/date.js @@ -0,0 +1,8 @@ +import { isPast } from 'date-fns'; + +export function isFutureDate (dt) { + if (dt.match(/^\d{4}-\d{2}-\d{2}$/) && isPast(new Date(dt))) { + return false + } + return true +} \ No newline at end of file diff --git a/src/pages/index.astro b/src/pages/index.astro index 8a4ddf2..69f3ec7 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -7,8 +7,9 @@ import articles from '../articles.json'; import talks from '../talks.json'; import explorer from '../explorer.json'; import dbRepo from '../db-repo.json'; -import { isPast, format } from 'date-fns'; +import { format } from 'date-fns'; import EventItem from '../components/EventItem.astro'; +import { isFutureDate } from '../lib/date'; const events = core.events; @@ -16,10 +17,7 @@ const upcomingEvents = [] let eventsPast = 0 let eventsUpcoming = 0 for (const ev of events) { - let future = true - if (ev.date.match(/^\d{4}-\d{2}-\d{2}$/) && isPast(new Date(ev.date))) { - future = false - } + let future = isFutureDate(ev.date) if (future) { upcomingEvents.push(ev) eventsUpcoming++