diff --git a/app/host/page.tsx b/app/host/page.tsx index f78c01e..1b63f2b 100644 --- a/app/host/page.tsx +++ b/app/host/page.tsx @@ -10,6 +10,7 @@ import { useRouter } from 'next/navigation'; import React, { useEffect, useRef, useState } from 'react'; import { motion } from 'framer-motion'; import { EventFormData } from '@/components/custom/EventForm'; +import { createEvent } from '@/lib/createEvent'; const Page = () => { const router = useRouter(); @@ -37,9 +38,19 @@ const Page = () => { eventEndTime?: Date | undefined; images?: string[] | undefined; }) { - // Logic for handling the form submission - console.log('Event data submitted:', data); - // You can replace the console log with an API call or any other handling logic + createEvent({ + name: data.name, + description: data.description, + location: data.location, + capacity: data.capacity, + ticketPrice: data.ticketPrice, + startDate: data.eventStartTime, + endDate: data.eventEndTime || new Date(), + images: data.images || [], + toast: ({ title, variant }) => { + alert(title); + }, + }); router.push('/events'); } diff --git a/lib/createEvent.ts b/lib/createEvent.ts index 7e84b9a..31a6f5b 100644 --- a/lib/createEvent.ts +++ b/lib/createEvent.ts @@ -1,3 +1,91 @@ -export const createEvent = async (event: Event) => { - console.log('HELLO'); +import { ethers } from 'ethers'; +import { getContract } from './ethers'; + +interface CreateEventProps { + name: string; + description: string; + location: string; + capacity: number; + ticketPrice: number; + startDate: Date; + endDate: Date; + images: string[]; + 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 createEvent = async ({ + name, + description, + location, + capacity, + ticketPrice, + startDate, + endDate, + images, + toast, +}: CreateEventProps) => { + try { + console.log('Starting createEvent function'); + 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 signer = provider.getSigner(); + const contract = getContract().connect(signer); + + console.log('Preparing transaction data'); + const tx = await contract.createEvent( + name, + description, + capacity, + ethers.utils.parseEther(ticketPrice.toString()), + Math.floor(startDate.getTime() / 1000), + images + ); + + console.log('Transaction sent, waiting for confirmation'); + const receipt = await tx.wait(); + + console.log('Transaction confirmed:', receipt.transactionHash); + toast({ + title: `Event created successfully! Transaction Hash: ${receipt.transactionHash}`, + }); + + return receipt.transactionHash; + } 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; + } };