Skip to content

Commit

Permalink
chore: Add toggle for raw hex input in SimpleInput component
Browse files Browse the repository at this point in the history
  • Loading branch information
masiedu4 committed Aug 7, 2024
1 parent 7d9e98f commit 5f41825
Showing 1 changed file with 27 additions and 47 deletions.
74 changes: 27 additions & 47 deletions cartesi-rollups/tutorials/react-frontend-application.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,13 @@ Here, we are importing the generated `useWriteInputBoxAddInput` hook and Viem's
const SimpleInput = () => {
const dAppAddress = `0xab7528bb862fb57e8a2bcd567a2e929a0be56a5e`;
const [inputValue, setInputValue] = useState("");
const [hexInput, setHexInput] = useState<boolean>(false);

// ... rest of the component
};
```

We define the `dAppAddress` and create a state variable `inputValue` to manage the user's input.
We define the `dAppAddress` and create a state variable `inputValue` to manage the user's input. The `hexInput` state variable is used to toggle between the generic text input and hex values.

The `dAppAddress` is the address of the Cartesi backend that will receive the input. In this case, we are using a hardcoded address of a local dApp instance for demonstration purposes.

Expand All @@ -401,10 +402,13 @@ This hook provides us with state variables and a `writeContractAsync` function t
### Form submission and component rendering

```typescript
async function submit(event: React.FormEvent<HTMLFormElement>) {
async function submit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
await writeContractAsync({
args: [dAppAddress, stringToHex(inputValue)],
args: [
dAppAddress,
hexInput ? (inputValue as Hex) : stringToHex(inputValue),
],
});
}
```
Expand All @@ -418,40 +422,6 @@ The `inputValue` will be received by the particular backend address is `dAppAddr

Now, let us build our component JSX with an input field and a submit button, styled with Tailwind CSS. It also includes conditional rendering for success and error messages.

```javascript
return (
<div className="max-w-md mx-auto mt-10 p-6 bg-gradient-to-r from-purple-500 to-indigo-600 rounded-lg shadow-xl">
<h2 className="text-3xl font-bold text-white mb-6">Send Generic Input</h2>
<form onSubmit={submit} className="space-y-4">
<div>
<input
type="text"
className="w-full px-4 py-2 rounded-md border border-gray-300 focus:outline-none focus:ring-2 focus:ring-purple-500"
placeholder="Enter something"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
/>
</div>
<button
type="submit"
className="w-full px-4 py-2 bg-white text-purple-600 rounded-md hover:bg-purple-100 transition-colors duration-300 font-medium"
>
{isPending ? "Sending..." : "Send"}
</button>
</form>

{isSuccess && (
<p className="mt-4 text-green-300 font-bold">Transaction Sent</p>
)}

{error && (
<div className="mt-4 text-red-300">
Error: {(error as BaseError).shortMessage || error.message}
</div>
)}
</div>
);
```

### Final Component

Expand All @@ -465,43 +435,54 @@ Putting it all together, our complete `<SimpleInput/>` component and `App.tsx` l
import React, { useState } from "react";
import { BaseError } from "wagmi";
import { useWriteInputBoxAddInput } from "../hooks/generated";
import { stringToHex } from "viem";
import { Hex, stringToHex } from "viem";

const SimpleInput = () => {
const dAppAddress = `0xab7528bb862fb57e8a2bcd567a2e929a0be56a5e`;
const [inputValue, setInputValue] = useState("");
const [hexInput, setHexInput] = useState<boolean>(false);

const { isPending, isSuccess, error, writeContractAsync } =
useWriteInputBoxAddInput();

async function submit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
await writeContractAsync({
args: [dAppAddress, stringToHex(inputValue)],
args: [
dAppAddress,
hexInput ? (inputValue as Hex) : stringToHex(inputValue),
],
});
}

return (
<div className="max-w-md mx-auto mt-10 p-6 bg-gradient-to-r
from-purple-500 to-indigo-600 rounded-lg shadow-xl">
<div className="max-w-md mx-auto mt-10 p-6
bg-gradient-to-r from-purple-500 to-indigo-600
rounded-lg shadow-xl">
<h2 className="text-3xl font-bold text-white mb-6">Send Generic Input</h2>
<form onSubmit={submit} className="space-y-4">
<div>
<input
type="text"
className="w-full px-4 py-2 rounded-md border
border-gray-300 focus:outline-none focus:ring-2
focus:ring-purple-500"
border-gray-300 focus:outline-none
focus:ring-2 focus:ring-purple-500"
placeholder="Enter something"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
/>
<input
type="checkbox"
checked={hexInput}
onChange={(e) => setHexInput(!hexInput)}
/>
<span>Raw Hex </span>
</div>
<button
type="submit"
className="w-full px-4 py-2 bg-white text-purple-600
rounded-md hover:bg-purple-100 transition-colors
duration-300 font-medium"
className="w-full px-4 py-2 bg-white
text-purple-600 rounded-md hover:bg-purple-100
transition-colors duration-300 font-medium"
>
{isPending ? "Sending..." : "Send"}
</button>
Expand All @@ -522,7 +503,6 @@ const SimpleInput = () => {

export default SimpleInput;


```

</code></pre>
Expand Down

0 comments on commit 5f41825

Please sign in to comment.