-
Notifications
You must be signed in to change notification settings - Fork 151
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
Conversation
685a1bf
to
5949c5c
Compare
There was a problem hiding this 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) => { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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}/`) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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,
});
}
};
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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
5949c5c
to
0606463
Compare
Also refactored the
<NFT />
component to expectcontractAddress
andtokenId
instead of acceptingimageUrl
etc.Closes #61