3
3
import { FC , useEffect , useState } from 'react'
4
4
5
5
import { ContractIds } from '@/deployments/deployments'
6
+ import { zodResolver } from '@hookform/resolvers/zod'
6
7
import GreeterContract from '@inkathon/contracts/typed-contracts/contracts/greeter'
7
8
import {
8
9
contractQuery ,
@@ -11,25 +12,29 @@ import {
11
12
useRegisteredContract ,
12
13
useRegisteredTypedContract ,
13
14
} from '@scio-labs/use-inkathon'
14
- import { useForm } from 'react-hook-form'
15
+ import { SubmitHandler , useForm } from 'react-hook-form'
15
16
import toast from 'react-hot-toast'
17
+ import * as z from 'zod'
16
18
17
19
import { Button } from '@/components/ui/button'
18
20
import { Card , CardContent } from '@/components/ui/card'
19
21
import { Form , FormControl , FormItem , FormLabel } from '@/components/ui/form'
20
22
import { Input } from '@/components/ui/input'
21
23
import { contractTxWithToast } from '@/utils/contract-tx-with-toast'
22
24
23
- type UpdateGreetingValues = { newMessage : string }
25
+ const formSchema = z . object ( {
26
+ newMessage : z . string ( ) . min ( 1 ) . max ( 90 ) ,
27
+ } )
24
28
25
29
export const GreeterContractInteractions : FC = ( ) => {
26
30
const { api, activeAccount, activeSigner } = useInkathon ( )
27
31
const { contract, address : contractAddress } = useRegisteredContract ( ContractIds . Greeter )
28
32
const { typedContract } = useRegisteredTypedContract ( ContractIds . Greeter , GreeterContract )
29
33
const [ greeterMessage , setGreeterMessage ] = useState < string > ( )
30
34
const [ fetchIsLoading , setFetchIsLoading ] = useState < boolean > ( )
31
- const [ updateIsLoading , setUpdateIsLoading ] = useState < boolean > ( )
32
- const form = useForm < UpdateGreetingValues > ( )
35
+ const form = useForm < z . infer < typeof formSchema > > ( {
36
+ resolver : zodResolver ( formSchema ) ,
37
+ } )
33
38
34
39
const { register, reset, handleSubmit } = form
35
40
@@ -60,14 +65,12 @@ export const GreeterContractInteractions: FC = () => {
60
65
} , [ typedContract ] )
61
66
62
67
// Update Greeting
63
- const updateGreeting = async ( { newMessage } : UpdateGreetingValues ) => {
68
+ const updateGreeting : SubmitHandler < z . infer < typeof formSchema > > = async ( { newMessage } ) => {
64
69
if ( ! activeAccount || ! contract || ! activeSigner || ! api ) {
65
70
toast . error ( 'Wallet not connected. Try again…' )
66
71
return
67
72
}
68
73
69
- // Send transaction
70
- setUpdateIsLoading ( true )
71
74
try {
72
75
await contractTxWithToast ( api , activeAccount . address , contract , 'setMessage' , { } , [
73
76
newMessage ,
@@ -76,7 +79,6 @@ export const GreeterContractInteractions: FC = () => {
76
79
} catch ( e ) {
77
80
console . error ( e )
78
81
} finally {
79
- setUpdateIsLoading ( false )
80
82
fetchGreeting ( )
81
83
}
82
84
}
@@ -115,12 +117,12 @@ export const GreeterContractInteractions: FC = () => {
115
117
< FormLabel className = "text-base" > Update Greeting</ FormLabel >
116
118
< FormControl >
117
119
< div className = "flex gap-2" >
118
- < Input disabled = { updateIsLoading } { ...register ( 'newMessage' ) } />
120
+ < Input disabled = { form . formState . isSubmitting } { ...register ( 'newMessage' ) } />
119
121
< Button
120
122
type = "submit"
121
123
className = "bg-primary font-bold"
122
- disabled = { fetchIsLoading || updateIsLoading }
123
- isLoading = { updateIsLoading }
124
+ disabled = { fetchIsLoading || form . formState . isSubmitting }
125
+ isLoading = { form . formState . isSubmitting }
124
126
>
125
127
Submit
126
128
</ Button >
0 commit comments