Skip to content

Commit 90538d8

Browse files
authored
Merge pull request #252 from hackdays-io/bugfix/issue246
bug fix for showing wrong nft
2 parents b69aabd + d62b644 commit 90538d8

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

frontend/src/hooks/useMintNFT.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ContractEvent,
23
getBlockNumber,
34
useAddress,
45
useContract,
@@ -49,7 +50,7 @@ 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 () => {
@@ -140,11 +141,11 @@ export const useGetOwnedNFTByAddress = (address?: string) => {
140141
const [isLoading, setIsLoading] = useState(true);
141142

142143
useEffect(() => {
143-
setIsLoading(true);
144144
setNfts([]);
145145
if (!ids) return;
146146

147147
const fetch = async () => {
148+
setIsLoading(true);
148149
const _nfts: any[] = [];
149150
for (const id of ids) {
150151
try {
@@ -259,11 +260,20 @@ export const useMintParticipateNFT = (
259260
}, [mintedTokenURI]);
260261

261262
useEffect(() => {
262-
if (status !== "success" || !data || data.length < 1) return;
263-
console.log(data);
264-
const tokenId = data[data.length - 1].data?.tokenId.toNumber();
263+
const includesNewEvent = (data: ContractEvent<Record<string, any>>[]) => {
264+
if (!fromBlock) return false;
265+
return data.some((event) => {
266+
return event.transaction.blockNumber > fromBlock;
267+
});
268+
};
269+
if (status !== "success" || !data || !includesNewEvent(data)) return;
270+
const tokenId = data
271+
.sort((a, b) => {
272+
return b.transaction.blockNumber - a.transaction.blockNumber;
273+
})[0]
274+
.data?.tokenId.toNumber();
265275
setMintedNFTId(tokenId);
266-
}, [data, status]);
276+
}, [data, status, fromBlock]);
267277

268278
const checkCanMint = useCallback(
269279
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)