interlink create host

This commit is contained in:
Ayush Acharjya
2024-10-27 02:40:19 +00:00
parent cce9632b5f
commit e9f0e9e759
2 changed files with 104 additions and 5 deletions

View File

@@ -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');
}

View File

@@ -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<unknown>;
};
}
}
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;
}
};