Skip to content

Commit

Permalink
Merge pull request #107 from acm-ucr/AldrieBarcena/eventsComponentFun…
Browse files Browse the repository at this point in the history
…ctionality

Events Component Functionality
  • Loading branch information
SafeDuck authored Nov 1, 2024
2 parents d534fd2 + 29f42c0 commit 0f5c57d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 36 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"prepare": "husky install"
},
"dependencies": {
"@tanstack/react-query": "^5.53.3",
"@tanstack/react-query": "^5.59.16",
"lucide-react": "^0.417.0",
"next": "^14.2.10",
"react": "^18.3.1",
Expand Down
84 changes: 57 additions & 27 deletions src/components/events/Events.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";
import { useQuery } from "@tanstack/react-query";
import Event from "@/components/events/Event";

type EventProps = {
Expand All @@ -8,38 +10,66 @@ type EventProps = {
side: "left" | "right";
};

const eventData: EventProps[] = [
{
name: "Event Name 1",
description:
"small description small description, small description, small description",
location: "Location Here",
time: "Time",
side: "left",
},
{
name: "Event Name 2",
description:
"small description, small description, small description, small description",
location: "Location 2 Here",
time: "Time",
side: "right",
},
{
name: "Event Name 3",
description:
"small description, small description, small description, small description",
location: "Location 3 Here",
time: "Time",
side: "left",
},
];
type ApiEvent = {
summary?: string;
description?: string;
location?: string;
start: {
dateTime?: string;
date?: string;
};
end: {
dateTime?: string;
date?: string;
};
};

const fetchEvents = async (): Promise<EventProps[]> => {
const response = await fetch(
`https://www.googleapis.com/calendar/v3/calendars/${
process.env.NEXT_PUBLIC_GOOGLE_CALENDAR_EMAIL
}/events?key=${process.env.NEXT_PUBLIC_GOOGLE_CALENDAR_API_KEY}
&singleEvents=true&orderBy=startTime&timeMin=${new Date().toISOString()}`,
);

if (!response.ok) {
throw new Error("Error fetching events");
}

const data = await response.json();

return (data.items || [])
.map((item: ApiEvent, index: number) => ({
name: item.summary || "Unnamed Event",
description: item.description || "No description available.",
location: item.location || "No location specified.",
time: item.start.dateTime
? new Date(item.start.dateTime).toLocaleString()
: item.start.date
? new Date(item.start.date).toLocaleString()
: "No time available",
side: index % 2 === 0 ? "left" : "right",
}))
.slice(0, 3);
};

const Events = () => {
const {
data: events = [],
error,
isLoading,
} = useQuery<EventProps[], Error>({
queryKey: ["events"],
queryFn: fetchEvents,
});

if (isLoading) return <p>Loading events...</p>;
if (error) return <p>Error fetching events: {error.message}</p>;

return (
<div className="flex w-full flex-col font-anek-telegu text-3xl">
<div className="mt-8">
{eventData.map((event, index) => (
{events.map((event, index) => (
<Event key={index} {...event} />
))}
</div>
Expand Down

0 comments on commit 0f5c57d

Please sign in to comment.