-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from Ayush272002/profile
fix profile
- Loading branch information
Showing
6 changed files
with
224 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
npm test | ||
#!/usr/bin/env sh | ||
npx lint-staged && npm run build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,179 @@ | ||
'use client'; | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
import React, { useState, useEffect } from 'react'; | ||
import { ethers } from 'ethers'; | ||
import Header from '../../components/custom/header'; | ||
import Footer from '../../components/custom/footer'; | ||
import Profile from '@/components/custom/Profile'; | ||
import PreviousTickets from '@/components/PreviousTickets'; | ||
import { Button } from '@/components/ui/button'; | ||
import { | ||
Card, | ||
CardContent, | ||
CardDescription, | ||
CardHeader, | ||
CardTitle, | ||
} from '@/components/ui/card'; | ||
import EventManagerABI from '../../contracts/EventManagerABI.json'; | ||
|
||
const RPC_URL = process.env.NEXT_PUBLIC_RPC_URL; | ||
const CONTRACT_ADDRESS = process.env.NEXT_PUBLIC_CONTRACT_ADDRESS; | ||
|
||
if (!RPC_URL || !CONTRACT_ADDRESS) { | ||
console.error( | ||
'Missing environment variables. Please check your .env.local file.' | ||
); | ||
} | ||
|
||
const ProfilePage: React.FC = () => { | ||
export default function ProfilePage() { | ||
const [isClient, setIsClient] = useState(false); | ||
const [isAuth, setAuth] = useState(false); | ||
const [userTickets, setUserTickets] = useState<number[]>([]); | ||
const [userAddress, setUserAddress] = useState(''); | ||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState(''); | ||
const [networkName, setNetworkName] = useState(''); | ||
|
||
useEffect(() => { | ||
// This ensures the component renders only on the client side | ||
setIsClient(true); | ||
checkNetwork(); | ||
}, []); | ||
|
||
useEffect(() => {}); | ||
const checkNetwork = async () => { | ||
if (typeof window.ethereum !== 'undefined') { | ||
try { | ||
const provider = new ethers.providers.Web3Provider(window.ethereum); | ||
const network = await provider.getNetwork(); | ||
console.log('Network:', network); | ||
setNetworkName(network.name); | ||
} catch (error) { | ||
console.error('Failed to get network:', error); | ||
} | ||
} | ||
}; | ||
|
||
const connectWallet = async () => { | ||
setLoading(true); | ||
setError(''); | ||
if (typeof window.ethereum !== 'undefined') { | ||
try { | ||
await window.ethereum.request({ method: 'eth_requestAccounts' }); | ||
const provider = new ethers.providers.Web3Provider(window.ethereum); | ||
const signer = provider.getSigner(); | ||
const address = await signer.getAddress(); | ||
setUserAddress(address); | ||
await fetchUserTickets(address); | ||
} catch (error) { | ||
console.error('Failed to connect wallet:', error); | ||
setError('Failed to connect wallet. Please try again.'); | ||
} | ||
} else { | ||
setError('Please install MetaMask!'); | ||
} | ||
setLoading(false); | ||
}; | ||
|
||
const fetchUserTickets = async (address: string) => { | ||
if (!RPC_URL || !CONTRACT_ADDRESS) { | ||
setError('Missing configuration. Please contact support.'); | ||
return; | ||
} | ||
|
||
try { | ||
const provider = new ethers.providers.JsonRpcProvider(RPC_URL); | ||
const contract = new ethers.Contract( | ||
CONTRACT_ADDRESS, | ||
EventManagerABI, | ||
provider | ||
); | ||
|
||
console.log('Fetching tickets for address:', address); | ||
|
||
const tickets = await contract.getUserTickets(address); | ||
console.log('Raw tickets data:', tickets); | ||
|
||
if (Array.isArray(tickets)) { | ||
const ticketNumbers = tickets.map((ticket: ethers.BigNumber) => | ||
ticket.toNumber() | ||
); | ||
console.log('Processed ticket numbers:', ticketNumbers); | ||
setUserTickets(ticketNumbers); | ||
} else { | ||
console.error('Unexpected response from getUserTickets:', tickets); | ||
setError('Unexpected response from contract. Please try again.'); | ||
} | ||
} catch (error) { | ||
console.error('Failed to fetch user tickets:', error); | ||
setError(`Failed to fetch user tickets: ${(error as Error).message}`); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className="relative min-h-screen overflow-hidden bg-black"> | ||
<Header /> | ||
<div className="relative z-20 container mx-auto p-4 pt-16"> | ||
<div className="relative min-h-screen overflow-hidden"> | ||
{/* Video Background */} | ||
{isClient && ( | ||
<video | ||
autoPlay | ||
loop | ||
muted | ||
className="absolute inset-0 w-full h-full object-cover z-0" | ||
src="/BGVid2.mp4" // Ensure the video file is placed in the public folder | ||
> | ||
Your browser does not support the video tag. | ||
</video> | ||
<div className="relative min-h-screen overflow-hidden bg-black"> | ||
<Header /> | ||
<div className="relative z-20 container mx-auto p-4 pt-16"> | ||
<div className="relative min-h-screen overflow-hidden"> | ||
{isClient && ( | ||
<video | ||
autoPlay | ||
loop | ||
muted | ||
className="absolute inset-0 w-full h-full object-cover z-0" | ||
src="/BGVid2.mp4" | ||
> | ||
Your browser does not support the video tag. | ||
</video> | ||
)} | ||
|
||
<div className="absolute inset-0 bg-black opacity-50 z-10"></div> | ||
|
||
<div className="relative z-20 text-white"> | ||
<h1 className="text-4xl font-bold mb-4">Your Profile</h1> | ||
{userAddress ? ( | ||
<p className="mb-4">Connected Address: {userAddress}</p> | ||
) : ( | ||
<p className="mb-4">Not connected</p> | ||
)} | ||
<p className="mb-4">Current Network: {networkName}</p> | ||
|
||
{/* Dark Overlay for Enhanced Readability */} | ||
<div className="absolute inset-0 bg-black opacity-50 z-10"></div> | ||
|
||
<div className="relative flex flex-col space-y-10 mt-20 z-20 ml-10"> | ||
<Profile profileKey="38902kj2-8hf082gb0f20g02h082" /> | ||
|
||
{/* Render PreviousTickets component with appropriate props */} | ||
<PreviousTickets | ||
name="Sample Event" | ||
status={true} | ||
description="Description of the event" | ||
capacity={100} | ||
ticketPrice={50} | ||
eventStartDate={new Date('2024-11-01T10:00:00')} | ||
eventHost="Host Name" | ||
/> | ||
</div> | ||
{error && ( | ||
<div className="bg-red-500 text-white p-2 rounded mb-4"> | ||
{error} | ||
</div> | ||
)} | ||
|
||
<Card className="bg-gray-800 text-white"> | ||
<CardHeader> | ||
<CardTitle>Your Tickets</CardTitle> | ||
<CardDescription> | ||
Here are the tickets associated with your wallet | ||
</CardDescription> | ||
</CardHeader> | ||
<CardContent> | ||
{loading ? ( | ||
<p>Loading...</p> | ||
) : userTickets.length > 0 ? ( | ||
<ul> | ||
{userTickets.map((ticketId) => ( | ||
<li key={ticketId} className="mb-2"> | ||
Ticket ID: {ticketId} | ||
</li> | ||
))} | ||
</ul> | ||
) : ( | ||
<p>You don't have any tickets yet.</p> | ||
)} | ||
</CardContent> | ||
</Card> | ||
|
||
<Button className="mt-4" onClick={connectWallet} disabled={loading}> | ||
{loading | ||
? 'Connecting...' | ||
: userAddress | ||
? 'Refresh Tickets' | ||
: 'Connect Wallet'} | ||
</Button> | ||
</div> | ||
</div> | ||
<Footer /> | ||
</div> | ||
</> | ||
<Footer /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ProfilePage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,53 @@ | ||
import { ethers } from "ethers"; | ||
import { getContract } from "./ethers"; | ||
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; | ||
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<Event[] | undefined> = 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[] = []; | ||
try { | ||
console.log('Starting events call'); | ||
if (typeof window.ethereum === 'undefined') { | ||
console.error('Ethereum provider not found'); | ||
return; | ||
} | ||
|
||
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, | ||
}); | ||
} | ||
console.log('Connecting to contract'); | ||
const contract = getContract(); | ||
const eventCount = await contract.eventCounter(); | ||
const eventsData: Event[] = []; | ||
|
||
return eventsData; | ||
} catch (error) { | ||
console.error('Failed to fetch events:', error); | ||
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); | ||
} | ||
}; |