Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch event data depending on calendar view #508

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions src/routes/(app)/events/calendar/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,58 @@
import eventPageLoad from "../EventPageLoad";
import { BASIC_EVENT_FILTER } from "$lib/events/events";
import dayjs from "dayjs";

export const load = eventPageLoad();
export type ViewParam = "day" | "week" | "month";
export type RangeDateParam = string;
const include = {
author: true,
comments: {
include: {
member: true,
},
},
going: true,
interested: true,
tags: true,
};

export const load = async ({ locals, url }) => {
const { prisma } = locals;
const view: "day" | "week" | "month" = "month";
const startDateParam = url.searchParams.get("startDate");
const endDateParam = url.searchParams.get("endDate");
const firstDate = startDateParam
? dayjs(startDateParam)
: dayjs().startOf(view).subtract(1, "week");
const lastDate = endDateParam
? dayjs(endDateParam)
: dayjs().endOf(view).add(1, "week");

const events = await prisma.event.findMany({
where: {
AND: [
BASIC_EVENT_FILTER(),
{
// capture all events which overlap the range firstDate-lastDate
OR: [
{
startDatetime: {
gte: firstDate.toDate(),
lte: lastDate.toDate(),
},
},
{
endDatetime: {
gte: firstDate.toDate(),
lte: lastDate.toDate(),
},
},
],
},
],
},
include,
});
return {
events,
};
};
166 changes: 113 additions & 53 deletions src/routes/(app)/events/calendar/Calendar.svelte
Original file line number Diff line number Diff line change
@@ -1,68 +1,128 @@
<script lang="ts">
import { onMount } from "svelte";
import { Calendar } from "@fullcalendar/core";
import daygridPlugin from "@fullcalendar/daygrid";
import type { Event } from "@prisma/client";
import { eventLink, goto } from "$lib/utils/redirect";
import { page } from "$app/stores";
import "$lib/FullCalendar.css";
import * as m from "$paraglide/messages";
import { languageTag } from "$paraglide/runtime";
import { toast } from "$lib/stores/toast";
import { page } from "$app/stores";
import { i18n } from "$lib/utils/i18n";
import { eventLink, goto } from "$lib/utils/redirect";
import * as m from "$paraglide/messages";
import { languageTag } from "$paraglide/runtime";
import { Calendar } from "@fullcalendar/core";
import daygridPlugin from "@fullcalendar/daygrid";
import type { Event } from "@prisma/client";
import dayjs from "dayjs";
import { onDestroy, onMount } from "svelte";
import type { RangeDateParam, ViewParam } from "./+page.server";

export let events: Event[] = [];
$: params = $page.url.searchParams;
$: view = formatViewName(params.get("view"));
$: middleDate = (() => {
const startDate = params.get("startDate");
const endDate = params.get("endDate");
if (startDate && endDate) {
return dayjs(startDate)
.add(dayjs(endDate).diff(startDate, "days") / 2, "day")
.toDate();
}
return null;
})();
const formatViewName = (view: string | null) => {
switch (view) {
case "day":
return "dayGridDay";
case "week":
return "dayGridWeek";
case "month":
return "dayGridMonth";
default:
return null;
}
};
const reverseViewName = (viewType: string): ViewParam | null => {
switch (viewType) {
case "dayGridDay":
return "day";
case "dayGridWeek":
return "week";
case "dayGridMonth":
return "month";
default:
return null;
}
};

let calendarEl: HTMLElement;
let calendar: Calendar;

let mounted = false;
onMount(() => {
calendar = new Calendar(calendarEl, {
initialView: "dayGridMonth",
plugins: [daygridPlugin],
events: events.map((event) => ({
title: event.title,
start: event.startDatetime,
end: event.endDatetime,
url: eventLink(event),
})),
locale: languageTag(),
eventColor: "#f280a1",
firstDay: 1,
headerToolbar: {
left: "prev,next,addEvent,subscribe",
center: "title",
right: "dayGridDay,dayGridWeek,dayGridMonth",
},
customButtons: {
addEvent: {
text: m.events_createEvent(),
click: () => {
void goto("/events/create");
mounted = true;
});

$: calendar = mounted
? (() => {
const cal = new Calendar(calendarEl, {
initialView: view ? view : "dayGridMonth",
initialDate: middleDate ?? undefined,
plugins: [daygridPlugin],
events: events.map((event) => ({
title: event.title,
start: event.startDatetime,
end: event.endDatetime,
url: eventLink(event),
})),
locale: languageTag(),
eventColor: "#f280a1",
firstDay: 1,
headerToolbar: {
left: "prev,next,addEvent,subscribe",
center: "title",
right: "dayGridDay,dayGridWeek,dayGridMonth",
},
customButtons: {
addEvent: {
text: m.events_createEvent(),
click: () => {
void goto("/events/create");
},
},
subscribe: {
text: m.events_calendar_subscribe(),
click: () => {
navigator.clipboard.writeText(
$page.url.origin +
i18n.resolveRoute("/events/subscribe", languageTag()),
);
toast(m.events_calendar_subscribe_copyToClipboard(), "success");
},
hint: m.events_calendar_subscribe_details(),
},
},
buttonText: {
today: m.events_calendar_today(),
month: m.events_calendar_month(),
week: m.events_calendar_week(),
day: m.events_calendar_day(),
},
},
subscribe: {
text: m.events_calendar_subscribe(),
click: () => {
navigator.clipboard.writeText(
$page.url.origin +
i18n.resolveRoute("/events/subscribe", languageTag()),
);
toast(m.events_calendar_subscribe_copyToClipboard(), "success");
datesSet: (arg) => {
const startDate: RangeDateParam = arg.startStr;
const endDate: RangeDateParam = arg.endStr;
const view = reverseViewName(arg.view.type);
if (!view) throw new Error("Unsupported view type");
const urlParams = new URLSearchParams($page.url.searchParams);
urlParams.set("startDate", startDate);
urlParams.set("endDate", endDate);
urlParams.set("view", view);
goto(`${i18n.route($page.url.pathname)}?${urlParams.toString()}`, {
replaceState: true,
});
},
hint: m.events_calendar_subscribe_details(),
},
},
buttonText: {
today: m.events_calendar_today(),
month: m.events_calendar_month(),
week: m.events_calendar_week(),
day: m.events_calendar_day(),
},
});
calendar.render();
});
cal.render();
return cal;
})()
: undefined;

return () => calendar?.destroy();
onDestroy(() => {
if (calendar) calendar.destroy();
});
</script>

Expand Down
Loading