Skip to content

Commit

Permalink
move nft data processing to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPereira committed Jan 11, 2024
1 parent 3e62eaf commit 9de49ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
19 changes: 9 additions & 10 deletions packages/nextjs/components/portfolio/sections/Achievements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ const fetcher = (url: string) => fetch(url).then(res => res.json());
export function Achievements() {
const { data, error, isLoading } = useSWR(`${process.env.NEXT_PUBLIC_VERCEL_URL}/api/get-nfts`, fetcher);

// if (error) {
// console.error("nftsError", error);
// return (
// <SectionContainer>
// <SectionHeader title="Achievements" />
// <div className="text-center">Error loading achievement NFTs</div>
// </SectionContainer>
// );
// }
// Process the data only if it's available
const nfts = data
? data.map((nft: any) => ({
image: nft.image?.originalUrl,
description: nft.description,
name: nft.name,
}))
: [];

return (
<SectionContainer>
Expand All @@ -28,7 +27,7 @@ export function Achievements() {
<div className="text-center">Error loading achievement NFTs</div>
) : (
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-8 rounded-xl">
{data.data.map((nft: any, idx: number) => (
{nfts.map((nft: any, idx: number) => (
<div
key={idx}
className="flex flex-col items-center justify-center border border-base-content rounded-xl p-3 bg-base-200"
Expand Down
16 changes: 9 additions & 7 deletions packages/nextjs/pages/api/get-nfts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
try {
const address = "0x41f727fA294E50400aC27317832A9F78659476B9";
// Base URL
const url = `https://arb-mainnet.g.alchemy.com/nft/v3/${process.env.ALCHEMY_API_KEY}/getNFTsForOwner?owner=${address}&withMetadata=true&pageSize=100`;
const url = `https://arb-mainnet.g.alchemy.com/nft/v3/${process.env.ALCHEMY_API_KEY}/getNFTsForOwner?owner=${address}&withMetadata=true`;

// Fetch data
const response = await fetch(url);
Expand All @@ -16,15 +16,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

const data = await response.json();
console.log("data", data.ownedNfts[0]);
const nfts = data.ownedNfts;

// MAY BE CAUSING ERROR WITH SERVERLESS FUNCTION DEPLOYED TO VERCEL
// Process the data
const nfts = data.ownedNfts.map((nft: any) => ({
image: nft.image?.originalUrl,
description: nft.description,
name: nft.name,
}));
// const nfts = data.ownedNfts.map((nft: any) => ({
// image: nft.image?.originalUrl,
// description: nft.description,
// name: nft.name,
// }));

res.status(200).json({ data: nfts });
res.status(200).json(nfts);
} catch (error) {
// Log the error for server-side debugging
console.error("Failed to fetch NFT data:", error);
Expand Down

1 comment on commit 9de49ef

@vercel
Copy link

@vercel vercel bot commented on 9de49ef Jan 11, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

matt-pereira – ./

matt-pereira-git-main-mattpereira.vercel.app
matt-pereira.vercel.app
matt-pereira-mattpereira.vercel.app

Please sign in to comment.