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