From dbb4eef2460218f9175f15cbb316b7047195f3f1 Mon Sep 17 00:00:00 2001 From: Oleg Chendighelean Date: Fri, 4 Oct 2024 15:20:06 +0100 Subject: [PATCH 1/4] Add url validation to add network menu --- apps/web/package.json | 1 + .../Menu/NetworkMenu/EditNetworkMenu.tsx | 27 ++- pnpm-lock.yaml | 214 ++++++++++++++++-- 3 files changed, 216 insertions(+), 26 deletions(-) diff --git a/apps/web/package.json b/apps/web/package.json index c7dade2aff..796da4f3dc 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -32,6 +32,7 @@ "@chakra-ui/theme-tools": "^2.2.3", "@emotion/react": "^11.13.3", "@emotion/styled": "^11.13.0", + "@hookform/resolvers": "^3.9.0", "@reduxjs/toolkit": "^2.2.7", "@tanstack/react-query": "^5.59.0", "@taquito/beacon-wallet": "^20.0.1", diff --git a/apps/web/src/components/Menu/NetworkMenu/EditNetworkMenu.tsx b/apps/web/src/components/Menu/NetworkMenu/EditNetworkMenu.tsx index 35d4453fba..83e17dc8ec 100644 --- a/apps/web/src/components/Menu/NetworkMenu/EditNetworkMenu.tsx +++ b/apps/web/src/components/Menu/NetworkMenu/EditNetworkMenu.tsx @@ -1,8 +1,10 @@ import { Button, FormControl, FormErrorMessage, FormLabel, Input, VStack } from "@chakra-ui/react"; +import { zodResolver } from "@hookform/resolvers/zod"; import { useDynamicDrawerContext } from "@umami/components"; import { networksActions, useAppDispatch, useAvailableNetworks } from "@umami/state"; import { type Network } from "@umami/tezos"; import { useForm } from "react-hook-form"; +import { z } from "zod"; import { DrawerContentWrapper } from "../DrawerContentWrapper"; @@ -12,6 +14,17 @@ type EditNetworkMenuProps = { const removeTrailingSlashes = (url: string) => url.replace(/\/+$/g, ""); +const networkSchema = z.object({ + name: z.string().min(1, "Name is required"), + rpcUrl: z.string().min(1, "RPC URL is required").url("Enter a valid RPC URL"), + tzktApiUrl: z.string().min(1, "Tzkt API URL is required").url("Enter a valid Tzkt API URL"), + tzktExplorerUrl: z + .string() + .min(1, "Tzkt Explorer URL is required") + .url("Enter a valid Tzkt Explorer URL"), + buyTezUrl: z.string().url("Enter a valid Buy Tez URL").or(z.literal("")), +}); + export const EditNetworkMenu = ({ network }: EditNetworkMenuProps) => { const { goBack } = useDynamicDrawerContext(); const dispatch = useAppDispatch(); @@ -21,7 +34,11 @@ export const EditNetworkMenu = ({ network }: EditNetworkMenuProps) => { formState: { errors, isValid }, register, handleSubmit, - } = useForm({ mode: "onBlur", defaultValues: network }); + } = useForm({ + mode: "onBlur", + defaultValues: network, + resolver: zodResolver(networkSchema), + }); const onSubmit = (network: Network) => { dispatch(networksActions.upsertNetwork(network)); @@ -38,7 +55,6 @@ export const EditNetworkMenu = ({ network }: EditNetworkMenuProps) => { { if (availableNetworks.find(n => n.name === name)) { return "Network with this name already exists"; @@ -54,7 +70,6 @@ export const EditNetworkMenu = ({ network }: EditNetworkMenuProps) => { @@ -65,7 +80,6 @@ export const EditNetworkMenu = ({ network }: EditNetworkMenuProps) => { @@ -76,7 +90,6 @@ export const EditNetworkMenu = ({ network }: EditNetworkMenuProps) => { @@ -85,9 +98,11 @@ export const EditNetworkMenu = ({ network }: EditNetworkMenuProps) => { )} - + Buy Tez URL + + {errors.buyTezUrl && {errors.buyTezUrl.message}}