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

NFT component improvements #62

Merged
merged 7 commits into from
Dec 8, 2021
Merged

NFT component improvements #62

merged 7 commits into from
Dec 8, 2021

Conversation

Dhaiwat10
Copy link
Member

@Dhaiwat10 Dhaiwat10 commented Dec 3, 2021

  • ENS support
  • GIF support
  • Video support
  • Audio support
  • Fix the tests
  • Fetch data more efficiently. At the moment, both the Gallery and the NFT components are fetching the same data twice.

Also refactored the <NFT /> component to expect contractAddress and tokenId instead of accepting imageUrl etc.

Closes #61

@Dhaiwat10 Dhaiwat10 force-pushed the feat/nft-improvements branch from 685a1bf to 5949c5c Compare December 3, 2021 11:53
Copy link
Contributor

@etr2460 etr2460 left a comment

Choose a reason for hiding this comment

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

a few comments, looking awesome!

assetContractSymbol,
}: NFTProps) => {
const displayName = name || tokenId;
export const NFT = ({ contractAddress, tokenId }: NFTProps) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

do you think we could take the approach i mentioned here for the NFT component:#46 (comment)

Add a wrapper around NFT (and make the current implementation private) that uses a tokenId and contract address to fetch metadata about it

We should keep the existing component around and use it for the NFTGallery component so we don't need to refetch the NFT data that already comes from the previous API

Copy link
Member Author

Choose a reason for hiding this comment

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

@etr2460 missed that. That sounds fair

@@ -15,24 +22,66 @@ export interface NFTProps {
* TODO: Add a component that fetchs the NFT data from the blockchain and uses this component to
Copy link
Contributor

Choose a reason for hiding this comment

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

let's cleanup this TODO (now that it's done)

interface NFTData {
tokenId: string;
imageUrl?: string;
name: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

not every NFT has a name. that's why this was nullable and i had const displayName = name || tokenId; below

Copy link
Member Author

Choose a reason for hiding this comment

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

Gotcha

}, [contractAddress, tokenId]);

if (!nftData) {
return null;
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe let's render a loader with a box around it the size of the NFT card by default, so that we don't have the cards pop in and move the layout around?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, good idea


const fetchNFTData = useCallback(async () => {
const response = await (
await fetch(`https://api.opensea.io/api/v1/asset/${contractAddress}/${tokenId}/`)
Copy link
Contributor

Choose a reason for hiding this comment

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

don't forget error handling!

Also, we should decide as a group if we wanna use .then or async/awaits for fetching. i'm fine either way, but let's be consistent

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, will add error handling.

I prefer async/await. Wbu? And @crondinini?

Copy link
Contributor

Choose a reason for hiding this comment

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

me too, let's go with async/await. i was probably a bit lazy on my other PR to not factor that out. Maybe it's worth moving all the api requests into their own file too, that way any component can import them.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm more of an async/await kinda of person. But I'm fine either way as long as we have consistency.

In this case though, I don't think we need the 2 awaits. As far as I know .json() does not return a promise. I also think we don't need useCallback at this point.

Could do:

const response = (
      await fetch(`https://api.opensea.io/api/v1/asset/${contractAddress}/${tokenId}/`)
    ).json();

or

  const fetchNFTData = async () => {
    const fetchResponse = await fetch(`https://api.opensea.io/api/v1/asset/${contractAddress}/${tokenId}/`);

    if (_isMounted.current) {
      const response = fetchResponse.json();
      setNftData({
        tokenId: response.token_id,
        imageUrl: response.image_url,
        name: response.name,
        assetContractName: response.asset_contract.name,
        assetContractSymbol: response.asset_contract.symbol,
        animationUrl: response.animation_url,
      });
    }
  };

Copy link
Contributor

Choose a reason for hiding this comment

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

you should need 2 awaits. json() is async, because parsing the response can be expensive for extremely large responses

@@ -1,16 +1,19 @@
// File with API handlers shared across all tests.
// See https://kentcdodds.com/blog/stop-mocking-fetch for more details

import { rest } from "msw";
import { MOCK_OPENSEA_ASSETS_RESPONSE } from "./mocks";
import { rest } from 'msw';
Copy link
Contributor

Choose a reason for hiding this comment

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

weird that prettier didn't prettify this originally. must be a config issue

@Dhaiwat10 Dhaiwat10 force-pushed the feat/nft-improvements branch from 5949c5c to 0606463 Compare December 8, 2021 14:48
@Dhaiwat10 Dhaiwat10 marked this pull request as ready for review December 8, 2021 15:20
@Dhaiwat10 Dhaiwat10 merged commit 2114626 into main Dec 8, 2021
@Dhaiwat10 Dhaiwat10 deleted the feat/nft-improvements branch December 24, 2021 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NFT component improvements
3 participants