-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
88 additions
and
102 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
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,76 +1,61 @@ | ||
// "use client"; | ||
// | ||
// import React, { useCallback, useState } from "react"; | ||
// import { useScaffoldContract } from "~~/hooks/scaffold-eth"; | ||
// | ||
// const LoadNFTsComponent = () => { | ||
// | ||
// // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
// const [nfts, setNfts] = useState<NFT[]>([]); // Specify the type for nfts | ||
// const [contractAddress, setContractAddress] = useState<string>(""); // State for contract address input | ||
// | ||
// const { data: myNFTContract, isLoading: myNFTIsLoading } = useScaffoldContract({ | ||
// contractName: "MyNFT", | ||
// }); | ||
// | ||
// const loadNFTs = async (contractAddress: string): Promise<NFT[]> => { | ||
// if (myNFTIsLoading || !myNFTContract) { | ||
// addErrorNotification("The NFT Contract is loading."); | ||
// return []; | ||
// } | ||
// // Get the current token ID | ||
// const currentTokenId = await myNFTContract.read.getCurrentTokenId(); | ||
// // console.log("Current token ID:", currentTokenId.toString()); | ||
// | ||
// // Create NFT object | ||
// const nfts: NFT[] = [ | ||
// { | ||
// name: await myNFTContract.read.name(), | ||
// tokenId: currentTokenId.toString(), | ||
// contractAddress: contractAddress, | ||
// }, | ||
// ]; | ||
// | ||
// // console.log("Found NFTs:", nfts); | ||
// return nfts; | ||
// }; | ||
// | ||
// const handleLoadNFTs = useCallback(async () => { | ||
// if (!contractAddress) { | ||
// alert("Please enter a valid contract address."); | ||
// return; | ||
// } | ||
// | ||
// try { | ||
// const nfts = await loadNFTs(contractAddress); | ||
// setNfts(nfts); | ||
// } catch (error) { | ||
// console.error("Error loading NFTs:", error); | ||
// addErrorNotification("Failed to load NFTs. Check console for details."); | ||
// } | ||
// // eslint-disable-next-line react-hooks/exhaustive-deps | ||
// }, [contractAddress, setContractAddress]); | ||
// | ||
// return ( | ||
// <div className="bg-secondary rounded-lg shadow-md p-6 mb-8"> | ||
// <h2 className="text-2xl font-semibold mb-4">Load NFTs from Contract</h2> | ||
// <div className="flex gap-4"> | ||
// <input | ||
// type="text" | ||
// placeholder="Enter NFT Contract Address" | ||
// value={contractAddress} | ||
// onChange={e => setContractAddress(e.target.value)} | ||
// className="flex-1 p-2 border rounded focus:outline-none focus:ring-2 focus:ring-blue-500 text-secondary" | ||
// /> | ||
// <button | ||
// onClick={handleLoadNFTs} | ||
// className="bg-info text-white px-6 py-2 rounded hover:bg-blue-600 transition-colors" | ||
// > | ||
// Load NFTs | ||
// </button> | ||
// </div> | ||
// </div> | ||
// ); | ||
// }; | ||
// | ||
// export default LoadNFTsComponent; | ||
"use client"; | ||
|
||
import React, { useCallback, useState } from "react"; | ||
import { useScaffoldContract } from "~~/hooks/scaffold-eth"; | ||
import { useFetchNFTs } from "~~/hooks/useFetchNFTs"; | ||
import useNFTStore from "~~/services/store/useNFTStore"; | ||
import { notification } from "~~/utils/scaffold-eth"; | ||
|
||
export const LoadNFTsComponent = () => { | ||
const { data: myNFTContract, isLoading: myNFTIsLoading } = useScaffoldContract({ | ||
contractName: "MyNFT", | ||
}); | ||
const [contractAddress, setContractAddress] = useState<string>(""); // State for contract address input | ||
const { setNFTs, setLoading } = useNFTStore(); | ||
const { fetchNFTs } = useFetchNFTs(); | ||
|
||
const handleLoadNFTs = useCallback(async () => { | ||
if (myNFTIsLoading || !myNFTContract || !contractAddress) { | ||
notification.error("Please enter a valid contract address."); | ||
return; | ||
} | ||
|
||
const loadNFTs = async () => { | ||
setLoading(true); | ||
try { | ||
const fetchedNFTs = await fetchNFTs(contractAddress); | ||
setNFTs(fetchedNFTs); | ||
} catch (err: unknown) { | ||
notification.error((err as Error)?.message); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
void loadNFTs(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [contractAddress]); | ||
|
||
return ( | ||
<div className="bg-secondary rounded-lg shadow-md p-6 mb-8"> | ||
<h2 className="text-2xl font-semibold mb-4">Load NFTs from Contract</h2> | ||
{myNFTContract && ( | ||
<div className="flex gap-4"> | ||
<input | ||
type="text" | ||
placeholder="Enter NFT Contract Address" | ||
value={contractAddress || myNFTContract.address} | ||
onChange={e => setContractAddress(e.target.value)} | ||
className="flex-1 p-2 border rounded focus:outline-none focus:ring-2 focus:ring-blue-500 text-secondary" | ||
/> | ||
<button | ||
onClick={handleLoadNFTs} | ||
className="bg-info text-white px-6 py-2 rounded hover:bg-blue-600 transition-colors" | ||
> | ||
Load NFTs | ||
</button> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; |
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
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* NFT type definition | ||
*/ | ||
export interface NFT { | ||
name: string; | ||
tokenId: string; | ||
tokenURI: string; | ||
contractAddress: string; | ||
} |