(null);
useEffect(() => {
setIsClient(true);
+ fetchEvents().then(eventsData => {
+ eventsData = eventsData?.sort((a, b) => (a.ticketsSold - b.ticketsSold));
+ setEvents(eventsData || []);
+ })
}, []);
+ useEffect(() => {
+ console.log("events updated");
+ console.log(events);
+ }, [events])
+
function searchForEvents() {
if (inputRef.current?.value === '') {
alert('Please enter a search term.');
@@ -54,6 +65,10 @@ export default function Home() {
'gathering',
];
+ const handleEventClick = (eventId: number) => {
+ router.push(`/events/${eventId}`);
+ };
+
return (
<>
@@ -102,46 +117,21 @@ export default function Home() {
diff --git a/components/custom/EventForm.tsx b/components/custom/EventForm.tsx
index f6dfe64..4b826c0 100644
--- a/components/custom/EventForm.tsx
+++ b/components/custom/EventForm.tsx
@@ -19,8 +19,7 @@ const eventSchema = z
.refine(Number.isInteger, { message: 'Capacity must be an integer' }),
ticketPrice: z
.number({ invalid_type_error: 'Ticket price must be a number' })
- .min(0, { message: 'Ticket price must be at least 0' })
- .refine(Number.isInteger, { message: 'Ticket price must be in cents' }),
+ .min(0, { message: 'Ticket price must be at least 0' }),
location: z.string().min(1, { message: 'Location is required' }),
eventStartTime: z.preprocess(
(val) =>
@@ -128,9 +127,8 @@ const EventForm = ({ onSubmit }: EventFormProps) => {
{/* Ticket Price Field */}
-
+
diff --git a/lib/createEvent.ts b/lib/createEvent.ts
index 7bfca92..d5b64d2 100644
--- a/lib/createEvent.ts
+++ b/lib/createEvent.ts
@@ -60,7 +60,7 @@ export const createEvent = async ({
description,
location,
capacity,
- ethers.utils.parseEther(ticketPrice.toString()),
+ Math.floor(ticketPrice * 100),
Math.floor(startDate.getTime() / 1000),
Math.floor(endDate.getTime() / 1000),
images
diff --git a/lib/fetchEventDetails.ts b/lib/fetchEventDetails.ts
index 4fe42d2..4c08c3c 100644
--- a/lib/fetchEventDetails.ts
+++ b/lib/fetchEventDetails.ts
@@ -53,7 +53,7 @@ export const fetchEventDetails = async ({
name: eventData.name,
date: eventData.eventStartDate.toNumber(),
location: eventData.location,
- ticketPrice: eventData.ticketPrice.div(ethers.BigNumber.from("1000000000000000000")).toNumber() / 100,
+ ticketPrice: eventData.ticketPrice.toNumber() / 100,
description: eventData.description,
capacity: eventData.capacity.toNumber(),
ticketsSold: eventData.ticketsSold.toNumber(),
diff --git a/lib/fetchEvents.ts b/lib/fetchEvents.ts
new file mode 100644
index 0000000..bc9e03f
--- /dev/null
+++ b/lib/fetchEvents.ts
@@ -0,0 +1,53 @@
+import { ethers } from "ethers";
+import { getContract } from "./ethers";
+
+interface Event {
+ eventId: number;
+ name: string;
+ description: string;
+ location: string;
+ capacity: number;
+ ticketsSold: number;
+ ticketPrice: number;
+ eventStartDate: number;
+ eventEndDate: number;
+ images: string[];
+ eventHost: string;
+}
+
+export const fetchEvents: () => Promise = async () => {
+ try {
+ console.log('Starting events call');
+ if (typeof window.ethereum === 'undefined') {
+ console.error('Ethereum provider not found');
+ return;
+ }
+
+ console.log('Connecting to contract');
+ const contract = getContract();
+ const eventCount = await contract.eventCounter();
+ const eventsData: Event[] = [];
+
+ for (let i = 0; i < eventCount; i++) {
+ const event = await contract.events(i);
+ const images = await contract.getEventImages(i);
+ eventsData.push({
+ eventId: i,
+ name: event.name,
+ description: event.description,
+ location: event.location,
+ capacity: event.capacity.toNumber(),
+ ticketsSold: event.ticketsSold.toNumber(),
+ ticketPrice: event.ticketPrice.toNumber() / 100,
+ eventStartDate: event.eventStartDate.toNumber(),
+ eventEndDate: event.eventEndDate.toNumber(),
+ images: images,
+ eventHost: event.eventHost,
+ });
+ }
+
+ return eventsData;
+ } catch (error) {
+ console.error('Failed to fetch events:', error);
+ }
+}
\ No newline at end of file