|
1 |
| -import { Button, Text, VStack, Input } from '@chakra-ui/react'; |
| 1 | +import { |
| 2 | + Button, |
| 3 | + Text, |
| 4 | + VStack, |
| 5 | + Input, |
| 6 | + FormControl, |
| 7 | + FormLabel, |
| 8 | + FormErrorMessage, |
| 9 | +} from '@chakra-ui/react'; |
2 | 10 | import React from 'react';
|
3 | 11 | import { NETWORKS, Provider, useWallet, useContract } from '..';
|
4 | 12 | import { useTransaction } from '../hooks';
|
@@ -66,28 +74,35 @@ const UsingUseContract = () => {
|
66 | 74 | const contract = useContract(ADDRESS, ABI);
|
67 | 75 | const [value, setValue] = React.useState('');
|
68 | 76 |
|
69 |
| - const [greet, greetLoading, greetError] = useTransaction(async () => |
| 77 | + const greet = async () => { |
70 | 78 | // @ts-expect-error
|
71 |
| - alert(await contract.greet()) |
72 |
| - ); |
| 79 | + const greeting = await contract.greet(); |
| 80 | + alert(greeting); |
| 81 | + }; |
73 | 82 |
|
74 | 83 | // @ts-expect-error
|
75 | 84 | const [setGreeting, loading, error] = useTransaction(contract.setGreeting, [value]);
|
76 | 85 |
|
77 | 86 | if (connected) {
|
78 | 87 | return (
|
79 | 88 | <VStack>
|
80 |
| - <Button isLoading={greetLoading} onClick={greet}> |
81 |
| - Greet |
82 |
| - </Button> |
83 |
| - {greetError && <Text color='red'>Error while greeting: {greetError.message}</Text>} |
| 89 | + <Button onClick={greet}>Greet</Button> |
84 | 90 | <Button onClick={disconnectWallet}>Disconnect Wallet</Button>
|
85 |
| - <Text fontSize='lg'>Set Greeting</Text> |
86 |
| - <Input isDisabled={loading} value={value} onChange={e => setValue(e.target.value)} /> |
87 |
| - <Button isLoading={loading} onClick={setGreeting}> |
88 |
| - Set Greeting |
89 |
| - </Button> |
90 |
| - {error && <Text color='red'>Error while setting greeting: {error.message}</Text>} |
| 91 | + <FormControl isInvalid={!!error}> |
| 92 | + <VStack> |
| 93 | + <FormLabel htmlFor='setGreeting'>Set greeting</FormLabel> |
| 94 | + <Input |
| 95 | + id='setGreeting' |
| 96 | + isDisabled={loading} |
| 97 | + value={value} |
| 98 | + onChange={e => setValue(e.target.value)} |
| 99 | + /> |
| 100 | + <Button type='submit' isLoading={loading} onClick={setGreeting}> |
| 101 | + Set Greeting |
| 102 | + </Button> |
| 103 | + <FormErrorMessage>{error && error.message}</FormErrorMessage> |
| 104 | + </VStack> |
| 105 | + </FormControl> |
91 | 106 | </VStack>
|
92 | 107 | );
|
93 | 108 | }
|
|
0 commit comments