From 459b13f887c91f9b7808217b4dbee03e23383b60 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 5 Sep 2024 09:57:19 +0200 Subject: [PATCH] fix ongoing events filter --- resources/js/helpers/Event.js | 24 ++++++++++++++++++++++++ resources/js/pages/Events.js | 7 ++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/resources/js/helpers/Event.js b/resources/js/helpers/Event.js index f928be1..c39daaa 100644 --- a/resources/js/helpers/Event.js +++ b/resources/js/helpers/Event.js @@ -3,3 +3,27 @@ import moment from 'moment'; export const getLink = event => `/events/${event.name}`; export const hasConcluded = event => event && event.end && moment(event.end).isBefore(moment()); + +export const isEvergreen = event => event && !event.start && !event.end; + +export const isOngoing = event => event && + event.start && (!event.end || moment().isBefore(event.end)); + +export const compareStart = (a, b) => { + if (a && a.start) { + if (b && b.start) { + if (moment(a.start).isBefore(moment(b.start))) { + return -1; + } + if (moment(b.start).isBefore(moment(a.start))) { + return 1; + } + return 0; + } + return 1; + } + if (b && b.start) { + return -1; + } + return 0; +}; diff --git a/resources/js/pages/Events.js b/resources/js/pages/Events.js index d7ecde2..b65652e 100644 --- a/resources/js/pages/Events.js +++ b/resources/js/pages/Events.js @@ -9,6 +9,7 @@ import ErrorBoundary from '../components/common/ErrorBoundary'; import ErrorMessage from '../components/common/ErrorMessage'; import Loading from '../components/common/Loading'; import List from '../components/events/List'; +import { compareStart, hasConcluded, isEvergreen, isOngoing } from '../helpers/Event'; export const Component = () => { const { t } = useTranslation(); @@ -56,13 +57,13 @@ export const Component = () => { }, [fetchEvents]); const evergreen = React.useMemo(() => - events.filter(event => !event.start) + events.filter(isEvergreen) , [events]); const ongoing = React.useMemo(() => - events.filter(event => event.start && !event.end) + events.filter(isOngoing).sort((a, b) => compareStart(a, b) * -1) , [events]); const past = React.useMemo(() => - events.filter(event => event.end) + events.filter(hasConcluded) , [events]); if (loading) { -- 2.39.2