diff --git a/.github/workflows/nextjs.yml b/.github/workflows/nextjs.yml index 4dc152d..a479d90 100644 --- a/.github/workflows/nextjs.yml +++ b/.github/workflows/nextjs.yml @@ -62,6 +62,7 @@ jobs: MY_GITHUB_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }} CALENDAR_API_KEY: ${{ secrets.CALENDAR_API_KEY }} GOOGLE_CALENDAR_ID: ${{ secrets.GOOGLE_CALENDAR_ID }} + NEXT_PUBLIC_BASE_URL: ${{ secrets.NEXT_PUBLIC_BASE_URL }} run: ${{ steps.detect-package-manager.outputs.runner }} next build - name: Upload artifact uses: actions/upload-pages-artifact@v3 diff --git a/src/app/api/events/byDate/route.ts b/src/app/api/events/byDate/route.ts deleted file mode 100644 index eb36ca4..0000000 --- a/src/app/api/events/byDate/route.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { NextResponse } from "next/server"; -import { GET as getEvents } from "../route"; -import type { Event } from "@/lib/types.d.ts"; - -const calendarId = process.env.GOOGLE_CALENDAR_ID; -const endpoint = `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events`; - -export async function GET(request: Request) { - const url = new URL(request.url); - const year = parseInt(url.searchParams.get("year") || "0", 10); - const month = parseInt(url.searchParams.get("month") || "0", 10); - - // Fetch filtered events directly - const filteredEvents = await byDate(month, year); - return NextResponse.json(filteredEvents); -} - -export async function byDate(month: number, year: number): Promise { - const response = await getEvents(); // Fetch the events data directly - const data = await response.json(); // Parse the response to JSON - const earliest = new Date(year, month, 1); - const latest = new Date(year, month + 1, 0); - - // Filter and transform events in a single pass - const filteredEvents = data - .map((item: any) => { - const startDate = item.start ? new Date(item.start) : undefined; // Ensure start is defined - return { - ...item, - start: startDate, - }; - }) - .filter((event: Event) => { - if (!event.start) return false; // Exclude events with no start date - const eventDate = new Date(event.start); - return eventDate >= earliest && eventDate <= latest; - }); - - return filteredEvents; -} diff --git a/src/app/api/events/route.ts b/src/app/api/events/route.ts index ed3b8fc..f457da4 100644 --- a/src/app/api/events/route.ts +++ b/src/app/api/events/route.ts @@ -1,25 +1,30 @@ import type { Event } from "@/lib/types.d.ts"; import { NextResponse } from "next/server"; -export const dynamic = "force-dynamic"; +export const dynamic = "force-dynamic"; // Ensure fresh data on every request + const calendarId = process.env.GOOGLE_CALENDAR_ID; const endpoint = `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events`; -export async function GET() { - // Fetch and parse data - const response = await fetch( - `${endpoint}?key=${process.env.CALENDAR_API_KEY}`, - { cache: "no-store" } - ); - const data = await response.json(); +export async function GET(request: Request) { + const url = new URL(request.url); + const year = parseInt(url.searchParams.get("year") || "0", 10); + const month = parseInt(url.searchParams.get("month") || "0", 10); + // Fetch all events + const response = await fetch(`${endpoint}?key=${process.env.CALENDAR_API_KEY}`, { + cache: "no-store", + }); + + const data = await response.json(); const items = data.items as unknown[] | undefined; + // If no items found, return 404 if (!items || !Array.isArray(items) || items.length === 0) { return new Response("No events found", { status: 404 }); } - // Use a single pass to parse and filter + // Filter and transform events in a single pass const now = new Date(); now.setHours(0, 0, 0, 0); @@ -44,8 +49,24 @@ export async function GET() { location: typeof item.location === "string" ? item.location : undefined, start: new Date(item.start.dateTime), })) - .filter((event: Event) => event.start !== undefined && event.start > now) + .filter((event: Event) => { + // Ensure start is defined and is a valid date + return event.start !== undefined && event.start > now; + }) .sort((a, b) => a.start.getTime() - b.start.getTime()); - return new NextResponse(JSON.stringify(upcoming)); + // If year and month are provided, filter further by date + if (year && month) { + const earliest = new Date(year, month, 1); + const latest = new Date(year, month + 1, 0); + + const filteredEvents = upcoming.filter((event: Event) => { + // Check if start is defined and falls within the date range + return event.start !== undefined && event.start >= earliest && event.start <= latest; + }); + + return NextResponse.json(filteredEvents); + } + + return NextResponse.json(upcoming); } diff --git a/src/components/events/Calendar.tsx b/src/components/events/Calendar.tsx index 8e2ae15..b6bf277 100644 --- a/src/components/events/Calendar.tsx +++ b/src/components/events/Calendar.tsx @@ -1,6 +1,5 @@ import Link from "next/link"; import CalendarBody from "./CalendarBody"; -import { byDate } from "@/app/api/events/byDate/route"; type Props = { month: number; @@ -34,8 +33,21 @@ function changeMonth( return { month: month - 1, year }; } +async function fetchEvents(month: number, year: number) { + const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || ''; // Define your base URL here + const url = new URL(`/api/events?month=${month}&year=${year}`, baseUrl); + const res = await fetch(url.toString(), { + cache: "no-store", // Ensure fresh data + }); + if (!res.ok) { + throw new Error("Failed to fetch events"); + } + + return res.json(); +} + async function Calendar({ month, year }: Props) { - const events = await byDate(month, year); + const events = await fetchEvents(month, year); const nextParams = changeMonth("inc", { month, year }); const prevParams = changeMonth("dec", { month, year });