-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathForm.tsx
170 lines (157 loc) · 5.19 KB
/
Form.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
"use client"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { z } from "zod"
import { Button } from "@/components/ui/button"
import { useRouter } from "next/navigation"
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import SelectNetwok from "./Select"
import { useNetwork } from "@/hooks/useNetwork"
import { useResponse } from "@/hooks/useResponse";
import { useState } from "react"
const formSchema = z.object({
contractAddress: z.string().refine(value => value.length === 42, "Must be a valid contract address"),
tokenId: z.string().refine(value => value.length > 0, "Must be a valid token ID"),
network: z.enum(["Network.ETH_MAINNET", "Network.MATIC_MAINNET", "Network.OPT_MAINNET", "ethereum"]),
});
export default function NFTForm() {
const router = useRouter();
const { isNetwork } = useNetwork();
const [isLoading, setIsLoading] = useState(false);
const { setisResponse } = useResponse();
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
contractAddress: "",
tokenId: "",
network: "ethereum",
},
})
const Spinner = () => (
<svg
className="animate-spin h-5 w-5 text-white"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
strokeWidth="4"
></circle>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 4.418 3.582 8 8 8v-4c-2.19 0-4.17-.896-5.657-2.343z"
></path>
</svg>
);
function onSubmit(values: z.infer<typeof formSchema>) {
setIsLoading(true);
if (isNetwork === "Network.ETH_MAINNET" || isNetwork === "Network.MATIC_MAINNET" || isNetwork === "Network.OPT_MAINNET") {
values.network = isNetwork;
const requestData = {
contractAddress: values.contractAddress,
tokenId: values.tokenId,
network: values.network
};
console.log(requestData);
fetch('https://assignment1-we2b.onrender.com/getNFTMetadata', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestData)
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log(data);
setisResponse(data);
router.push('/metanft')
})
.catch(error => {
console.error('Error fetching metadata:', error);
}).finally(() => {
setIsLoading(false);
});;
}
}
return (
<div className="m-auto my-10 max-w-5xl space-y-10 px-3">
<Form {...form} >
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8 max-w-full ">
<FormField
control={form.control}
name="contractAddress"
render={({ field }) => (
<FormItem>
<FormLabel>Contract Address</FormLabel>
<FormControl>
<Input className="dark:bg-[primary]" placeholder="Paste NFT contract address" {...field} />
</FormControl>
<FormDescription>
This is the contract address of the NFT.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="tokenId"
render={({ field }) => (
<FormItem>
<FormLabel>Token ID</FormLabel>
<FormControl>
<Input className="dark:bg-[primary]" placeholder="Enter Token ID" {...field} />
</FormControl>
<FormDescription>
This is your unique token ID.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="network"
render={({ field }) => (
<FormItem>
<FormLabel className="text-center ">Network</FormLabel>
<FormControl>
<SelectNetwok />
</FormControl>
<FormDescription>
Select the network of the NFT.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<div className="flex justify-center ">
<Button className="max-w-5xl lg:w-[360px] text-center" type="submit" disabled={isLoading}>
{isLoading ? <> Fetching <Spinner /> </> : "Fetch metadata"}
</Button>
</div>
</form>
</Form>
</div>
)
}