Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

j #2

Open
wants to merge 1 commit into
base: starter
Choose a base branch
from
Open

j #2

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 58 additions & 5 deletions src/components/FetchCandyMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,71 @@ import { FC, useEffect, useState } from "react"
import styles from "../styles/custom.module.css"

export const FetchCandyMachine: FC = () => {
const [candyMachineAddress, setCandyMachineAddress] = useState(null)
const [candyMachineAddress, setCandyMachineAddress] = useState("C8idUmyyWBK3A5JLQut8cMZurSRu4U7zj8FeWTbCTG4")
const [candyMachineData, setCandyMachineData] = useState(null)
const [pageItems, setPageItems] = useState(null)
const [page, setPage] = useState(1)
const { connection } = useConnection()
const metaplex = Metaplex.make(connection)
const fetchCandyMachine = async () => {

// Set page to 1 - we wanna be at the first page whenever we fetch a new Candy Machine
setPage(1)

const fetchCandyMachine = async () => {}
// fetch candymachine data
try {
const candyMachine = await metaplex
.candyMachinesV2()
.findByAddress({ address: new PublicKey(candyMachineAddress) })

const getPage = async (page, perPage) => {}
setCandyMachineData(candyMachine)
} catch (e) {
alert("Please submit a valid CMv2 address.")
}
}

const prev = async () => {}
const getPage = async (page, perPage) => {
const pageItems = candyMachineData.items.slice(
(page - 1) * perPage,
page * perPage
)

const next = async () => {}
// fetch metadata of NFTs for page
let nftData = []
for (let i = 0; i < pageItems.length; i++) {
let fetchResult = await fetch(pageItems[i].uri)
let json = await fetchResult.json()
nftData.push(json)
}

// set state
setPageItems(nftData)
}

const prev = async () => {
if (page - 1 < 1) {
setPage(1)
} else {
setPage(page - 1)
}
}

// next page
const next = async () => {
setPage(page + 1)
}
// fetch placeholder candy machine on load
useEffect(() => {
fetchCandyMachine()
}, [])

// fetch metadata for NFTs when page or candy machine changes
useEffect(() => {
if (!candyMachineData) {
return
}
getPage(page, 9)
}, [candyMachineData, page])

return (
<div>
Expand Down
46 changes: 44 additions & 2 deletions src/components/FetchNft.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,49 @@ import { FC, useEffect, useState } from "react"
import styles from "../styles/custom.module.css"

export const FetchNft: FC = () => {
const fetchNfts = async () => {}
const [nftData, setNftData] = useState(null)

return <div></div>
const { connection } = useConnection()
const wallet = useWallet()
const metaplex = Metaplex.make(connection).use(walletAdapterIdentity(wallet))

const fetchNfts = async () => {
if (!wallet.connected) {
return
}

// fetch NFTs for connected wallet
const nfts = await metaplex
.nfts()
.findAllByOwner({ owner: wallet.publicKey })

// fetch off chain metadata for each NFT
let nftData = []
for (let i = 0; i < nfts.length; i++) {
let fetchResult = await fetch(nfts[i].uri)
let json = await fetchResult.json()
nftData.push(json)
}

// set state
setNftData(nftData)
}
useEffect(() => {
fetchNfts()
}, [wallet])

return (
<div>
{nftData && (
<div className={styles.gridNFT}>
{nftData.map((nft) => (
<div>
<ul>{nft.name}</ul>
<img src={nft.image} />
</div>
))}
</div>
)}
</div>
)
}