Skip to content

Commit

Permalink
combine the api routes for events
Browse files Browse the repository at this point in the history
  • Loading branch information
atiqurx committed Oct 2, 2024
1 parent ddc1024 commit 588a3f6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 53 deletions.
1 change: 1 addition & 0 deletions .github/workflows/nextjs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 0 additions & 40 deletions src/app/api/events/byDate/route.ts

This file was deleted.

43 changes: 32 additions & 11 deletions src/app/api/events/route.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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<Event[]>(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);
}
16 changes: 14 additions & 2 deletions src/components/events/Calendar.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 });

Expand Down

0 comments on commit 588a3f6

Please sign in to comment.