From f47465e959b7810682d7aa2c3bb663f84ce8e988 Mon Sep 17 00:00:00 2001 From: Nelito Junior Date: Mon, 16 Dec 2024 18:16:52 -0300 Subject: [PATCH 1/3] feat: enhance NetworkForm with dynamic name field and validation - Added a controlled input for customizing the network name in the NetworkForm component. - Implemented useEffect to set the network name based on the chainName when reviewing. - Updated AddNetwork component to ensure the name is required before adding a network. - Improved error handling in NetworkService to check for existing networks by name and URL separately. --- .../components/NetworkForm/NetworkForm.tsx | 43 ++++++++++++++++--- .../Network/pages/AddNetwork/AddNetwork.tsx | 5 ++- .../src/systems/Network/services/network.ts | 8 +++- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/packages/app/src/systems/Network/components/NetworkForm/NetworkForm.tsx b/packages/app/src/systems/Network/components/NetworkForm/NetworkForm.tsx index 0eebab47d..7f213d0ca 100644 --- a/packages/app/src/systems/Network/components/NetworkForm/NetworkForm.tsx +++ b/packages/app/src/systems/Network/components/NetworkForm/NetworkForm.tsx @@ -40,11 +40,17 @@ export function NetworkForm({ }: NetworkFormProps) { const [isFirstShownTestConnectionBtn, setIsFirstShownTestConnectionBtn] = useState(false); - const { control, formState } = form; + const { control, formState, setValue } = form; const url = useWatch({ control, name: 'url' }); const chainId = useWatch({ control, name: 'chainId' }); + useEffect(() => { + if (isReviewing && chainName) { + setValue('name', chainName); + } + }, [isReviewing, chainName, setValue]); + useEffect(() => { if (isValid && chainId) { setIsFirstShownTestConnectionBtn(true); @@ -54,12 +60,35 @@ export function NetworkForm({ return ( {isReviewing && ( - + <> + + + Network Name + + } + isRequired + isInvalid={Boolean(formState.errors?.name)} + render={({ field }) => ( + + + + )} + /> + )} {!isReviewing && ( <> diff --git a/packages/app/src/systems/Network/pages/AddNetwork/AddNetwork.tsx b/packages/app/src/systems/Network/pages/AddNetwork/AddNetwork.tsx index b31643cf8..c4f30e259 100644 --- a/packages/app/src/systems/Network/pages/AddNetwork/AddNetwork.tsx +++ b/packages/app/src/systems/Network/pages/AddNetwork/AddNetwork.tsx @@ -31,6 +31,7 @@ export function AddNetwork() { const form = useNetworkForm({ context }); const url = useWatch({ control: form.control, name: 'url' }); const chainId = useWatch({ control: form.control, name: 'chainId' }); + const name = useWatch({ control: form.control, name: 'name' }); const isValid = form.formState.isDirty && form.formState.isValid && @@ -62,7 +63,7 @@ export function AddNetwork() { } function onAddNetwork() { - const name = chainInfoToAdd?.name || ''; + if (!name) return; handlers.addNetwork({ data: { chainId: Number(chainId), @@ -101,7 +102,7 @@ export function AddNetwork() {