|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import { FormControl, Input } from "@chakra-ui/react"; |
| 3 | +import { |
| 4 | + Form, |
| 5 | + FormControl, |
| 6 | + FormDescription, |
| 7 | + FormField, |
| 8 | + FormItem, |
| 9 | + FormLabel, |
| 10 | + FormMessage, |
| 11 | +} from "@/components/ui/form"; |
| 12 | +import { Input } from "@/components/ui/input"; |
| 13 | +import { zodResolver } from "@hookform/resolvers/zod"; |
4 | 14 | import { TransactionButton } from "components/buttons/TransactionButton"; |
5 | 15 | import { useTrack } from "hooks/analytics/useTrack"; |
6 | 16 | import { useForm } from "react-hook-form"; |
7 | 17 | import { toast } from "sonner"; |
8 | | -import type { ThirdwebContract } from "thirdweb"; |
| 18 | +import { type ThirdwebContract, isAddress } from "thirdweb"; |
9 | 19 | import { mintAdditionalSupplyTo } from "thirdweb/extensions/erc1155"; |
10 | 20 | import { useActiveAccount, useSendAndConfirmTransaction } from "thirdweb/react"; |
11 | | -import { FormErrorMessage, FormHelperText, FormLabel } from "tw-components"; |
| 21 | +import { z } from "zod"; |
12 | 22 |
|
13 | 23 | interface MintSupplyTabProps { |
14 | 24 | contract: ThirdwebContract; |
15 | 25 | tokenId: string; |
16 | 26 | } |
17 | 27 |
|
18 | | -// Intended for Edition contracts (not Edition Drop) |
| 28 | +const mintAdditionalSupplyFormSchema = z.object({ |
| 29 | + to: z.string().refine((value) => isAddress(value), { |
| 30 | + message: "Invalid Ethereum address", |
| 31 | + }), |
| 32 | + amount: z.number().int().min(1, "Amount must be at least 1"), |
| 33 | +}); |
| 34 | + |
19 | 35 | const MintSupplyTab: React.FC<MintSupplyTabProps> = ({ contract, tokenId }) => { |
20 | 36 | const trackEvent = useTrack(); |
21 | 37 | const address = useActiveAccount()?.address; |
22 | | - const { |
23 | | - register, |
24 | | - handleSubmit, |
25 | | - formState: { errors }, |
26 | | - reset, |
27 | | - } = useForm<{ to: string; amount: string }>({ |
28 | | - defaultValues: { amount: "1", to: address }, |
| 38 | + |
| 39 | + const form = useForm<z.input<typeof mintAdditionalSupplyFormSchema>>({ |
| 40 | + resolver: zodResolver(mintAdditionalSupplyFormSchema), |
| 41 | + defaultValues: { |
| 42 | + amount: 1, |
| 43 | + to: address || "", |
| 44 | + }, |
29 | 45 | }); |
30 | 46 |
|
31 | | - const { mutateAsync, isPending } = useSendAndConfirmTransaction(); |
| 47 | + const sendAndConfirmTx = useSendAndConfirmTransaction(); |
32 | 48 |
|
33 | | - return ( |
34 | | - <form |
35 | | - className="flex w-full flex-col gap-2" |
36 | | - onSubmit={handleSubmit((data) => { |
37 | | - if (!data.to) { |
38 | | - return toast.error( |
39 | | - "Please enter the wallet address of the recipient.", |
40 | | - ); |
41 | | - } |
| 49 | + function onSubmit(values: z.input<typeof mintAdditionalSupplyFormSchema>) { |
| 50 | + trackEvent({ |
| 51 | + category: "nft", |
| 52 | + action: "mint-supply", |
| 53 | + label: "attempt", |
| 54 | + }); |
| 55 | + const transaction = mintAdditionalSupplyTo({ |
| 56 | + contract, |
| 57 | + to: values.to, |
| 58 | + tokenId: BigInt(tokenId), |
| 59 | + supply: BigInt(values.amount), |
| 60 | + }); |
| 61 | + const promise = sendAndConfirmTx.mutateAsync(transaction, { |
| 62 | + onSuccess: () => { |
42 | 63 | trackEvent({ |
43 | 64 | category: "nft", |
44 | 65 | action: "mint-supply", |
45 | | - label: "attempt", |
| 66 | + label: "success", |
46 | 67 | }); |
47 | | - const transaction = mintAdditionalSupplyTo({ |
48 | | - contract, |
49 | | - to: data.to, |
50 | | - tokenId: BigInt(tokenId), |
51 | | - supply: BigInt(data.amount), |
52 | | - }); |
53 | | - const promise = mutateAsync(transaction, { |
54 | | - onSuccess: () => { |
55 | | - trackEvent({ |
56 | | - category: "nft", |
57 | | - action: "mint-supply", |
58 | | - label: "success", |
59 | | - }); |
60 | | - reset(); |
61 | | - }, |
62 | | - onError: (error) => { |
63 | | - trackEvent({ |
64 | | - category: "nft", |
65 | | - action: "mint-supply", |
66 | | - label: "error", |
67 | | - error, |
68 | | - }); |
69 | | - console.error(error); |
70 | | - }, |
71 | | - }); |
72 | | - toast.promise(promise, { |
73 | | - loading: "Minting additional supply", |
74 | | - success: "NFT(s) minted successfully", |
75 | | - error: "Failed to mint NFT(s)", |
| 68 | + form.reset(); |
| 69 | + }, |
| 70 | + onError: (error) => { |
| 71 | + trackEvent({ |
| 72 | + category: "nft", |
| 73 | + action: "mint-supply", |
| 74 | + label: "error", |
| 75 | + error, |
76 | 76 | }); |
77 | | - })} |
78 | | - > |
79 | | - <div className="flex w-full flex-col gap-6 md:flex-row"> |
80 | | - <FormControl isRequired isInvalid={!!errors.to}> |
81 | | - <FormLabel>Amount</FormLabel> |
82 | | - <Input placeholder="1" {...register("amount")} min={1} /> |
83 | | - <FormHelperText>How many would you like to mint?</FormHelperText> |
84 | | - <FormLabel className="mt-3">Recipient</FormLabel> |
85 | | - <Input |
86 | | - placeholder="0x..." |
87 | | - defaultValue={address || ""} |
88 | | - {...register("to")} |
89 | | - /> |
90 | | - <FormHelperText> |
91 | | - Address to receive the NFT(s) - Defaults to your own wallet |
92 | | - </FormHelperText> |
93 | | - <FormErrorMessage>{errors.to?.message}</FormErrorMessage> |
94 | | - </FormControl> |
95 | | - </div> |
| 77 | + }, |
| 78 | + }); |
| 79 | + toast.promise(promise, { |
| 80 | + loading: "Minting NFT", |
| 81 | + success: "Minted successfully", |
| 82 | + error: "Failed to mint", |
| 83 | + }); |
| 84 | + } |
96 | 85 |
|
97 | | - <TransactionButton |
98 | | - txChainID={contract.chain.id} |
99 | | - transactionCount={1} |
100 | | - isLoading={isPending} |
101 | | - type="submit" |
102 | | - colorScheme="primary" |
103 | | - alignSelf="flex-end" |
| 86 | + return ( |
| 87 | + <Form {...form}> |
| 88 | + <form |
| 89 | + onSubmit={form.handleSubmit(onSubmit)} |
| 90 | + className="flex w-full flex-col gap-2" |
104 | 91 | > |
105 | | - Mint |
106 | | - </TransactionButton> |
107 | | - </form> |
| 92 | + <FormField |
| 93 | + control={form.control} |
| 94 | + name="amount" |
| 95 | + render={({ field }) => ( |
| 96 | + <FormItem> |
| 97 | + <FormLabel>Amount</FormLabel> |
| 98 | + <FormControl> |
| 99 | + <Input {...field} /> |
| 100 | + </FormControl> |
| 101 | + <FormDescription> |
| 102 | + How many would you like to mint? |
| 103 | + </FormDescription> |
| 104 | + <FormMessage /> |
| 105 | + </FormItem> |
| 106 | + )} |
| 107 | + /> |
| 108 | + |
| 109 | + <FormField |
| 110 | + control={form.control} |
| 111 | + name="to" |
| 112 | + render={({ field }) => ( |
| 113 | + <FormItem> |
| 114 | + <FormLabel>Recipient</FormLabel> |
| 115 | + <FormControl> |
| 116 | + <Input {...field} /> |
| 117 | + </FormControl> |
| 118 | + <FormMessage /> |
| 119 | + </FormItem> |
| 120 | + )} |
| 121 | + /> |
| 122 | + |
| 123 | + <TransactionButton |
| 124 | + txChainID={contract.chain.id} |
| 125 | + transactionCount={1} |
| 126 | + isLoading={sendAndConfirmTx.isPending} |
| 127 | + type="submit" |
| 128 | + colorScheme="primary" |
| 129 | + alignSelf="flex-end" |
| 130 | + > |
| 131 | + Mint |
| 132 | + </TransactionButton> |
| 133 | + </form> |
| 134 | + </Form> |
108 | 135 | ); |
109 | 136 | }; |
110 | 137 |
|
|
0 commit comments