Skip to content

Commit

Permalink
add pagination to collection grid
Browse files Browse the repository at this point in the history
  • Loading branch information
samepant committed Jan 20, 2024
1 parent a55a3b3 commit a9410b4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 29 deletions.
8 changes: 8 additions & 0 deletions frontend/src/components/NFTGrid/NFTGrid.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@
padding: 20px;
font-size: 12px;
}

.pageButtons {
display: flex;
gap: 20px;
margin-top: 20px;
justify-content: center;
align-items: center;
}
43 changes: 34 additions & 9 deletions frontend/src/components/NFTGrid/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import useTokenBalances from "../../hooks/useTokenBalances"
import { getNFTContext, getNFTContexts } from "../../utils/getNFTContext"
import { MoralisNFT } from "../../types/Token"
import useCollection from "../../hooks/useCollection"
import Button from "../Button"

interface Props {
address: string
Expand Down Expand Up @@ -57,11 +58,16 @@ export const AccountNftGrid: React.FC<Props> = ({ address }) => {

export const CollectionNftGrid: React.FC<Props> = ({ address }) => {
const chainId = useChainId()
const { data, isLoading, error } = useCollection({
const { data, isLoading, fetchNextPage, fetchPreviousPage } = useCollection({
tokenAddress: address,
chainId,
})
const nftBalances = data || []
const currentPage = data?.pages[0]
const nftBalances =
(currentPage?.result as MoralisNFT[]) || ([] as MoralisNFT[])

const previousPageAvailable = (data?.pageParams[0] as string[]).length > 0
const nextPageAvailable = currentPage?.cursor && currentPage.cursor.length > 0

const deployedMechs = useDeployedMechs(getNFTContexts(nftBalances), chainId)

Expand All @@ -78,12 +84,31 @@ export const CollectionNftGrid: React.FC<Props> = ({ address }) => {
if (isLoading) return <Spinner />

return (
<ul className={classes.grid}>
{nfts.map((nft, index) => (
<li key={`${index}-${nft.token_address}`}>
<NFTGridItem chainId={chainId} nft={nft} />
</li>
))}
</ul>
<>
<ul className={classes.grid}>
{nfts.map((nft, index) => (
<li key={`${index}-${nft.token_address}`}>
<NFTGridItem chainId={chainId} nft={nft} />
</li>
))}
</ul>
<div className={classes.pageButtons}>
<Button
secondary
onClick={() => fetchPreviousPage()}
disabled={!previousPageAvailable}
>
Previous
</Button>

<Button
secondary
onClick={() => fetchNextPage()}
disabled={!nextPageAvailable}
>
Next
</Button>
</div>
</>
)
}
4 changes: 4 additions & 0 deletions frontend/src/components/NFTItem/NFTItem.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@

.tokenName {
max-width: 68%;
color: var(--text-color);
}
.tokenName:hover {
filter: brightness(1.2);
}

.main {
Expand Down
23 changes: 9 additions & 14 deletions frontend/src/components/NFTItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { formatUnits } from "viem"
import { MoralisNFT } from "../../types/Token"
import { getNFTContext } from "../../utils/getNFTContext"
import { AccountNftGrid } from "../NFTGrid"
import NFTMedia from "../NFTMedia"
import { Link } from "react-router-dom"

interface Props {
nft: MoralisNFT
Expand Down Expand Up @@ -41,26 +43,19 @@ const NFTItem: React.FC<Props> = ({ nft, chainId }) => {
return (
<div className={classes.itemContainer}>
<div className={classes.header}>
<p className={classes.tokenName}>{name}</p>
<Link
to={`/collection/${nft.token_address}`}
className={classes.tokenName}
>
<p>{name}</p>
</Link>

<p className={classes.tokenId} title={nft.token_id}>
{nft.token_id}
</p>
</div>
<div className={classes.main}>
{(imageError || !metadata?.image) && (
<div className={classes.noImage}></div>
)}
{!imageError && metadata?.image && (
<div className={classes.imageContainer}>
<img
src={metadata?.image}
alt={name}
className={classes.image}
onError={() => setImageError(true)}
/>
</div>
)}
<NFTMedia nft={nft} />

<ul className={classes.info}>
<li>
Expand Down
21 changes: 15 additions & 6 deletions frontend/src/hooks/useCollection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from "@tanstack/react-query"
import { useInfiniteQuery, useQuery } from "@tanstack/react-query"
import { MoralisApiListResponse, MoralisNFT } from "../types/Token"

interface Props {
Expand All @@ -12,21 +12,30 @@ if (!process.env.REACT_APP_PROXY_URL) {
}

const useCollection = ({ tokenAddress, chainId, page = 0 }: Props) => {
return useQuery({
return useInfiniteQuery({
queryKey: ["collection", chainId, tokenAddress],
queryFn: async () => {
queryFn: async ({ pageParam }) => {
if (!chainId || !tokenAddress) throw new Error("No chainId or token")

const params = new URLSearchParams([
["cursor", pageParam],
["media_items", "true"],
])
// get collection metadata
const nftRes = await fetch(
`${process.env.REACT_APP_PROXY_URL}/${chainId}/moralis/nft/${tokenAddress}?media_items=true`
`${
process.env.REACT_APP_PROXY_URL
}/${chainId}/moralis/nft/${tokenAddress}?${params.toString()}`
)
if (!nftRes.ok) {
throw new Error("NFT request failed")
}
const collection = (await nftRes.json()) as MoralisApiListResponse
return collection.result as MoralisNFT[]
return collection
},
initialPageParam: "",
maxPages: 1,
getNextPageParam: (lastPage) => lastPage.cursor || "",
getPreviousPageParam: (firstPage) => firstPage.cursor || "",
enabled: !!chainId && !!tokenAddress,
})
}
Expand Down

1 comment on commit a9410b4

@vercel
Copy link

@vercel vercel bot commented on a9410b4 Jan 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.