Skip to content

Commit

Permalink
Merge pull request #42 from rsproule/fix/game-form-number-validation
Browse files Browse the repository at this point in the history
fix: form faults in bad input on game creation
  • Loading branch information
rsproule authored Jan 29, 2024
2 parents 87867e7 + 241fcab commit 8a4934d
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions frontend/src/components/CreateGameForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StandardMerkleTree } from "@openzeppelin/merkle-tree";
import { zodResolver } from "@hookform/resolvers/zod";
import * as z from "zod";
import { Button } from "./ui/button";
import { BaseError, getAddress } from "viem";
import { BaseError, getAddress, parseEther } from "viem";
import { useAccount, useWaitForTransaction } from "wagmi";
import {
usePrepareTankGameFactoryCreateGame,
Expand All @@ -29,7 +29,7 @@ const formSchema = z.object({
initHearts: z.number(),
initShootRange: z.number(),
epochSeconds: z.number(),
buyInMinimum: z.number(),
buyInMinimum: z.string(),
revealWaitBlocks: z.number(),
autoStart: z.boolean(),
root: z.string(),
Expand All @@ -49,7 +49,7 @@ export default function CreateGameForm({
initHearts: 3,
initShootRange: 3,
epochSeconds: 60,
buyInMinimum: 0,
buyInMinimum: "0",
revealWaitBlocks: 60,
autoStart: "true",
root: "0x0000000000000000000000000000000000000000000000000000000000000000",
Expand All @@ -71,7 +71,7 @@ export default function CreateGameForm({
initHearts: BigInt(formState.initHearts),
initShootRange: BigInt(formState.initShootRange),
epochSeconds: BigInt(formState.epochSeconds),
buyInMinimum: BigInt(formState.buyInMinimum),
buyInMinimum: parseEther(formState.buyInMinimum),
revealWaitBlocks: BigInt(formState.revealWaitBlocks),
autoStart: Boolean(formState.autoStart),
root: formState.root as `0x${string}`,
Expand Down Expand Up @@ -127,8 +127,11 @@ export default function CreateGameForm({
<Input
{...field}
onChange={(e) => {
if (!isNaN(Number(e.target.value))) {
try {
BigInt(e.target.value);
handleInputChange(e);
} catch {
console.error("Value is not convertible to BigInt");
}
}}
value={formState.playerCount}
Expand All @@ -148,15 +151,18 @@ export default function CreateGameForm({
<Input
{...field}
onChange={(e) => {
if (!isNaN(Number(e.target.value))) {
try {
BigInt(e.target.value);
handleInputChange(e);
} catch {
console.error("Value is not convertible to BigInt");
}
}}
value={formState.boardSize}
/>
</FormControl>
<FormDescription>
Enter the size of the board (must be div by 3)
Enter the size of the board (must be div by 3){" "}
</FormDescription>
</div>
)}
Expand All @@ -171,8 +177,11 @@ export default function CreateGameForm({
<Input
{...field}
onChange={(e) => {
if (!isNaN(Number(e.target.value))) {
try {
BigInt(e.target.value);
handleInputChange(e);
} catch {
console.error("Value is not convertible to BigInt");
}
}}
value={formState.initAPs}
Expand All @@ -192,8 +201,11 @@ export default function CreateGameForm({
<Input
{...field}
onChange={(e) => {
if (!isNaN(Number(e.target.value))) {
try {
BigInt(e.target.value);
handleInputChange(e);
} catch {
console.error("Value is not convertible to BigInt");
}
}}
value={formState.initHearts}
Expand Down Expand Up @@ -236,8 +248,11 @@ export default function CreateGameForm({
<Input
{...field}
onChange={(e) => {
if (!isNaN(Number(e.target.value))) {
try {
BigInt(e.target.value);
handleInputChange(e);
} catch {
console.error("Value is not convertible to BigInt");
}
}}
value={formState.epochSeconds}
Expand Down Expand Up @@ -278,8 +293,11 @@ export default function CreateGameForm({
<Input
{...field}
onChange={(e) => {
if (!isNaN(Number(e.target.value))) {
try {
BigInt(e.target.value);
handleInputChange(e);
} catch {
console.error("Value is not convertible to BigInt");
}
}}
value={formState.revealWaitBlocks}
Expand Down

0 comments on commit 8a4934d

Please sign in to comment.