@@ -2,7 +2,7 @@ import React, { useEffect, useState } from "react";
22import { ApiPromise } from "@polkadot/api" ;
33import type { InjectedAccountWithMeta } from "@polkadot/extension-inject/types" ;
44import type { Signer } from "@polkadot/types/types" ;
5- import { Button } from "./ui/Button" ;
5+ import { Button } from "./ui/components/ Button" ;
66import { decodeMetadata } from "../utils/utils" ;
77
88interface NFTMarketplaceProps {
@@ -164,6 +164,39 @@ export const NFTMarketplace: React.FC<NFTMarketplaceProps> = ({
164164 }
165165 } ;
166166
167+ const mintNft = async ( collectionId : number , itemId : number ) => {
168+ if ( ! api || ! account ?. address || ! signer ) {
169+ setError ( "Missing required properties for purchase" ) ;
170+ return ;
171+ }
172+
173+ setIsLoading ( true ) ;
174+ setError ( null ) ;
175+
176+ try {
177+ const tx = api . tx . template . mintNft ( collectionId , itemId ) ;
178+ await tx . signAndSend (
179+ account . address ,
180+ { signer } ,
181+ ( { status } ) => {
182+ if ( status . isInBlock ) {
183+ console . log (
184+ `NFT purchased in block ${ status . asInBlock . toString ( ) } `
185+ ) ;
186+ loadNFTs ( ) ; // Refresh NFTs after purchase
187+ }
188+ }
189+ ) ;
190+ } catch ( error ) {
191+ console . error ( "Failed to buy NFT:" , error ) ;
192+ setError (
193+ error instanceof Error ? error . message : "Failed to buy NFT"
194+ ) ;
195+ } finally {
196+ setIsLoading ( false ) ;
197+ }
198+ } ;
199+
167200 if ( isLoading && nfts . length === 0 ) {
168201 return (
169202 < div className = "text-center py-8" >
@@ -183,15 +216,13 @@ export const NFTMarketplace: React.FC<NFTMarketplaceProps> = ({
183216
184217 return (
185218 < div className = "max-w-6xl mx-auto p-6" >
186- < h2 className = "text-2xl font-bold text-gray-800 mb-6" >
187- Available NFTs
188- </ h2 >
219+ < h2 className = "text-2xl font-bold mb-6" > Available NFTs</ h2 >
189220
190221 < div className = "grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6" >
191222 { nfts . map ( ( nft ) => (
192223 < div
193224 key = { `${ nft . collectionId } -${ nft . id } ` }
194- className = "bg-white rounded-lg shadow-md overflow-hidden" >
225+ className = "bg-white rounded-lg shadow-md overflow-hidden hover:shadow-lg transition-shadow duration-300 " >
195226 { nft . metadata . image && (
196227 < img
197228 src = { nft . metadata . image }
@@ -221,16 +252,16 @@ export const NFTMarketplace: React.FC<NFTMarketplaceProps> = ({
221252
222253 < p className = "text-sm text-gray-500" >
223254 Status:{ " " }
224- { nft . is_sold
255+ { ! nft . is_sold
225256 ? "Sold"
226257 : "Available" }
227258 </ p >
228- { nft . owner === account . address && (
259+ { nft . owner === account . address ? (
229260 < div className = "space-y-2" >
230261 < input
231262 type = "text"
232263 placeholder = "Recipient address"
233- className = "w-full px-3 py-2 text-sm border border-gray-300 rounded-lg placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-blue-400"
264+ className = "w-full px-3 py-2 text-sm border border-gray-300 rounded-lg placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-blue-400 placeholder:text-gray-400 text-gray-700 "
234265 id = { `transfer-${ nft . collectionId } -${ nft . id } ` }
235266 />
236267 < Button
@@ -245,13 +276,29 @@ export const NFTMarketplace: React.FC<NFTMarketplaceProps> = ({
245276 input . value
246277 ) ;
247278 } }
248- disabled = { isLoading }
249- className = "w-full bg-blue-600 hover:bg-blue-700 text-white" >
250- { isLoading
251- ? "Transferring..."
252- : " Transfer NFT" }
279+ isLoading = { isLoading }
280+ variant = "primary"
281+ size = "sm"
282+ className = "w-full" >
283+ Transfer NFT
253284 </ Button >
254285 </ div >
286+ ) : (
287+ nft . is_sold && (
288+ < Button
289+ onClick = { ( ) =>
290+ mintNft (
291+ nft . collectionId ,
292+ nft . id
293+ )
294+ }
295+ isLoading = { isLoading }
296+ variant = "primary"
297+ size = "sm"
298+ className = "w-full" >
299+ Mint NFT
300+ </ Button >
301+ )
255302 ) }
256303 </ div >
257304 </ div >
0 commit comments