forked from thirdweb-example/erc721
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
229 lines (208 loc) · 7.46 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import {
ChainId,
useClaimedNFTSupply,
useContractMetadata,
useNetwork,
useNFTDrop,
useUnclaimedNFTSupply,
useActiveClaimCondition,
useClaimNFT,
useWalletConnect,
useCoinbaseWallet,
} from '@thirdweb-dev/react';
import { useNetworkMismatch } from '@thirdweb-dev/react';
import { useAddress, useMetamask } from '@thirdweb-dev/react';
import { formatUnits, parseUnits } from 'ethers/lib/utils';
import type { NextPage } from 'next';
import { useState } from 'react';
import styles from '../styles/Theme.module.css';
// Put Your NFT Drop Contract address from the dashboard here
const myNftDropContractAddress = '0x322067594DBCE69A9a9711BC393440aA5e3Aaca1';
const Home: NextPage = () => {
const nftDrop = useNFTDrop(myNftDropContractAddress);
const address = useAddress();
const connectWithMetamask = useMetamask();
const connectWithWalletConnect = useWalletConnect();
const connectWithCoinbaseWallet = useCoinbaseWallet();
const isOnWrongNetwork = useNetworkMismatch();
const claimNFT = useClaimNFT(nftDrop);
const [, switchNetwork] = useNetwork();
// The amount the user claims
const [quantity, setQuantity] = useState(1); // default to 1
// Load contract metadata
const { data: contractMetadata } = useContractMetadata(
myNftDropContractAddress,
);
// Load claimed supply and unclaimed supply
const { data: unclaimedSupply } = useUnclaimedNFTSupply(nftDrop);
const { data: claimedSupply } = useClaimedNFTSupply(nftDrop);
// Load the active claim condition
const { data: activeClaimCondition } = useActiveClaimCondition(nftDrop);
// Check if there's NFTs left on the active claim phase
const isNotReady =
activeClaimCondition &&
parseInt(activeClaimCondition?.availableSupply) === 0;
// Check if there's any NFTs left
const isSoldOut = unclaimedSupply?.toNumber() === 0;
// Check price
const price = parseUnits(
activeClaimCondition?.currencyMetadata.displayValue || '0',
activeClaimCondition?.currencyMetadata.decimals,
);
// Multiply depending on quantity
const priceToMint = price.mul(quantity);
// Loading state while we fetch the metadata
if (!nftDrop || !contractMetadata) {
return <div className={styles.container}>Loading...</div>;
}
// Function to mint/claim an NFT
const mint = async () => {
if (isOnWrongNetwork) {
switchNetwork && switchNetwork(ChainId.Mumbai);
return;
}
claimNFT.mutate(
{ to: address as string, quantity },
{
onSuccess: () => {
alert(`Successfully minted NFT${quantity > 1 ? 's' : ''}!`);
},
onError: (err: any) => {
console.error(err);
alert(err?.message || 'Something went wrong');
},
},
);
};
return (
<div className={styles.container}>
<div className={styles.mintInfoContainer}>
<div className={styles.infoSide}>
{/* Title of your NFT Collection */}
<h1>{contractMetadata?.name}</h1>
{/* Description of your NFT Collection */}
<p className={styles.description}>{contractMetadata?.description}</p>
</div>
<div className={styles.imageSide}>
{/* Image Preview of NFTs */}
<img
className={styles.image}
src={contractMetadata?.image}
alt={`${contractMetadata?.name} preview image`}
/>
{/* Amount claimed so far */}
<div className={styles.mintCompletionArea}>
<div className={styles.mintAreaLeft}>
<p>Total Minted</p>
</div>
<div className={styles.mintAreaRight}>
{claimedSupply && unclaimedSupply ? (
<p>
{/* Claimed supply so far */}
<b>{claimedSupply?.toNumber()}</b>
{' / '}
{
// Add unclaimed and claimed supply to get the total supply
claimedSupply?.toNumber() + unclaimedSupply?.toNumber()
}
</p>
) : (
// Show loading state if we're still loading the supply
<p>Loading...</p>
)}
</div>
</div>
{/* Show claim button or connect wallet button */}
{address ? (
// Sold out or show the claim button
isSoldOut ? (
<div>
<h2>Sold Out</h2>
</div>
) : isNotReady ? (
<div>
<h2>Not ready to be minted yet</h2>
</div>
) : (
<>
<p>Quantity</p>
<div className={styles.quantityContainer}>
<button
className={`${styles.quantityControlButton}`}
onClick={() => setQuantity(quantity - 1)}
disabled={quantity <= 1}
>
-
</button>
<h4>{quantity}</h4>
<button
className={`${styles.quantityControlButton}`}
onClick={() => setQuantity(quantity + 1)}
disabled={
quantity >=
parseInt(
activeClaimCondition?.quantityLimitPerTransaction ||
'0',
)
}
>
+
</button>
</div>
<button
className={`${styles.mainButton} ${styles.spacerTop} ${styles.spacerBottom}`}
onClick={mint}
disabled={claimNFT.isLoading}
>
{claimNFT.isLoading
? 'Minting...'
: `Mint${quantity > 1 ? ` ${quantity}` : ''}${
activeClaimCondition?.price.eq(0)
? ' (Free)'
: activeClaimCondition?.currencyMetadata.displayValue
? ` (${formatUnits(
priceToMint,
activeClaimCondition.currencyMetadata.decimals,
)} ${
activeClaimCondition?.currencyMetadata.symbol
})`
: ''
}`}
</button>
</>
)
) : (
<div className={styles.buttons}>
<button
className={styles.mainButton}
onClick={connectWithMetamask}
>
Connect MetaMask
</button>
<button
className={styles.mainButton}
onClick={connectWithWalletConnect}
>
Connect with Wallet Connect
</button>
<button
className={styles.mainButton}
onClick={connectWithCoinbaseWallet}
>
Connect with Coinbase Wallet
</button>
</div>
)}
</div>
</div>
{/* Powered by thirdweb */}{' '}
<img
src="/logo.png"
alt="thirdweb Logo"
width={135}
className={styles.buttonGapTop}
/>
</div>
);
};
export default Home;