Skip to content

Commit

Permalink
fix: lending page
Browse files Browse the repository at this point in the history
  • Loading branch information
decebal committed Nov 17, 2024
1 parent e1745bd commit ff8fee1
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 102 deletions.
17 changes: 11 additions & 6 deletions packages/nextjs/app/lend/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { useCallback, useEffect, useState } from "react";
import Image from "next/image";
import { parseEther } from "viem";
import { useAccount, useWalletClient, useWriteContract } from "wagmi";
import { LoadNFTsComponent } from "~~/components/lend/LoadNFTs";
import { useScaffoldContract, useTransactor } from "~~/hooks/scaffold-eth";
import { useFetchNFTs } from "~~/hooks/useFetchNFTs";
import useNFTStore from "~~/services/store/useNFTStore";
Expand Down Expand Up @@ -70,12 +71,14 @@ const RentToOwnPage = () => {
// 3. Get the RentToOwn contract using the constant
// 4. Approve the NFT transfer
console.log("Approving NFT transfer...");
const approveTx = await writeContractAsync({
address: MyNFT.address,
abi: MyNFT.abi,
functionName: "approve",
args: [RentToOwn.address, currentTokenId],
});
const approveNFT = () =>
writeContractAsync({
address: MyNFT.address,
abi: MyNFT.abi,
functionName: "approve",
args: [RentToOwn.address, currentTokenId],
});
const approveTx = await writeTx(approveNFT, { blockConfirmations: 1 });
console.log("Approval transaction:", approveTx);

// 5. List the NFT
Expand Down Expand Up @@ -110,6 +113,8 @@ const RentToOwnPage = () => {
<div className="container mx-auto px-4 py-8">
<h1 className="text-3xl font-bold mb-8">Rent to Own NFTs</h1>

<LoadNFTsComponent />

<div className="bg-secondary rounded-lg shadow-md p-6 mb-8">
<h2 className="text-2xl font-semibold mb-4">Your NFTs</h2>
{isLoading ? (
Expand Down
137 changes: 61 additions & 76 deletions packages/nextjs/components/lend/LoadNFTs.tsx
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>
);
};
16 changes: 6 additions & 10 deletions packages/nextjs/hooks/useFetchNFTs.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import { useAccount } from "wagmi";
import { useScaffoldContract } from "~~/hooks/scaffold-eth";
import { NFT } from "~~/types/NFT";
import { notification } from "~~/utils/scaffold-eth";

interface NFT {
name: string;
tokenId: string;
tokenURI: string;
contractAddress: string;
}

export function useFetchNFTs() {
const { address: connectedAddress } = useAccount();
const { data: myNFTContract } = useScaffoldContract({
contractName: "MyNFT",
});

const fetchNFTs = async (): Promise<NFT[]> => {
const fetchNFTs = async (currentNftAddress?: string): Promise<NFT[]> => {
if (!myNFTContract || !connectedAddress) {
notification.error("Contract not available.");
return [];
}
const nfts = [];
const contractAddress = currentNftAddress || myNFTContract.address;

try {
const nfts = [];
//TODO use contractAddress to get tokenId, ownerOf, tokenURI and contract name

// Fetch current token ID
const currentTokenId = await myNFTContract.read.getCurrentTokenId();
Expand All @@ -37,7 +33,7 @@ export function useFetchNFTs() {
name: await myNFTContract.read.name(),
tokenId: `${i}`,
tokenURI: tokenURI.toString(),
contractAddress: myNFTContract.address,
contractAddress,
});
}
}
Expand Down
11 changes: 1 addition & 10 deletions packages/nextjs/services/store/useNFTStore.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { create } from "zustand";
import { devtools, persist } from "zustand/middleware";

/**
* NFT type definition
*/
interface NFT {
name: string;
tokenId: string;
tokenURI: string;
contractAddress: string;
}
import { NFT } from "~~/types/NFT";

/**
* Store shape definition
Expand Down
9 changes: 9 additions & 0 deletions packages/nextjs/types/NFT.ts
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;
}

0 comments on commit ff8fee1

Please sign in to comment.