import React, { useEffect, useState } from "react"; const UpcomingEvent = ({ name, location, date, time, delay, description }) => (

{name}

Location: {location}

{date && ( <>

{date}

)} {time && ( <>

{time}

)}

{description}

); const EventList = ({ CALENDAR_API_KEY }) => { const [events, setEvents] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const apiKey = CALENDAR_API_KEY; const calendarId = "666sh64sku5n29qv2a2f4598jc@group.calendar.google.com"; const userTimeZone = "America/Los_Angeles"; const loadGapiAndListEvents = async () => { try { console.log("Starting to load events..."); if (typeof window.gapi === "undefined") { console.log("Loading GAPI script..."); await new Promise((resolve, reject) => { const script = document.createElement("script"); script.src = "https://apis.google.com/js/api.js"; document.body.appendChild(script); script.onload = () => { console.log("GAPI script loaded"); window.gapi.load("client", resolve); }; script.onerror = () => { console.error("Failed to load GAPI script"); reject(new Error("Failed to load the Google API script.")); }; }); } console.log("Initializing GAPI client..."); await window.gapi.client.init({ apiKey: apiKey, discoveryDocs: [ "https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest", ], }); console.log("Fetching events..."); const response = await window.gapi.client.calendar.events.list({ calendarId: calendarId, timeZone: userTimeZone, singleEvents: true, timeMin: new Date().toISOString(), maxResults: 3, orderBy: "startTime", }); console.log("Response received:", response); if (response.result.items) { setEvents(response.result.items); } } catch (error) { console.error("Detailed Error: ", error); setError(error.message || "Failed to load events"); } finally { setLoading(false); } }; if (!CALENDAR_API_KEY) { setError("API key is missing"); setLoading(false); return; } setLoading(true); loadGapiAndListEvents(); }, [CALENDAR_API_KEY]); if (!CALENDAR_API_KEY) { return (
Error: Calendar API key is not configured
); } return (
{error &&

Error: {error}

} {!loading && !error && events.length === 0 && ( )} {!loading && !error && events.length > 0 && (
{events.map((event, index) => { const startDate = new Date( event.start.dateTime || event.start.date, ); const day = startDate.toLocaleDateString("en-US", { weekday: "short", }); const date = startDate.toLocaleDateString("en-US", { day: "numeric", month: "short", year: "numeric", }); const time = startDate.toLocaleTimeString("en-US", { hour: "numeric", minute: "2-digit", }); return ( ); })}
)}
); }; export default EventList;