mirror of
https://github.com/0xShay/ticketchain.git
synced 2026-01-11 21:23:24 +00:00
add num tickets
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React from 'react';
|
import React, { useState } from 'react';
|
||||||
import {
|
import {
|
||||||
Card,
|
Card,
|
||||||
CardHeader,
|
CardHeader,
|
||||||
@@ -32,8 +32,10 @@ const EventDescription: React.FC<EventDescriptionProps> = ({
|
|||||||
eventDetails,
|
eventDetails,
|
||||||
}) => {
|
}) => {
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
const [numTickets, setNumTickets] = useState(1);
|
||||||
|
|
||||||
const handleBuyNow = () => {
|
const handleBuyNow = () => {
|
||||||
buyHandler(eventDetails.EventID, toast);
|
buyHandler(eventDetails.EventID, numTickets, toast);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -79,7 +81,10 @@ const EventDescription: React.FC<EventDescriptionProps> = ({
|
|||||||
</Button>
|
</Button>
|
||||||
<div className="relative md:left-5">
|
<div className="relative md:left-5">
|
||||||
<NumberPicker
|
<NumberPicker
|
||||||
|
initialCount={numTickets}
|
||||||
|
min={1}
|
||||||
max={eventDetails.capacity - eventDetails.ticketsSold}
|
max={eventDetails.capacity - eventDetails.ticketsSold}
|
||||||
|
onChange={setNumTickets}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</CardFooter>
|
</CardFooter>
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
'use client';
|
import React from 'react';
|
||||||
import React, { useState } from 'react';
|
import { Button } from '../ui/button';
|
||||||
import { Button } from '../ui/button'; // Adjust import path to where your shadcn Button component is located
|
|
||||||
|
|
||||||
interface NumberPickerProps {
|
interface NumberPickerProps {
|
||||||
initialCount?: number;
|
initialCount?: number;
|
||||||
@@ -15,25 +14,23 @@ const NumberPicker: React.FC<NumberPickerProps> = ({
|
|||||||
max = 10,
|
max = 10,
|
||||||
onChange,
|
onChange,
|
||||||
}) => {
|
}) => {
|
||||||
const [count, setCount] = useState<number>(initialCount);
|
const [count, setCount] = React.useState(initialCount);
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (onChange) {
|
||||||
|
onChange(count);
|
||||||
|
}
|
||||||
|
}, [count, onChange]);
|
||||||
|
|
||||||
const increment = () => {
|
const increment = () => {
|
||||||
if (count < max) {
|
if (count < max) {
|
||||||
const newCount = count + 1;
|
setCount(count + 1);
|
||||||
setCount(newCount);
|
|
||||||
if (onChange) {
|
|
||||||
onChange(newCount);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const decrement = () => {
|
const decrement = () => {
|
||||||
if (count > min) {
|
if (count > min) {
|
||||||
const newCount = count - 1;
|
setCount(count - 1);
|
||||||
setCount(newCount);
|
|
||||||
if (onChange) {
|
|
||||||
onChange(newCount);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ type ToastFunction = (options: {
|
|||||||
|
|
||||||
export const buyHandler = async (
|
export const buyHandler = async (
|
||||||
eventId: number,
|
eventId: number,
|
||||||
|
numTickets: number,
|
||||||
toast: ToastFunction
|
toast: ToastFunction
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
if (eventId < 0) {
|
if (eventId < 0) {
|
||||||
@@ -24,6 +25,14 @@ export const buyHandler = async (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (numTickets <= 0) {
|
||||||
|
toast({
|
||||||
|
title: 'Please select at least one ticket.',
|
||||||
|
variant: 'destructive',
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (typeof window.ethereum === 'undefined') {
|
if (typeof window.ethereum === 'undefined') {
|
||||||
toast({
|
toast({
|
||||||
@@ -38,11 +47,11 @@ export const buyHandler = async (
|
|||||||
const signer = provider.getSigner();
|
const signer = provider.getSigner();
|
||||||
const contract = getContract().connect(signer);
|
const contract = getContract().connect(signer);
|
||||||
|
|
||||||
let ticketCost = await contract.getEventPriceFlare(eventId);
|
const singleTicketCost = await contract.getEventPriceFlare(eventId);
|
||||||
ticketCost = ticketCost.mul(105).div(100);
|
const totalTicketCost = singleTicketCost.mul(numTickets).mul(105).div(100);
|
||||||
const balance = await provider.getBalance(await signer.getAddress());
|
|
||||||
|
|
||||||
if (balance.lt(ticketCost)) {
|
const balance = await provider.getBalance(await signer.getAddress());
|
||||||
|
if (balance.lt(totalTicketCost)) {
|
||||||
toast({
|
toast({
|
||||||
title: 'Insufficient balance to cover ticket cost and gas fees.',
|
title: 'Insufficient balance to cover ticket cost and gas fees.',
|
||||||
variant: 'destructive',
|
variant: 'destructive',
|
||||||
@@ -50,14 +59,14 @@ export const buyHandler = async (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const tx = await contract.buyTicket(eventId, { value: ticketCost });
|
const tx = await contract.buyTicket(eventId, { value: totalTicketCost });
|
||||||
const receipt = await tx.wait();
|
const receipt = await tx.wait();
|
||||||
|
|
||||||
toast({
|
toast({
|
||||||
title: `Ticket purchased successfully! Transaction Hash: ${receipt.transactionHash}`,
|
title: `Tickets purchased successfully! Transaction Hash: ${receipt.transactionHash}`,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error buying ticket:', error);
|
console.error('Error buying tickets:', error);
|
||||||
toast({
|
toast({
|
||||||
title: 'Transaction failed. Please try again.',
|
title: 'Transaction failed. Please try again.',
|
||||||
variant: 'destructive',
|
variant: 'destructive',
|
||||||
|
|||||||
Reference in New Issue
Block a user