-
Follow along with the course at 1:03:03:39, we are trying to display if th NFT is found on the front end, using the tokenURI, when patrick refreshes his front end he gets "Found", but mine says "Loading" is this error coming from my NFTBox.js code?
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 15 replies
-
Copy this (refactor paths and variable names as per your convention): NFTBox.js: import { useState, useEffect } from "react"
import { useWeb3Contract, useMoralis } from "react-moralis"
import nftMarketplaceAbi from "../constants/NftMarketplace.json"
import nftAbi from "../constants/BasicNft.json"
import Image from "next/image"
import { Card, useNotification } from "web3uikit"
import { ethers } from "ethers"
import UpdateListingModal from "./UpdateListingModal"
const truncateStr = (fullStr, strLen) => {
if (fullStr.length <= strLen) return fullStr
const separator = "..."
const seperatorLength = separator.length
const charsToShow = strLen - seperatorLength
const frontChars = Math.ceil(charsToShow / 2)
const backChars = Math.floor(charsToShow / 2)
return (
fullStr.substring(0, frontChars) +
separator +
fullStr.substring(fullStr.length - backChars)
)
}
export default function NFTBox({ price, nftAddress, tokenId, marketplaceAddress, seller }) {
const { isWeb3Enabled, account } = useMoralis()
const [imageURI, setImageURI] = useState("")
const [tokenName, setTokenName] = useState("")
const [tokenDescription, setTokenDescription] = useState("")
const [showModal, setShowModal] = useState(false)
const hideModal = () => setShowModal(false)
const dispatch = useNotification()
const { runContractFunction: getTokenURI } = useWeb3Contract({
abi: nftAbi,
contractAddress: nftAddress,
functionName: "tokenURI",
params: {
tokenId: tokenId,
},
})
const { runContractFunction: buyItem } = useWeb3Contract({
abi: nftMarketplaceAbi,
contractAddress: marketplaceAddress,
functionName: "buyItem",
msgValue: price,
params: {
nftAddress: nftAddress,
tokenId: tokenId,
},
})
async function updateUI() {
const tokenURI = await getTokenURI()
console.log(`The TokenURI is ${tokenURI}`)
// We are going to cheat a little here...
if (tokenURI) {
// IPFS Gateway: A server that will return IPFS files from a "normal" URL.
const requestURL = tokenURI.replace("ipfs://", "https://ipfs.io/ipfs/")
const tokenURIResponse = await (await fetch(requestURL)).json()
const imageURI = tokenURIResponse.image
const imageURIURL = imageURI.replace("ipfs://", "https://ipfs.io/ipfs/")
setImageURI(imageURIURL)
setTokenName(tokenURIResponse.name)
setTokenDescription(tokenURIResponse.description)
// We could render the Image on our sever, and just call our sever.
// For testnets & mainnet -> use moralis server hooks
// Have the world adopt IPFS
// Build our own IPFS gateway
}
// get the tokenURI
// using the image tag from the tokenURI, get the image
}
useEffect(() => {
if (isWeb3Enabled) {
updateUI()
}
}, [isWeb3Enabled])
const isOwnedByUser = seller === account || seller === undefined
const formattedSellerAddress = isOwnedByUser ? "you" : truncateStr(seller || "", 15)
const handleCardClick = () => {
isOwnedByUser
? setShowModal(true)
: buyItem({
onError: (error) => console.log(error),
onSuccess: handleBuyItemSuccess,
})
}
const handleBuyItemSuccess = async (tx) => {
await tx.wait(1)
dispatch({
type: "success",
message: "Item bought!",
title: "Item Bought",
position: "topR",
})
}
return (
<div>
<div>
{imageURI ? (
<div>
<UpdateListingModal
isVisible={showModal}
tokenId={tokenId}
marketplaceAddress={marketplaceAddress}
nftAddress={nftAddress}
onClose={hideModal}
/>
<Card
title={tokenName}
description={tokenDescription}
onClick={handleCardClick}
>
<div className="p-2">
<div className="flex flex-col items-end gap-2">
<div>#{tokenId}</div>
<div className="italic text-sm">
Owned by {formattedSellerAddress}
</div>
<Image
loader={() => imageURI}
src={imageURI}
height="200"
width="200"
/>
<div className="font-bold">
{ethers.utils.formatUnits(price, "ether")} ETH
</div>
</div>
</div>
</Card>
</div>
) : (
<div>Loading...</div>
)}
</div>
</div>
)
} |
Beta Was this translation helpful? Give feedback.
-
Your code : import { useEffect, useState } from "react"
import { useWeb3Contract, useMoralis } from "react-moralis"
import nftMarketplaceAbi from "../constants/NftMarketplace.json"
import nftAbi from "../constants/BasicNft.json"
export default function NFTBox({ price, nftAddress, tokenId, marketplaceAddress, seller }) {
const { isWeb3Enabled } = useMoralis()
const [imageURI, setImageURI] = useState("")
const { runContractFunction: getTokenURI } = useWeb3Contract({
abi: nftAbi,
contractAddress: nftAddress,
functionName: "tokenURI",
params: {
tokenId: tokenId,
},
})
async function updateUI() {
const tokenURI = await getTokenURI()
console.log(`The TokenURI is ${tokenURI}`)
// We are going to cheat a little here...
if (tokenURI) {
// IPFS Gateway: A server that will return IPFS files from a "normal" URL.
const requestURL = tokenURI.replace("ipfs://", "https://ipfs.io/ipfs/")
const tokenURIResponse = await (await fetch(requestURL)).json()
const imageURI = tokenURIResponse.image
const imageURIURL = imageURI.replace("ipfs://", "https://ipfs.io/ipfs/")
+ console.log("Inside If block : ",imageURIURL);
setImageURI(imageURIURL)
// We could render the Image on our sever, and just call our sever.
// For testnets & mainnet -> use moralis server hooks
// Have the world adopt IPFS
// Build our own IPFS gateway
}
// get the tokenURI
// using the image tag from the tokenURI, get the image
}
useEffect(() => {
if (isWeb3Enabled) {
updateUI()
}
}, [isWeb3Enabled])
return (
<div>
<div>{imageURI ? <div>Found It!</div> : <div>Loading...</div>}</div>
</div>
)
} |
Beta Was this translation helpful? Give feedback.
-
try updating your server from the moralis admin |
Beta Was this translation helpful? Give feedback.
-
Check if you did not forget to switch the Metamask to the hardhat-localhost network. At least that is what I was missing and I was getting the same behavior. |
Beta Was this translation helpful? Give feedback.
try updating your server from the moralis admin