Skip to content

Commit

Permalink
Merge pull request #57 from peetzweg/pz/zod-form
Browse files Browse the repository at this point in the history
feat: use zod for form validation
  • Loading branch information
wottpal authored Feb 5, 2024
2 parents ee442ba + b753f5f commit 4c1f1c5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-deers-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inkathon/frontend": minor
---

uses zod for form validation
24 changes: 13 additions & 11 deletions frontend/src/components/web3/greeter-contract-interactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { FC, useEffect, useState } from 'react'

import { ContractIds } from '@/deployments/deployments'
import { zodResolver } from '@hookform/resolvers/zod'
import GreeterContract from '@inkathon/contracts/typed-contracts/contracts/greeter'
import {
contractQuery,
Expand All @@ -11,25 +12,29 @@ import {
useRegisteredContract,
useRegisteredTypedContract,
} from '@scio-labs/use-inkathon'
import { useForm } from 'react-hook-form'
import { SubmitHandler, useForm } from 'react-hook-form'
import toast from 'react-hot-toast'
import * as z from 'zod'

import { Button } from '@/components/ui/button'
import { Card, CardContent } from '@/components/ui/card'
import { Form, FormControl, FormItem, FormLabel } from '@/components/ui/form'
import { Input } from '@/components/ui/input'
import { contractTxWithToast } from '@/utils/contract-tx-with-toast'

type UpdateGreetingValues = { newMessage: string }
const formSchema = z.object({
newMessage: z.string().min(1).max(90),
})

export const GreeterContractInteractions: FC = () => {
const { api, activeAccount, activeSigner } = useInkathon()
const { contract, address: contractAddress } = useRegisteredContract(ContractIds.Greeter)
const { typedContract } = useRegisteredTypedContract(ContractIds.Greeter, GreeterContract)
const [greeterMessage, setGreeterMessage] = useState<string>()
const [fetchIsLoading, setFetchIsLoading] = useState<boolean>()
const [updateIsLoading, setUpdateIsLoading] = useState<boolean>()
const form = useForm<UpdateGreetingValues>()
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
})

const { register, reset, handleSubmit } = form

Expand Down Expand Up @@ -60,14 +65,12 @@ export const GreeterContractInteractions: FC = () => {
}, [typedContract])

// Update Greeting
const updateGreeting = async ({ newMessage }: UpdateGreetingValues) => {
const updateGreeting: SubmitHandler<z.infer<typeof formSchema>> = async ({ newMessage }) => {
if (!activeAccount || !contract || !activeSigner || !api) {
toast.error('Wallet not connected. Try again…')
return
}

// Send transaction
setUpdateIsLoading(true)
try {
await contractTxWithToast(api, activeAccount.address, contract, 'setMessage', {}, [
newMessage,
Expand All @@ -76,7 +79,6 @@ export const GreeterContractInteractions: FC = () => {
} catch (e) {
console.error(e)
} finally {
setUpdateIsLoading(false)
fetchGreeting()
}
}
Expand Down Expand Up @@ -115,12 +117,12 @@ export const GreeterContractInteractions: FC = () => {
<FormLabel className="text-base">Update Greeting</FormLabel>
<FormControl>
<div className="flex gap-2">
<Input disabled={updateIsLoading} {...register('newMessage')} />
<Input disabled={form.formState.isSubmitting} {...register('newMessage')} />
<Button
type="submit"
className="bg-primary font-bold"
disabled={fetchIsLoading || updateIsLoading}
isLoading={updateIsLoading}
disabled={fetchIsLoading || form.formState.isSubmitting}
isLoading={form.formState.isSubmitting}
>
Submit
</Button>
Expand Down

0 comments on commit 4c1f1c5

Please sign in to comment.