organize events by day in UpcomingEvents
This commit is contained in:
@ -8,7 +8,7 @@ import {
|
|||||||
EventCategory,
|
EventCategory,
|
||||||
SingularEvent,
|
SingularEvent,
|
||||||
getSingularEvents,
|
getSingularEvents,
|
||||||
organizeEventsByDate,
|
organizeEventsInCalendar,
|
||||||
EventOrganizer,
|
EventOrganizer,
|
||||||
} from "@/lib/event";
|
} from "@/lib/event";
|
||||||
import { isTodayOrFuture } from "@/lib/date";
|
import { isTodayOrFuture } from "@/lib/date";
|
||||||
@ -141,7 +141,7 @@ const EventCalendar = ({ events }: { events: EventFragment[] }) => {
|
|||||||
(x) => x.occurrence?.start && isTodayOrFuture(x.occurrence.start)
|
(x) => x.occurrence?.start && isTodayOrFuture(x.occurrence.start)
|
||||||
);
|
);
|
||||||
|
|
||||||
const eventsByDate = organizeEventsByDate(futureSingularEvents);
|
const eventsByDate = organizeEventsInCalendar(futureSingularEvents);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.eventCalendar}>
|
<div className={styles.eventCalendar}>
|
||||||
|
@ -18,7 +18,7 @@ export const EventItem = ({
|
|||||||
size,
|
size,
|
||||||
}: {
|
}: {
|
||||||
event: SingularEvent | EventFragment;
|
event: SingularEvent | EventFragment;
|
||||||
mode: "list" | "calendar" | "singular";
|
mode: "list" | "calendar" | "singular-time-only";
|
||||||
size?: "small" | "medium" | "large";
|
size?: "small" | "medium" | "large";
|
||||||
}) => {
|
}) => {
|
||||||
const futureOccurrences = getFutureOccurrences(event);
|
const futureOccurrences = getFutureOccurrences(event);
|
||||||
@ -58,14 +58,7 @@ export const EventItem = ({
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{mode === "singular" &&
|
{(mode === "calendar" || mode === "singular-time-only") &&
|
||||||
"occurrence" in event &&
|
|
||||||
event.occurrence?.start && (
|
|
||||||
<p className={styles.dates}>
|
|
||||||
{formatExtendedDateTime(event.occurrence.start)}
|
|
||||||
</p>
|
|
||||||
)}
|
|
||||||
{mode === "calendar" &&
|
|
||||||
"occurrence" in event &&
|
"occurrence" in event &&
|
||||||
event.occurrence?.start && (
|
event.occurrence?.start && (
|
||||||
<p className={styles.dates}>
|
<p className={styles.dates}>
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
import { EventFragment } from "@/gql/graphql";
|
import { EventFragment } from "@/gql/graphql";
|
||||||
import { isTodayOrFuture } from "@/lib/date";
|
import { isTodayOrFuture, formatDate } from "@/lib/date";
|
||||||
import { getSingularEvents, sortSingularEvents } from "@/lib/event";
|
import { parse } from "date-fns";
|
||||||
|
import {
|
||||||
|
getSingularEvents,
|
||||||
|
organizeEventsByDate,
|
||||||
|
sortSingularEvents,
|
||||||
|
} from "@/lib/event";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { EventItem } from "./EventItem";
|
import { EventItem } from "./EventItem";
|
||||||
import styles from "./upcomingEvents.module.scss";
|
import styles from "./upcomingEvents.module.scss";
|
||||||
@ -11,6 +16,8 @@ export const UpcomingEvents = ({ events }: { events: EventFragment[] }) => {
|
|||||||
isTodayOrFuture(event.occurrence.start)
|
isTodayOrFuture(event.occurrence.start)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
const sliced = upcomingSingularEvents.slice(0, 10);
|
||||||
|
const eventsByDate = organizeEventsByDate(sliced);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className={styles.upcomingWrapper}>
|
<section className={styles.upcomingWrapper}>
|
||||||
@ -20,16 +27,31 @@ export const UpcomingEvents = ({ events }: { events: EventFragment[] }) => {
|
|||||||
<span className="circle"></span>
|
<span className="circle"></span>
|
||||||
Denne uka på Chateau Neuf
|
Denne uka på Chateau Neuf
|
||||||
</h2>
|
</h2>
|
||||||
<ul className={styles.eventList}>
|
<div className={styles.eventList}>
|
||||||
{upcomingSingularEvents.slice(0, 10).map((event) => (
|
{Object.keys(eventsByDate).map((day) => {
|
||||||
|
const eventsThisDay = eventsByDate[day];
|
||||||
|
return (
|
||||||
|
<div key={day}>
|
||||||
|
<h3>
|
||||||
|
{formatDate(
|
||||||
|
parse(day, "yyyy-MM-dd", new Date()),
|
||||||
|
"eeee dd.MM."
|
||||||
|
)}
|
||||||
|
</h3>
|
||||||
|
<ul>
|
||||||
|
{eventsThisDay.map((event) => (
|
||||||
<EventItem
|
<EventItem
|
||||||
key={`${event.id}-${event.occurrence.id}`}
|
key={`${event.id}-${event.occurrence.id}`}
|
||||||
event={event}
|
event={event}
|
||||||
mode="singular"
|
mode="singular-time-only"
|
||||||
size="medium"
|
size="medium"
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
<div className={styles.calendarLink}>
|
<div className={styles.calendarLink}>
|
||||||
<Link href="/arrangementer?mode=calendar">Vis kalender →</Link>
|
<Link href="/arrangementer?mode=calendar">Vis kalender →</Link>
|
||||||
</div>
|
</div>
|
||||||
|
@ -143,7 +143,7 @@ export function sortSingularEvents(events: SingularEvent[]) {
|
|||||||
compareDates(a.occurrence.start, b.occurrence.start)
|
compareDates(a.occurrence.start, b.occurrence.start)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
interface EventsByDate {
|
interface EventCalendar {
|
||||||
[yearMonth: string]: {
|
[yearMonth: string]: {
|
||||||
[week: string]: {
|
[week: string]: {
|
||||||
[day: string]: SingularEvent[];
|
[day: string]: SingularEvent[];
|
||||||
@ -151,10 +151,12 @@ interface EventsByDate {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function organizeEventsByDate(events: SingularEvent[]): EventsByDate {
|
export function organizeEventsInCalendar(
|
||||||
|
events: SingularEvent[]
|
||||||
|
): EventCalendar {
|
||||||
const sortedEvents = sortSingularEvents(events);
|
const sortedEvents = sortSingularEvents(events);
|
||||||
|
|
||||||
const eventsByDate: EventsByDate = {};
|
const calendar: EventCalendar = {};
|
||||||
|
|
||||||
const minDate = new Date(sortedEvents[0]?.occurrence.start);
|
const minDate = new Date(sortedEvents[0]?.occurrence.start);
|
||||||
const maxDate = new Date(
|
const maxDate = new Date(
|
||||||
@ -170,16 +172,16 @@ export function organizeEventsByDate(events: SingularEvent[]): EventsByDate {
|
|||||||
end: endOfWeek(currentDate, { weekStartsOn: 1 }),
|
end: endOfWeek(currentDate, { weekStartsOn: 1 }),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!eventsByDate[yearMonth]) {
|
if (!calendar[yearMonth]) {
|
||||||
eventsByDate[yearMonth] = {};
|
calendar[yearMonth] = {};
|
||||||
}
|
}
|
||||||
if (!eventsByDate[yearMonth][week]) {
|
if (!calendar[yearMonth][week]) {
|
||||||
eventsByDate[yearMonth][week] = {};
|
calendar[yearMonth][week] = {};
|
||||||
}
|
}
|
||||||
daysOfWeek.forEach((day) => {
|
daysOfWeek.forEach((day) => {
|
||||||
const formattedDay = formatDate(day, "yyyy-MM-dd");
|
const formattedDay = formatDate(day, "yyyy-MM-dd");
|
||||||
if (!eventsByDate[yearMonth][week][formattedDay]) {
|
if (!calendar[yearMonth][week][formattedDay]) {
|
||||||
eventsByDate[yearMonth][week][formattedDay] = [];
|
calendar[yearMonth][week][formattedDay] = [];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -191,7 +193,27 @@ export function organizeEventsByDate(events: SingularEvent[]): EventsByDate {
|
|||||||
const yearMonth = formatDate(start, "yyyy-MM");
|
const yearMonth = formatDate(start, "yyyy-MM");
|
||||||
const week = formatDate(start, "w");
|
const week = formatDate(start, "w");
|
||||||
const day = formatDate(start, "yyyy-MM-dd");
|
const day = formatDate(start, "yyyy-MM-dd");
|
||||||
eventsByDate[yearMonth][week][day].push(event);
|
calendar[yearMonth][week][day].push(event);
|
||||||
|
});
|
||||||
|
|
||||||
|
return calendar;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface EventsByDate {
|
||||||
|
[day: string]: SingularEvent[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function organizeEventsByDate(events: SingularEvent[]): EventsByDate {
|
||||||
|
const sortedEvents = sortSingularEvents(events);
|
||||||
|
const eventsByDate: EventsByDate = {};
|
||||||
|
|
||||||
|
sortedEvents.forEach((event) => {
|
||||||
|
const start = toLocalTime(event.occurrence.start);
|
||||||
|
const day = formatDate(start, "yyyy-MM-dd");
|
||||||
|
if (!eventsByDate[day]) {
|
||||||
|
eventsByDate[day] = [];
|
||||||
|
}
|
||||||
|
eventsByDate[day].push(event);
|
||||||
});
|
});
|
||||||
|
|
||||||
return eventsByDate;
|
return eventsByDate;
|
||||||
|
Reference in New Issue
Block a user