diff --git a/app/events/[...eventId]/page.tsx b/app/events/[...eventId]/page.tsx index 81c98b9..2a44b8f 100644 --- a/app/events/[...eventId]/page.tsx +++ b/app/events/[...eventId]/page.tsx @@ -4,42 +4,28 @@ import { useParams } from 'next/navigation'; import Header from '../../../components/custom/header'; import Footer from '../../../components/custom/footer'; import EventDescription from '../../../components/custom/EventDescription'; +import { fetchEventDetails } from '@/lib/fetchEventDetails'; const ListingPage: React.FC = () => { const { eventId } = useParams(); const [eventDetails, setEventDetails] = useState(null); + const [eventNotFound, setEventNotFound] = useState(false); useEffect(() => { - const fetchEventDetails = async (id: number) => { - alert(`Fetching details for event ID: ${id}`); - // Dummy Response - const details = { - EventID: id, - name: 'Example Event Name', - date: '2023-12-01', - location: 'Example Location', - ticketPrice: 100, - description: 'Detailed description of the event.', - capacity: 300, - ticketsSold: 295, - imageUrl: [ - 'https://via.placeholder.com/150', - 'https://via.placeholder.com/150', - ], - host: 'Example Host', - tickets: [1, 2, 3, 4], - }; - return details; - }; - const getEventDetails = async () => { if (eventId) { - const details = await fetchEventDetails(Number(eventId)); + const details = await fetchEventDetails({ + eventID: Number(eventId), + toast: ({ title, variant }: any) => {alert(title);}}); + console.log(details) setEventDetails(details); } }; - getEventDetails(); + getEventDetails().catch((err) => { + setEventNotFound(true); + console.log(eventNotFound); + }); }, [eventId]); return ( @@ -64,11 +50,13 @@ const ListingPage: React.FC = () => {
- {eventDetails ? ( - - ) : ( -

Loading...

- )} + {eventNotFound ?

Event not found

: + (eventDetails ? ( + + ) : ( +

Loading...

+ )) + }
diff --git a/components/custom/EventDescription.tsx b/components/custom/EventDescription.tsx index 974790d..f7e1daf 100644 --- a/components/custom/EventDescription.tsx +++ b/components/custom/EventDescription.tsx @@ -48,21 +48,21 @@ const EventDescription: React.FC = ({ variant="outline" className="text-blue-600 bg-blue-100 px-3 py-1 rounded-full" > - Price: ${eventDetails.ticketPrice} + Price: ${eventDetails.ticketPrice.toFixed(2)}
- +

{eventDetails.description}

-

Location: {eventDetails.location}

-

Date: {eventDetails.date}

-

Host: {eventDetails.host}

+

Location:
{eventDetails.location}


+

Date:
{eventDetails.date}


+

Host:
{eventDetails.host}

{eventDetails.ticketsSold / eventDetails.capacity >= 0.9 && (
Limited Tickets Remaining! diff --git a/lib/fetchEventDetails.ts b/lib/fetchEventDetails.ts new file mode 100644 index 0000000..4fe42d2 --- /dev/null +++ b/lib/fetchEventDetails.ts @@ -0,0 +1,78 @@ +import { ethers } from 'ethers'; +import { getContract } from './ethers'; + +interface GetEventDetailsProps { + eventID: number; + toast: ToastFunction; +} + +type ToastFunction = (options: { + title: string; + variant?: 'default' | 'destructive' | null | undefined; +}) => void; + +declare global { + interface Window { + ethereumProvider?: ethers.providers.ExternalProvider & { + isMetaMask?: boolean; + request?: (method: string, params?: unknown[]) => Promise; + }; + } +} + +export const fetchEventDetails = async ({ + eventID, + toast, +}: GetEventDetailsProps) => { + try { + console.log('Starting events call'); + if (typeof window.ethereum === 'undefined') { + console.error('Ethereum provider not found'); + toast({ + title: 'Please install MetaMask or another Ethereum wallet', + variant: 'destructive', + }); + return; + } + + console.log('Connecting to Ethereum provider'); + const provider = new ethers.providers.Web3Provider(window.ethereum); + const contract = getContract(); + + console.log('Requesting data'); + const eventData = await contract.callStatic.events(eventID); + const eventImages = await contract.callStatic.getEventImages(eventID); + console.log(eventData); + + // toast({ + // title: `Data fetched successfully!`, + // }); + + return { + EventID: eventID, + name: eventData.name, + date: eventData.eventStartDate.toNumber(), + location: eventData.location, + ticketPrice: eventData.ticketPrice.div(ethers.BigNumber.from("1000000000000000000")).toNumber() / 100, + description: eventData.description, + capacity: eventData.capacity.toNumber(), + ticketsSold: eventData.ticketsSold.toNumber(), + imageUrl: eventImages, + host: eventData.eventHost + } + } catch (error) { + console.error('Error in createEvent:', error); + // if (error instanceof Error) { + // toast({ + // title: `Transaction failed: ${error.message}`, + // variant: 'destructive', + // }); + // } else { + // toast({ + // title: 'Transaction failed. Please try again.', + // variant: 'destructive', + // }); + // } + throw error; + } +};