Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic example of how to group NFTs by collection #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 38 additions & 29 deletions client/src/components/NFT.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import React, { useEffect, useState } from "react";
import axios from "axios";
import { SingleNFT } from "./SingleNFT";

const UNKNOWN_COLLECTION = 'Unknown';

const fun = async (x) => {
try {
Expand All @@ -10,15 +13,26 @@ const fun = async (x) => {
let val = await axios.get(x[i].data.uri);
arr.push(val);
}
return arr;

const collections = arr.reduce((acc, { data }) => {
const { family, name } = data.collection || {};
const collectionName = family || name || UNKNOWN_COLLECTION;
if (!acc[collectionName]) {
acc[collectionName] = [];
}
acc[collectionName].push(data);
return acc;
}, {});

return collections;
} catch (error) {
console.log(error);
}
};

const NFT = (props) => {
const [nft, setNft] = useState([]);
const [api, setApi] = useState([]);
const [api, setApi] = useState({});
const [loading, setLoading] = useState(false);
useEffect(() => {
if (props.valid === true) {
Expand All @@ -36,6 +50,8 @@ const NFT = (props) => {
data();
}, [nft]);

const collections = Object.keys(api);

return (
<>
{props.valid === true ? (
Expand All @@ -50,40 +66,33 @@ const NFT = (props) => {
<div className="row d-flex justify-content-center">
{loading ? (
<>
{api &&
api.length > 0 &&
api.map((val, ind) => {
return (
<div className="col-4 mt-3" key={ind}>
<div className="cart text-center">
<div className="img mt-4 pt-3">
<img src={val.data.image} alt="loading..." />
<p className="mt-1">{val.data.name}</p>
<h6 className=" mt-2">
{val.data.description}
</h6>
</div>
<div className="group mb-5 pb-2 mt-3 text-center">
<button>Stake</button>
<button>UnStake</button>
</div>
</div>
</div>
);
})}
{collections.map((name) => {
return (
<div class="row">
<h5>{name}</h5>
{api[name].map((item, idx) => (
<SingleNFT
key={`${name}-${idx}`}
name={item.name}
image={item.image}
/>
))}
</div>
);
})}
</>
) : (
<>
<p className="text-center">loading...</p>
</>
)}
<>
<p className="text-center">loading...</p>
</>
)}
</div>
</div>
</section>
</>
) : (
""
)}
""
)}
</>
);
};
Expand Down
21 changes: 21 additions & 0 deletions client/src/components/SingleNFT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from "react";

export const SingleNFT = ({ image, name, description }) => {
return (
<div className="col-4 mt-3">
<div className="cart text-center">
<div className="img mt-4 pt-3">
<img src={image} alt="loading..." />
<p className="mt-1">{name}</p>
<h6 className=" mt-2">
{description}
</h6>
</div>
<div className="group mb-5 pb-2 mt-3 text-center">
<button>Stake</button>
<button>UnStake</button>
</div>
</div>
</div>
);
}