Skip to content

Commit

Permalink
feat: enhance AddNetwork and NetworkForm components with improved err…
Browse files Browse the repository at this point in the history
…or handling and dynamic name input
  • Loading branch information
nelitow committed Dec 16, 2024
1 parent f47465e commit 7651aa6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function NetworkForm({

const url = useWatch({ control, name: 'url' });
const chainId = useWatch({ control, name: 'chainId' });
const customName = useWatch({ control, name: 'name' });

useEffect(() => {
if (isReviewing && chainName) {
Expand All @@ -63,7 +64,7 @@ export function NetworkForm({
<>
<NetworkReviewCard
headerText="You're adding this network"
name={chainName || ''}
name={customName || chainName || ''}
chainId={chainId}
url={url}
/>
Expand All @@ -88,6 +89,11 @@ export function NetworkForm({
</MotionInput>
)}
/>
{formState.errors?.name && (
<Form.ErrorMessage>
{formState.errors.name.message}
</Form.ErrorMessage>
)}
</>
)}
{!isReviewing && (
Expand Down
5 changes: 4 additions & 1 deletion packages/app/src/systems/Network/machines/networksMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,11 @@ export const networksMachine = createMachine(
},
onDone: [
{
target: 'idle',
target: 'waitingAddNetwork',
cond: FetchMachine.hasError,
actions: assign({
error: (_, ev) => ev.data,
}),
},
{
actions: [
Expand Down
25 changes: 17 additions & 8 deletions packages/app/src/systems/Network/pages/AddNetwork/AddNetwork.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,24 @@ export function AddNetwork() {
});
}

function onAddNetwork() {
async function onAddNetwork() {
if (!name) return;
handlers.addNetwork({
data: {
chainId: Number(chainId),
name,
url,
},
});
try {
await handlers.addNetwork({
data: {
chainId: Number(chainId),
name,
url,
},
});
} catch (error) {
if (error instanceof Error && error.message.includes('already exists')) {
form.setError('name', {
type: 'manual',
message: 'A network with this name already exists',
});
}
}
}

return (
Expand Down

0 comments on commit 7651aa6

Please sign in to comment.