organize events by months, weeks and days and print a calendar view

This commit is contained in:
2024-05-12 16:06:49 +02:00
parent e12a9a82fa
commit 7dfb505af0
4 changed files with 139 additions and 22 deletions

View File

@ -1,7 +1,23 @@
import {
compareAsc,
getYear,
getMonth,
getWeek,
getDate,
endOfWeek,
startOfWeek,
eachDayOfInterval,
addWeeks,
} from "date-fns";
import { toLocalTime, formatDate } from "./date";
import { graphql } from "@/gql";
import { EventFragment, EventOccurrence } from "@/gql/graphql";
export type { EventFragment } from "@/gql/graphql"
export type { EventFragment } from "@/gql/graphql";
export type SingularEvent = EventFragment & {
occurrence: EventOccurrence;
};
const EventFragmentDefinition = graphql(`
fragment Event on EventPage {
@ -55,10 +71,6 @@ export const allEventsQuery = graphql(`
}
`);
export type SingularEvent = EventFragment & {
occurrence: EventOccurrence;
};
export function getSingularEvents(events: EventFragment[]) {
return events
.map((event) => {
@ -77,3 +89,59 @@ export function getSingularEvents(events: EventFragment[]) {
function isDefined<T>(val: T | undefined | null): val is T {
return val !== undefined && val !== null;
}
interface EventsByDate {
[yearMonth: string]: {
[week: number]: {
[day: number]: SingularEvent[];
};
};
}
export function organizeEventsByDate(events: SingularEvent[]): EventsByDate {
const sortedEvents = events.sort((a, b) =>
compareAsc(new Date(a.occurrence.start), new Date(b.occurrence.start))
);
const eventsByDate: EventsByDate = {};
const minDate = new Date(sortedEvents[0]?.occurrence.start);
const maxDate = new Date(
sortedEvents[sortedEvents.length - 1]?.occurrence.start
);
let currentDate = startOfWeek(minDate, { weekStartsOn: 1 });
while (currentDate <= maxDate) {
const yearMonth = formatDate(currentDate, "yyyy-MM");
const week = formatDate(currentDate, "w");
const daysOfWeek = eachDayOfInterval({
start: currentDate,
end: endOfWeek(currentDate, { weekStartsOn: 1 }),
});
if (!eventsByDate[yearMonth]) {
eventsByDate[yearMonth] = {};
}
if (!eventsByDate[yearMonth][week]) {
eventsByDate[yearMonth][week] = {};
}
daysOfWeek.forEach((day) => {
const formattedDay = formatDate(day, "yyyy-MM-dd");
if (!eventsByDate[yearMonth][week][formattedDay]) {
eventsByDate[yearMonth][week][formattedDay] = [];
}
});
currentDate = addWeeks(currentDate, 1);
}
sortedEvents.forEach((event) => {
const start = toLocalTime(event.occurrence.start);
const yearMonth = formatDate(start, "yyyy-MM");
const week = formatDate(start, "w");
const day = formatDate(start, "yyyy-MM-dd");
eventsByDate[yearMonth][week][day].push(event);
});
return eventsByDate;
}