Skip to content

Commit 4aeeeb8

Browse files
authored
Merge pull request #254 from hackdays-io/staging
Bugfix: Show wrong NFT image after mint
2 parents a5714ca + fd3f26c commit 4aeeeb8

File tree

2 files changed

+77
-45
lines changed

2 files changed

+77
-45
lines changed

frontend/src/hooks/useMintNFT.ts

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ContractEvent,
23
getBlockNumber,
34
useAddress,
45
useContract,
@@ -49,21 +50,20 @@ export const useGetTokenURI = (id: number | null) => {
4950

5051
export const useGetOwnedNftIdsByAddress = (address?: string) => {
5152
const { mintNFTContract, isLoading } = useMintNFTContract();
52-
const [ids, setIds] = useState<number[]>([]);
53+
const [ids, setIds] = useState<number[]>();
5354

5455
useEffect(() => {
5556
const fetch = async () => {
5657
if (!address || isLoading) return;
57-
const _ids: number[] = [];
5858
const balance = await mintNFTContract?.call("balanceOf", [address]);
59-
for (let index = 0; index < balance.toNumber(); index++) {
60-
const tokenId = await mintNFTContract?.call("tokenOfOwnerByIndex", [
61-
address,
62-
index,
63-
]);
64-
_ids.push(tokenId.toNumber());
65-
}
66-
setIds(_ids);
59+
const tokenIdsPromise = Array(balance.toNumber())
60+
.fill("")
61+
.map((_, index) => {
62+
return mintNFTContract?.call("tokenOfOwnerByIndex", [address, index]);
63+
});
64+
const tokenIds = await Promise.all(tokenIdsPromise);
65+
66+
setIds(tokenIds.map((id) => id.toNumber()));
6767
};
6868
fetch();
6969
}, [address, isLoading]);
@@ -140,23 +140,34 @@ export const useGetOwnedNFTByAddress = (address?: string) => {
140140
const [isLoading, setIsLoading] = useState(true);
141141

142142
useEffect(() => {
143-
setIsLoading(true);
144143
setNfts([]);
145144
if (!ids) return;
146145

147146
const fetch = async () => {
148-
const _nfts: any[] = [];
149-
for (const id of ids) {
150-
try {
147+
setIsLoading(true);
148+
149+
const tokenURIPromises = ids.map((id) => {
150+
const getTokenURI = async (id: number) => {
151151
const tokenURI = await mintNFTContract?.call("tokenURI", [id]);
152-
const { data: metaData } = await axios.get(ipfs2http(tokenURI));
153-
_nfts.push({ ...metaData, tokenId: id });
154-
} catch (error) {
155-
console.log(error);
156-
continue;
157-
}
158-
}
159-
setNfts(_nfts);
152+
return { tokenURI, tokenId: id };
153+
};
154+
return getTokenURI(id);
155+
});
156+
const tokenURIs = await Promise.all(tokenURIPromises);
157+
158+
const metaDataPromises = tokenURIs.map(({ tokenURI, tokenId }) => {
159+
const getMetaData = async (tokenURI: string, tokenId: number) => {
160+
try {
161+
const { data: metaData } = await axios.get(ipfs2http(tokenURI));
162+
return { ...metaData, tokenId };
163+
} catch (error) {
164+
console.log(error);
165+
}
166+
};
167+
return getMetaData(tokenURI, tokenId);
168+
});
169+
const _nfts = await Promise.all(metaDataPromises);
170+
setNfts(_nfts.filter((nft) => nft));
160171
setIsLoading(false);
161172
};
162173

@@ -259,11 +270,20 @@ export const useMintParticipateNFT = (
259270
}, [mintedTokenURI]);
260271

261272
useEffect(() => {
262-
if (status !== "success" || !data || data.length < 1) return;
263-
console.log(data);
264-
const tokenId = data[data.length - 1].data?.tokenId.toNumber();
273+
const includesNewEvent = (data: ContractEvent<Record<string, any>>[]) => {
274+
if (!fromBlock) return false;
275+
return data.some((event) => {
276+
return event.transaction.blockNumber > fromBlock;
277+
});
278+
};
279+
if (status !== "success" || !data || !includesNewEvent(data)) return;
280+
const tokenId = data
281+
.sort((a, b) => {
282+
return b.transaction.blockNumber - a.transaction.blockNumber;
283+
})[0]
284+
.data?.tokenId.toNumber();
265285
setMintedNFTId(tokenId);
266-
}, [data, status]);
286+
}, [data, status, fromBlock]);
267287

268288
const checkCanMint = useCallback(
269289
async (eventId: number, secretPhrase: string) => {

frontend/src/pages/events/[eventid].tsx

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Heading, Spinner, Text, Container, Box } from "@chakra-ui/react";
22
import { useRouter } from "next/router";
3-
import { Fragment, useMemo } from "react";
3+
import { FC, Fragment, useMemo } from "react";
44
import { useGetEventById } from "../../hooks/useEventManager";
55
import LoginRequired from "../../components/atoms/web3/LoginRequired";
66
import { useLocale } from "../../hooks/useLocale";
@@ -9,15 +9,13 @@ import { MintForm } from "src/components/organisms/nft/MintForm";
99
import { useAddress } from "@thirdweb-dev/react";
1010
import { useGetOwnedNFTByAddress } from "src/hooks/useMintNFT";
1111
import { NFTItem } from "src/components/atoms/nft/NFTItem";
12+
import { Event } from "types/Event";
1213

13-
const Event = () => {
14-
const router = useRouter();
15-
const { eventid } = router.query;
16-
const { event, loading: fetchingEvent } = useGetEventById(Number(eventid));
14+
const MintNFTSection: FC<{ event: Event.EventRecord }> = ({ event }) => {
1715
const address = useAddress();
18-
const { t } = useLocale();
1916
const { nfts, isLoading: checkHoldingNFTs } =
2017
useGetOwnedNFTByAddress(address);
18+
2119
const holdingNFT = useMemo(() => {
2220
return nfts.find(
2321
(nft) =>
@@ -26,6 +24,32 @@ const Event = () => {
2624
);
2725
}, [nfts, address]);
2826

27+
return (
28+
<>
29+
{checkHoldingNFTs || !address ? (
30+
<Spinner />
31+
) : holdingNFT ? (
32+
<Box maxW={200} mx="auto" cursor="pointer">
33+
<NFTItem
34+
shareURL={false}
35+
nft={holdingNFT}
36+
tokenId={holdingNFT.tokenId}
37+
/>
38+
</Box>
39+
) : (
40+
<MintForm event={event} address={address} />
41+
)}
42+
</>
43+
);
44+
};
45+
46+
const Event: FC = () => {
47+
const router = useRouter();
48+
const { eventid } = router.query;
49+
const { event, loading: fetchingEvent } = useGetEventById(Number(eventid));
50+
51+
const { t } = useLocale();
52+
2953
return (
3054
<>
3155
<Container maxW={800} py={6} pb="120px">
@@ -47,19 +71,7 @@ const Event = () => {
4771
requiredChainID={+process.env.NEXT_PUBLIC_CHAIN_ID!}
4872
forbiddenText={t.SIGN_IN_TO_GET_NFT}
4973
>
50-
{checkHoldingNFTs || !address ? (
51-
<Spinner />
52-
) : holdingNFT ? (
53-
<Box maxW={200} mx="auto" cursor="pointer">
54-
<NFTItem
55-
shareURL={false}
56-
nft={holdingNFT}
57-
tokenId={holdingNFT.tokenId}
58-
/>
59-
</Box>
60-
) : (
61-
<MintForm event={event} address={address} />
62-
)}
74+
<MintNFTSection event={event} />
6375
</LoginRequired>
6476
</>
6577
)}

0 commit comments

Comments
 (0)